Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. Database & SysAdmin
  3. Database
  4. One to Many Relationships - Tabular Display

One to Many Relationships - Tabular Display

Scheduled Pinned Locked Moved Database
databasequestion
8 Posts 5 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • E Offline
    E Offline
    eddieangel
    wrote on last edited by
    #1

    I have designed my database with a one to many relationship where one user can have many statistics, but now I need to display the data in a single row format. The UserStatistics table has a foreign key to the User table (user_id in UserStatistics) Statistics might be height, weight, etc... The table should look like this: Name | Height | Weight Joe 6' 150 And so on. The header will always be the same (Name, Height, Weight or whatever) but any time the user does not have that particular statistic I need a blank column. Do I need to rejoin against the UserStatistics table over and over again to get the results I need or what?

    J M P V 4 Replies Last reply
    0
    • E eddieangel

      I have designed my database with a one to many relationship where one user can have many statistics, but now I need to display the data in a single row format. The UserStatistics table has a foreign key to the User table (user_id in UserStatistics) Statistics might be height, weight, etc... The table should look like this: Name | Height | Weight Joe 6' 150 And so on. The header will always be the same (Name, Height, Weight or whatever) but any time the user does not have that particular statistic I need a blank column. Do I need to rejoin against the UserStatistics table over and over again to get the results I need or what?

      J Offline
      J Offline
      Jorgen Andersson
      wrote on last edited by
      #2

      Assuming your statistics table looks like this:

      userid

      property

      value

      1

      height

      180

      1

      width

      100

      2

      height

      170

      2

      width

      110

      This query:

      Select userid
      ,max(case when property = 'height' then value else null end) as height
      ,max(case when property = 'width' then value else null end) as width
      from properties
      group by userid

      Would get this result:

      userid

      height

      width

      1

      180

      100

      2

      170

      110

      Depending on what database you're using, the pivot function might already be included and in that case it's easy enough to google how to use it.

      Light moves faster than sound. That is why some people appear bright, until you hear them speak. List of common misconceptions

      E 1 Reply Last reply
      0
      • E eddieangel

        I have designed my database with a one to many relationship where one user can have many statistics, but now I need to display the data in a single row format. The UserStatistics table has a foreign key to the User table (user_id in UserStatistics) Statistics might be height, weight, etc... The table should look like this: Name | Height | Weight Joe 6' 150 And so on. The header will always be the same (Name, Height, Weight or whatever) but any time the user does not have that particular statistic I need a blank column. Do I need to rejoin against the UserStatistics table over and over again to get the results I need or what?

        M Offline
        M Offline
        Mycroft Holmes
        wrote on last edited by
        #3

        Alternatively you can use a pivot [^]

        Never underestimate the power of human stupidity RAH

        1 Reply Last reply
        0
        • J Jorgen Andersson

          Assuming your statistics table looks like this:

          userid

          property

          value

          1

          height

          180

          1

          width

          100

          2

          height

          170

          2

          width

          110

          This query:

          Select userid
          ,max(case when property = 'height' then value else null end) as height
          ,max(case when property = 'width' then value else null end) as width
          from properties
          group by userid

          Would get this result:

          userid

          height

          width

          1

          180

          100

          2

          170

          110

          Depending on what database you're using, the pivot function might already be included and in that case it's easy enough to google how to use it.

          Light moves faster than sound. That is why some people appear bright, until you hear them speak. List of common misconceptions

          E Offline
          E Offline
          eddieangel
          wrote on last edited by
          #4

          This is MySql. Thank you for the suggestion, between you and Mycroft I will get it sorted out either using pivot or when ... then. Cheers, --EA

          J 1 Reply Last reply
          0
          • E eddieangel

            I have designed my database with a one to many relationship where one user can have many statistics, but now I need to display the data in a single row format. The UserStatistics table has a foreign key to the User table (user_id in UserStatistics) Statistics might be height, weight, etc... The table should look like this: Name | Height | Weight Joe 6' 150 And so on. The header will always be the same (Name, Height, Weight or whatever) but any time the user does not have that particular statistic I need a blank column. Do I need to rejoin against the UserStatistics table over and over again to get the results I need or what?

            P Offline
            P Offline
            PIEBALDconsult
            wrote on last edited by
            #5

            eddieangel wrote:

            rejoin against the UserStatistics table over and over again

            That's what I would do.

            1 Reply Last reply
            0
            • E eddieangel

              I have designed my database with a one to many relationship where one user can have many statistics, but now I need to display the data in a single row format. The UserStatistics table has a foreign key to the User table (user_id in UserStatistics) Statistics might be height, weight, etc... The table should look like this: Name | Height | Weight Joe 6' 150 And so on. The header will always be the same (Name, Height, Weight or whatever) but any time the user does not have that particular statistic I need a blank column. Do I need to rejoin against the UserStatistics table over and over again to get the results I need or what?

              V Offline
              V Offline
              vvashishta
              wrote on last edited by
              #6

              You can do this by using simple Join on these two tables.. Taking selected column in SELECT statement..

              - Happy Coding - Vishal Vashishta

              1 Reply Last reply
              0
              • E eddieangel

                This is MySql. Thank you for the suggestion, between you and Mycroft I will get it sorted out either using pivot or when ... then. Cheers, --EA

                J Offline
                J Offline
                Jorgen Andersson
                wrote on last edited by
                #7

                MySQL doesn't have a specific PIVOT functionality AFAIK.

                Light moves faster than sound. That is why some people appear bright, until you hear them speak. List of common misconceptions

                E 1 Reply Last reply
                0
                • J Jorgen Andersson

                  MySQL doesn't have a specific PIVOT functionality AFAIK.

                  Light moves faster than sound. That is why some people appear bright, until you hear them speak. List of common misconceptions

                  E Offline
                  E Offline
                  eddieangel
                  wrote on last edited by
                  #8

                  It does not have a native pivot feature, but your aggregate / group by solution will work perfectly. Cheers, --EA

                  1 Reply Last reply
                  0
                  Reply
                  • Reply as topic
                  Log in to reply
                  • Oldest to Newest
                  • Newest to Oldest
                  • Most Votes


                  • Login

                  • Don't have an account? Register

                  • Login or register to search.
                  • First post
                    Last post
                  0
                  • Categories
                  • Recent
                  • Tags
                  • Popular
                  • World
                  • Users
                  • Groups