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. Combining Data From Temp Tables

Combining Data From Temp Tables

Scheduled Pinned Locked Moved Database
questiontutorialdatabasehelp
11 Posts 4 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.
  • M Offline
    M Offline
    Matt U
    wrote on last edited by
    #1

    I deleted my last question since I feel like I've made some kind of progress. However, I'm still not sure how to group all of the data together. I have five tables that hold data. What I am doing is selecting: UserId, ProductNum, Qty into a temporary table for each master data table. Here's an example of what each temp table looks like:

    John PART-NUM-ONE 5
    Jane PART-NUM-ONE 8
    Jane PART-NUM-TWO 4
    Jack PART-NUM-THREE 9

    I have five tables structured just like that, except the Part Number and Qty will be different in each table. How can I build a query to group by Name, then by Part Number, and a column for each temp table (e.g. Passed, Finished, Scrapped)? I just can't seem to combine them. The Part Numbers come from a Parts table, which stores the Part Number.

    djj55: Nice but may have a permission problem Pete O'Hanlon: He has my permission to run it.

    D 1 Reply Last reply
    0
    • M Matt U

      I deleted my last question since I feel like I've made some kind of progress. However, I'm still not sure how to group all of the data together. I have five tables that hold data. What I am doing is selecting: UserId, ProductNum, Qty into a temporary table for each master data table. Here's an example of what each temp table looks like:

      John PART-NUM-ONE 5
      Jane PART-NUM-ONE 8
      Jane PART-NUM-TWO 4
      Jack PART-NUM-THREE 9

      I have five tables structured just like that, except the Part Number and Qty will be different in each table. How can I build a query to group by Name, then by Part Number, and a column for each temp table (e.g. Passed, Finished, Scrapped)? I just can't seem to combine them. The Part Numbers come from a Parts table, which stores the Part Number.

      djj55: Nice but may have a permission problem Pete O'Hanlon: He has my permission to run it.

      D Offline
      D Offline
      David Mujica
      wrote on last edited by
      #2

      I came a bit late to the problem. Can you confirm that in your example there are 4 temporary tables? Is this going to be static? (Always 4 Users) The solution appears to be using a series of Select statements with UNION.

      M 1 Reply Last reply
      0
      • D David Mujica

        I came a bit late to the problem. Can you confirm that in your example there are 4 temporary tables? Is this going to be static? (Always 4 Users) The solution appears to be using a series of Select statements with UNION.

        M Offline
        M Offline
        Matt U
        wrote on last edited by
        #3

        Well, I forgot to elaborate the end result I am going for. It is not a limit of users. The temp tables store every user's numbers for a particular piece of the process (Pass, Finished, Scrap, etc.). The end result I'm looking for is like this:

        Name Part Passed Finished Scrap

        John P-ONE 8 5 7
        Jane P-ONE 4 6 10
        Jane P-TWO 9 2 3
        Jack P-THREE 10 12 6

        Bring in the data from ALL temp tables into a group of columns, I guess sort of like a pivot. However, I can't seem to implement any kind of pivot in SQL that solves the problem.

        djj55: Nice but may have a permission problem Pete O'Hanlon: He has my permission to run it.

        J M 2 Replies Last reply
        0
        • M Matt U

          Well, I forgot to elaborate the end result I am going for. It is not a limit of users. The temp tables store every user's numbers for a particular piece of the process (Pass, Finished, Scrap, etc.). The end result I'm looking for is like this:

          Name Part Passed Finished Scrap

          John P-ONE 8 5 7
          Jane P-ONE 4 6 10
          Jane P-TWO 9 2 3
          Jack P-THREE 10 12 6

          Bring in the data from ALL temp tables into a group of columns, I guess sort of like a pivot. However, I can't seem to implement any kind of pivot in SQL that solves the problem.

          djj55: Nice but may have a permission problem Pete O'Hanlon: He has my permission to run it.

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

          I would use a pivot

          SELECT UserId,ProductNum,Passed,Finished,Scrapped
          FROM
          (SELECT UserId, ProductNum, Qty, 'passed' as status
          FROM Passed
          UNION
          SELECT UserId, ProductNum, Qty, 'finished' as status
          FROM finished
          UNION
          SELECT UserId, ProductNum, Qty, 'scrapped' as status
          FROM scrapped
          ) AS SourceTable
          PIVOT
          (
          sum(qty)
          FOR status IN ('passed', 'finished','scrapped')
          ) AS PivotTable;

          I havent tested this, but the principle should work. I also have the opinion that you should normalize the database, several tables with the same content isn't good. Put them together into one table with a status column.

          People say nothing is impossible, but I do nothing every day.

          M 1 Reply Last reply
          0
          • M Matt U

            Well, I forgot to elaborate the end result I am going for. It is not a limit of users. The temp tables store every user's numbers for a particular piece of the process (Pass, Finished, Scrap, etc.). The end result I'm looking for is like this:

            Name Part Passed Finished Scrap

            John P-ONE 8 5 7
            Jane P-ONE 4 6 10
            Jane P-TWO 9 2 3
            Jack P-THREE 10 12 6

            Bring in the data from ALL temp tables into a group of columns, I guess sort of like a pivot. However, I can't seem to implement any kind of pivot in SQL that solves the problem.

            djj55: Nice but may have a permission problem Pete O'Hanlon: He has my permission to run it.

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

            I agree with Jorgen, Pivot is you best option, and the database should be normalised, the design is rubbish! Alternatively you could do this:

            Select Name, Part, 1 Passed,0 Finished,0 Scrap
            from PassedTable
            UNION
            Select Name, Part, 0 Passed,1 Finished,0 Scrap
            from FinishedTable
            UNION
            Select Name, Part, 0 Passed,0 Finished,1 Scrap
            from ScrapTable

            Group and sum the results as required.

            Never underestimate the power of human stupidity RAH

            M J 2 Replies Last reply
            0
            • J Jorgen Andersson

              I would use a pivot

              SELECT UserId,ProductNum,Passed,Finished,Scrapped
              FROM
              (SELECT UserId, ProductNum, Qty, 'passed' as status
              FROM Passed
              UNION
              SELECT UserId, ProductNum, Qty, 'finished' as status
              FROM finished
              UNION
              SELECT UserId, ProductNum, Qty, 'scrapped' as status
              FROM scrapped
              ) AS SourceTable
              PIVOT
              (
              sum(qty)
              FOR status IN ('passed', 'finished','scrapped')
              ) AS PivotTable;

              I havent tested this, but the principle should work. I also have the opinion that you should normalize the database, several tables with the same content isn't good. Put them together into one table with a status column.

              People say nothing is impossible, but I do nothing every day.

              M Offline
              M Offline
              Matt U
              wrote on last edited by
              #6

              Thank you, Jorgen. I will give that a try in the morning and I will provide an update.

              djj55: Nice but may have a permission problem Pete O'Hanlon: He has my permission to run it.

              1 Reply Last reply
              0
              • M Mycroft Holmes

                I agree with Jorgen, Pivot is you best option, and the database should be normalised, the design is rubbish! Alternatively you could do this:

                Select Name, Part, 1 Passed,0 Finished,0 Scrap
                from PassedTable
                UNION
                Select Name, Part, 0 Passed,1 Finished,0 Scrap
                from FinishedTable
                UNION
                Select Name, Part, 0 Passed,0 Finished,1 Scrap
                from ScrapTable

                Group and sum the results as required.

                Never underestimate the power of human stupidity RAH

                M Offline
                M Offline
                Matt U
                wrote on last edited by
                #7

                How is it not normalized? We must keep up with each movement for historical purposes. The temp tables only contain the same type of data because I cannot figure out how to make it work with the core tables.

                djj55: Nice but may have a permission problem Pete O'Hanlon: He has my permission to run it.

                J 1 Reply Last reply
                0
                • M Mycroft Holmes

                  I agree with Jorgen, Pivot is you best option, and the database should be normalised, the design is rubbish! Alternatively you could do this:

                  Select Name, Part, 1 Passed,0 Finished,0 Scrap
                  from PassedTable
                  UNION
                  Select Name, Part, 0 Passed,1 Finished,0 Scrap
                  from FinishedTable
                  UNION
                  Select Name, Part, 0 Passed,0 Finished,1 Scrap
                  from ScrapTable

                  Group and sum the results as required.

                  Never underestimate the power of human stupidity RAH

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

                  Mycroft Holmes wrote:

                  Select Name, Part, 1 Qty Passed,0 Finished,0 Scrap
                  from PassedTable
                  UNION
                  Select Name, Part, 0 Passed,1 Qty Finished,0 Scrap
                  from FinishedTable
                  UNION
                  Select Name, Part, 0 Passed,0 Finished,1 Qty Scrap
                  from ScrapTable

                  FTFY This is probably more efficient than a normal Pivot. I'd use your suggestion instead.

                  People say nothing is impossible, but I do nothing every day.

                  1 Reply Last reply
                  0
                  • M Matt U

                    How is it not normalized? We must keep up with each movement for historical purposes. The temp tables only contain the same type of data because I cannot figure out how to make it work with the core tables.

                    djj55: Nice but may have a permission problem Pete O'Hanlon: He has my permission to run it.

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

                    Matt U. wrote:

                    How is it not normalized

                    Because you have five tables using the same columns and key Consider the following design instead:

                    UserId
                    ProductNum
                    Status
                    Qty
                    

                    Where status is Passed, Finished, Scrapped and so on. (Or rather a reference from a status table.) And if you need just the scrapped products you could use a view instead of a table for that purpose.

                    CREATE VIEW Finished AS
                    SELECT UserId,ProductNum,Qty
                    FROM mytable
                    WHERE status = 'finished'

                    How does the core tables look like if they present such a problem?

                    People say nothing is impossible, but I do nothing every day.

                    M 1 Reply Last reply
                    0
                    • J Jorgen Andersson

                      Matt U. wrote:

                      How is it not normalized

                      Because you have five tables using the same columns and key Consider the following design instead:

                      UserId
                      ProductNum
                      Status
                      Qty
                      

                      Where status is Passed, Finished, Scrapped and so on. (Or rather a reference from a status table.) And if you need just the scrapped products you could use a view instead of a table for that purpose.

                      CREATE VIEW Finished AS
                      SELECT UserId,ProductNum,Qty
                      FROM mytable
                      WHERE status = 'finished'

                      How does the core tables look like if they present such a problem?

                      People say nothing is impossible, but I do nothing every day.

                      M Offline
                      M Offline
                      Matt U
                      wrote on last edited by
                      #10

                      Well, I implemented the UNION suggestion and it works perfectly. And thanks to that solution I should not have any problem using the same concept with the raw tables. It wasn't the fact that I don't have any structure to it (but I don't claim to be an expert DBA or anything ;P). I've only had experience with joins, not unions and pivots and such. I appreciate all of the suggestions and input. I've got it working exactly how I needed it to. Thanks again, Jorgen and Mycroft. :)

                      djj55: Nice but may have a permission problem Pete O'Hanlon: He has my permission to run it.

                      J 1 Reply Last reply
                      0
                      • M Matt U

                        Well, I implemented the UNION suggestion and it works perfectly. And thanks to that solution I should not have any problem using the same concept with the raw tables. It wasn't the fact that I don't have any structure to it (but I don't claim to be an expert DBA or anything ;P). I've only had experience with joins, not unions and pivots and such. I appreciate all of the suggestions and input. I've got it working exactly how I needed it to. Thanks again, Jorgen and Mycroft. :)

                        djj55: Nice but may have a permission problem Pete O'Hanlon: He has my permission to run it.

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

                        You're welcome.

                        People say nothing is impossible, but I do nothing every day.

                        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