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. Pivot?

Pivot?

Scheduled Pinned Locked Moved Database
databasequestion
14 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.
  • M mark_w_

    I have the following data returned buy a simple SQL query | Site | X | Y | Z | -------------------- A 1 2 3 B 4 5 6 C 7 8 9 I need to get it to the following format | A | B | C | -------------- 1 4 7 2 5 8 3 6 9 Any ideas / code snippits most welcome Mark

    G Offline
    G Offline
    Goutam Patra
    wrote on last edited by
    #2

    See PIVOT[^] in SQL Server. Even a Google on Pivot sql server[^] will help you a lot.

    M 1 Reply Last reply
    0
    • G Goutam Patra

      See PIVOT[^] in SQL Server. Even a Google on Pivot sql server[^] will help you a lot.

      M Offline
      M Offline
      mark_w_
      wrote on last edited by
      #3

      I have done that! Can't work it out tho, as all the example sum values and I dont want to do that. TSQL is not my strong point

      G 1 Reply Last reply
      0
      • M mark_w_

        I have done that! Can't work it out tho, as all the example sum values and I dont want to do that. TSQL is not my strong point

        G Offline
        G Offline
        Goutam Patra
        wrote on last edited by
        #4

        Well Here[^] is an example on Pivot two or more columns. Read through it and try out.

        1 Reply Last reply
        0
        • M mark_w_

          I have the following data returned buy a simple SQL query | Site | X | Y | Z | -------------------- A 1 2 3 B 4 5 6 C 7 8 9 I need to get it to the following format | A | B | C | -------------- 1 4 7 2 5 8 3 6 9 Any ideas / code snippits most welcome Mark

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

          Do I understand that both your sites (A,B,C) and your attributes (X,Y,Z) are variable in number ? And you are counting some sort of occurrence ? My pivot tables show the months across the top, the salesman down the left and the number in the grid represents the number of orders closed in that month. Sound familiar ? Your Pivot might look something like this: select Salesman, [1] as Jan,[2] Feb,[3] Mar,[4] Apr,[5] May,[6] Jun, [7] Jul, [8] Aug, [9] Sep, [10] Oct, [11] Nov, [12] Dec from ( select salesman, OrderID, datepart(month,order_date) as month from orders where datepart(year,order_date) = 2010) AS SourceTable PIVOT (COUNT(OrderID) FOR month in ([1],[2],[3],[4],[5],[6],[7],[8],[9],[10],[11],[12])) AS PivotTable

          M 1 Reply Last reply
          0
          • D David Mujica

            Do I understand that both your sites (A,B,C) and your attributes (X,Y,Z) are variable in number ? And you are counting some sort of occurrence ? My pivot tables show the months across the top, the salesman down the left and the number in the grid represents the number of orders closed in that month. Sound familiar ? Your Pivot might look something like this: select Salesman, [1] as Jan,[2] Feb,[3] Mar,[4] Apr,[5] May,[6] Jun, [7] Jul, [8] Aug, [9] Sep, [10] Oct, [11] Nov, [12] Dec from ( select salesman, OrderID, datepart(month,order_date) as month from orders where datepart(year,order_date) = 2010) AS SourceTable PIVOT (COUNT(OrderID) FOR month in ([1],[2],[3],[4],[5],[6],[7],[8],[9],[10],[11],[12])) AS PivotTable

            M Offline
            M Offline
            mark_w_
            wrote on last edited by
            #6

            [int] NOT NULL, [Y] [int] NOT NULL, [Z] [int] NOT NULL ) ON [PRIMARY] insert into Example(Site, X,Y,Z) Values ('A',1,2,3) insert into Example(Site, X,Y,Z) Values ('B',4,5,6) insert into Example(Site, X,Y,Z) Values ('C',7,8,9) select * from Example

            1 Reply Last reply
            0
            • M mark_w_

              I have the following data returned buy a simple SQL query | Site | X | Y | Z | -------------------- A 1 2 3 B 4 5 6 C 7 8 9 I need to get it to the following format | A | B | C | -------------- 1 4 7 2 5 8 3 6 9 Any ideas / code snippits most welcome Mark

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

              This article [^]may be useful, I use MAX (min will do just as well) for the aggregator.

              Never underestimate the power of human stupidity RAH

              M 1 Reply Last reply
              0
              • M Mycroft Holmes

                This article [^]may be useful, I use MAX (min will do just as well) for the aggregator.

                Never underestimate the power of human stupidity RAH

                M Offline
                M Offline
                mark_w_
                wrote on last edited by
                #8

                I think the main problem is I need multiple aggregates (for X, Y and Z). I have this so far select * from Example pivot ( Max(X) for site in ([A],[B],[C]) ) as p

                M 1 Reply Last reply
                0
                • M mark_w_

                  I think the main problem is I need multiple aggregates (for X, Y and Z). I have this so far select * from Example pivot ( Max(X) for site in ([A],[B],[C]) ) as p

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

                  Not sure I understand your problem, are you saying you want 3 aggregates(X,Y,Z) for each type(columns) (A,B,C)

                  Never underestimate the power of human stupidity RAH

                  M 1 Reply Last reply
                  0
                  • M Mycroft Holmes

                    Not sure I understand your problem, are you saying you want 3 aggregates(X,Y,Z) for each type(columns) (A,B,C)

                    Never underestimate the power of human stupidity RAH

                    M Offline
                    M Offline
                    mark_w_
                    wrote on last edited by
                    #10

                    I think thats what I need, but not sure. Basically I want to rotate the returned data by 90 degrees

                    J M 2 Replies Last reply
                    0
                    • M mark_w_

                      I think thats what I need, but not sure. Basically I want to rotate the returned data by 90 degrees

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

                      You need to unpivot first, or normalize it actually, into something similar to this format:

                      CREATE TABLE [dbo].[Example](
                      [Site] [nvarchar](50) COLLATE Latin1_General_CI_AS NOT NULL,
                      [Accident] [nvarchar] NOT NULL,
                      [Occations] [int] NOT NULL,
                      ) ON [PRIMARY]

                      Either in the query or a temp table, but preferably it should be stored normalized in the database After that you can make the pivot the normal way

                      "When did ignorance become a point of view" - Dilbert

                      M 1 Reply Last reply
                      0
                      • M mark_w_

                        I think thats what I need, but not sure. Basically I want to rotate the returned data by 90 degrees

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

                        mark_w_ wrote:

                        I think thats what I need, but not sure.

                        In that case work through the article because that is EXACTLY what it does with 2 columns, just extend it to meet your requirements.

                        Never underestimate the power of human stupidity RAH

                        1 Reply Last reply
                        0
                        • J Jorgen Andersson

                          You need to unpivot first, or normalize it actually, into something similar to this format:

                          CREATE TABLE [dbo].[Example](
                          [Site] [nvarchar](50) COLLATE Latin1_General_CI_AS NOT NULL,
                          [Accident] [nvarchar] NOT NULL,
                          [Occations] [int] NOT NULL,
                          ) ON [PRIMARY]

                          Either in the query or a temp table, but preferably it should be stored normalized in the database After that you can make the pivot the normal way

                          "When did ignorance become a point of view" - Dilbert

                          M Offline
                          M Offline
                          mark_w_
                          wrote on last edited by
                          #13

                          Thanks, you were right :) I now have it working.

                          J 1 Reply Last reply
                          0
                          • M mark_w_

                            Thanks, you were right :) I now have it working.

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

                            You should consider normalizing the table. It would make it a lot easier in the future to add functionality. Consider this:

                            CREATE TABLE Incidents (
                            Site varchar
                            IncidentType varchar,
                            IncidentDate Date,
                            Incidentinfo varchar,
                            ...
                            )

                            Then just make your pivot on Select site,incidenttype,count(*) as incidentcount from incidents You may also exchange site and incidenttype for IDs referencing tables holding info on sites and incidenttypes. If you add a site to your organisation you simply add a row in a table.

                            "When did ignorance become a point of view" - Dilbert

                            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