Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 529729
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T09:07:20+00:00 2026-05-13T09:07:20+00:00

I always struggle with dealing with normalised data, and how I display it. Maybe

  • 0

I always struggle with dealing with normalised data, and how I display it. Maybe its because I don’t fully understand the normalisation rules, like how to get it fully into Boyce-Codd. Performance is not really an issue at this stage, though maintainability of the schema is.

user

ID  Name
1   Alice
2   Bob
3   Charlie

skills

ID   Name
1    Karate
2    Marksmen
3    Cook

event

ID   Name
1    Island
2    Volcano

user-m2m-skill

MemberID  SkillID
1         1
1         2
2         1
2         3
3         1

user-m2m-event

MemberID  EventID
1         1
1         2
2         1
3         2

How do I get this information out of the database? I’d like to display a table like this, where I’ve got the total count of each skill:

Skills at event

Event    Karate Marksmen Cook
Island   2      1        1
Volcano  2      1        0

It is unlikely that the skills table will change very much. This means I could do a set of subqueries like this (obviously shortened and incorrect syntax)

SELECT event.name,
  (SELECT COUNT(*) FROM ... WHERE skill = 'Karate'), 
  (SELECT COUNT(*) FROM ... WHERE skill = 'Marksmen') FROM event

And that’s what I’ve been doing, putting it into a view. But its a bit horrible, no? I have to edit the view every time I add a new skill.

The other way to to process it client side. So I just get back something like this:

Event   Skill    Count
Island  Karate   2
Island  Marksmen 1
Island  Cook     1
Volcano Karate   2
Volcano Marksmen 1

And I loop through the results, reformatting it. But I hate that even more. Isn’t the database supposed to do data?

So: what am I doing wrong? Am I expecting too much? Which is the lesser evil?

(As b3ta would say, apologies for length of post and for bad markup. 🙁 )

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-13T09:07:20+00:00Added an answer on May 13, 2026 at 9:07 am

    This is a typical pivot query, because you are looking to convert data in rows into columns.

       SELECT e.name,
              MAX(CASE WHEN x.skill_name = 'Karate' THEN x.num_skill ELSE 0) END AS Karate,
              MAX(CASE WHEN x.skill_name = 'Marksmen' THEN x.num_skill ELSE 0 END) AS Marksmen
         FROM EVENT e
    LEFT JOIN (SELECT um.eventid,
                      s.name AS skill_name,
                      COUNT(*) 'num_skill'
                 FROM SKILLS s
                 JOIN USER-M2M-SKILL us ON us.skillid = s.id
                 JOIN USER-M2M-EVENT um ON um.memberid = us.memberid
             GROUP BY um.eventid, s.name) x ON x.eventid = e.id
     GROUP BY e.name
    

    Followup question:

    …what does this have that a load of sub queries doesn’t?

    SELECTs as statements within the SELECT clause. IE:

    SELECT x.name,
           (SELECT COUNT(*) FROM TABLE)
    

    …means that a separate query is run for every skill. If the queries were correllated – if they were tied together by an ID to make sure records sync’d with an event, then the count would be running for every event.

    Conclusion to Followup

    The approach is terribly inefficient. It is better to fetch the necessary values once, as I provided in my answer.

    Addendum

    Regarding updating the query – it is possible to minimize the maintenance by implementing the query with dynamic SQL.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 398k
  • Answers 398k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer You need a ValueConverter. Bind to the scrollableWidth property, and… May 15, 2026 at 3:39 am
  • Editorial Team
    Editorial Team added an answer Yes, window.opener exists as a reference to the window that… May 15, 2026 at 3:39 am
  • Editorial Team
    Editorial Team added an answer Is it a library stored in git and you are… May 15, 2026 at 3:39 am

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.