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. Need Arrange Query result into another table Header

Need Arrange Query result into another table Header

Scheduled Pinned Locked Moved Database
csharpjavadatabasevisual-studiodesign
11 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.
  • R rrrriiizz

    Heelo All, I want to arrange a result of query into Row header. Example : in one table i have the Names Of Books. I want to arrage each book name as heading in another Table. Select * from BookNames BookNames --------- Java Programming visual studio.net Operating Sytems ORDBMS System analysis and Design System Software I want to arrage the Result as a separate table Like Below JavaProgarmming visual studio.net OperatingSytems ORDBMS --------------- ----------------- ----------------- ------ IS it possible? Please Help me. Thanks RIZ.......

    A Offline
    A Offline
    Ashfield
    wrote on last edited by
    #2

    It is to a certain extent, but what would happen if you had say 1000 booknames? You would not want 1000 columns - and most versions of RDBMS wouldn't allow that many any way.

    Bob Ashfield Consultants Ltd

    R 1 Reply Last reply
    0
    • R rrrriiizz

      Heelo All, I want to arrange a result of query into Row header. Example : in one table i have the Names Of Books. I want to arrage each book name as heading in another Table. Select * from BookNames BookNames --------- Java Programming visual studio.net Operating Sytems ORDBMS System analysis and Design System Software I want to arrage the Result as a separate table Like Below JavaProgarmming visual studio.net OperatingSytems ORDBMS --------------- ----------------- ----------------- ------ IS it possible? Please Help me. Thanks RIZ.......

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

      take a look at Pivot. Use that with a dynamic sql to construct your book colums. In this script the @List would bhe your book titles BEGIN Set @SQL = 'SELECT PortfolioID, Portfolio, ' + @List + char(13) Set @SQL = @SQL + 'FROM (SELECT PortfolioID, Portfolio, A.Element, PFClass ' + char(13) Set @SQL = @SQL + 'FROM vwPFAttribute A ' + char(13) Set @SQL = @SQL + 'INNER JOIN PFTree T ON T.PFClassID = A.PFClassID AND T.TreeID = ' + CONVERT(VARCHAR(20),@TreeID) + char(13) Set @SQL = @SQL + 'WHERE A.PortfolioID NOT IN (SELECT PortfolioID FROM dbo.fn_TreeExcludedPortfolios(' + CONVERT(VARCHAR(20),@TreeID) + ')))P' + char(13) Set @SQL = @SQL + 'Pivot (Max(Element) For PFClass In (' + @List + ')) as Pvt' + char(13) Set @SQL = @SQL + 'Order By Portfolio' Print @SQL END

      Never underestimate the power of human stupidity RAH

      P R 2 Replies Last reply
      0
      • M Mycroft Holmes

        take a look at Pivot. Use that with a dynamic sql to construct your book colums. In this script the @List would bhe your book titles BEGIN Set @SQL = 'SELECT PortfolioID, Portfolio, ' + @List + char(13) Set @SQL = @SQL + 'FROM (SELECT PortfolioID, Portfolio, A.Element, PFClass ' + char(13) Set @SQL = @SQL + 'FROM vwPFAttribute A ' + char(13) Set @SQL = @SQL + 'INNER JOIN PFTree T ON T.PFClassID = A.PFClassID AND T.TreeID = ' + CONVERT(VARCHAR(20),@TreeID) + char(13) Set @SQL = @SQL + 'WHERE A.PortfolioID NOT IN (SELECT PortfolioID FROM dbo.fn_TreeExcludedPortfolios(' + CONVERT(VARCHAR(20),@TreeID) + ')))P' + char(13) Set @SQL = @SQL + 'Pivot (Max(Element) For PFClass In (' + @List + ')) as Pvt' + char(13) Set @SQL = @SQL + 'Order By Portfolio' Print @SQL END

        Never underestimate the power of human stupidity RAH

        P Offline
        P Offline
        Paul Conrad
        wrote on last edited by
        #4

        Mycroft Holmes wrote:

        look at Pivot.

        Seems like alot of questions lately floating around PIVOTs :suss:

        "The clue train passed his station without stopping." - John Simmons / outlaw programmer "Real programmers just throw a bunch of 1s and 0s at the computer to see what sticks" - Pete O'Hanlon

        M 1 Reply Last reply
        0
        • P Paul Conrad

          Mycroft Holmes wrote:

          look at Pivot.

          Seems like alot of questions lately floating around PIVOTs :suss:

          "The clue train passed his station without stopping." - John Simmons / outlaw programmer "Real programmers just throw a bunch of 1s and 0s at the computer to see what sticks" - Pete O'Hanlon

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

          Nasty bloody things until you get the hang of them. Once you get the idea that there are inner and outer queries it starts to fall into place. I used to do a lot of horrible cursor and while selects to get the same result until 2005. I broke down into 3 steps 1 Build your query to the minimum of data you need (inner query) 2 build your column string = @List 3 wrap these in the pivot query

          Never underestimate the power of human stupidity RAH

          1 Reply Last reply
          0
          • M Mycroft Holmes

            take a look at Pivot. Use that with a dynamic sql to construct your book colums. In this script the @List would bhe your book titles BEGIN Set @SQL = 'SELECT PortfolioID, Portfolio, ' + @List + char(13) Set @SQL = @SQL + 'FROM (SELECT PortfolioID, Portfolio, A.Element, PFClass ' + char(13) Set @SQL = @SQL + 'FROM vwPFAttribute A ' + char(13) Set @SQL = @SQL + 'INNER JOIN PFTree T ON T.PFClassID = A.PFClassID AND T.TreeID = ' + CONVERT(VARCHAR(20),@TreeID) + char(13) Set @SQL = @SQL + 'WHERE A.PortfolioID NOT IN (SELECT PortfolioID FROM dbo.fn_TreeExcludedPortfolios(' + CONVERT(VARCHAR(20),@TreeID) + ')))P' + char(13) Set @SQL = @SQL + 'Pivot (Max(Element) For PFClass In (' + @List + ')) as Pvt' + char(13) Set @SQL = @SQL + 'Order By Portfolio' Print @SQL END

            Never underestimate the power of human stupidity RAH

            R Offline
            R Offline
            rrrriiizz
            wrote on last edited by
            #6

            Sir, That query returns a below error in Parse. am using sqlserver 2005. ' Must Declare a Scalar variable @SQL' ' Must declare a Scalar Variable @OList' What can i Do Now? This is sample Program only, hardly i will use only 10 to 15 books only. because this is my mini project. Please help me

            M 1 Reply Last reply
            0
            • R rrrriiizz

              Sir, That query returns a below error in Parse. am using sqlserver 2005. ' Must Declare a Scalar variable @SQL' ' Must declare a Scalar Variable @OList' What can i Do Now? This is sample Program only, hardly i will use only 10 to 15 books only. because this is my mini project. Please help me

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

              Astonishing - you actually thought that query would run right, it was there as an example.

              rrrriiizz wrote:

              What can i Do Now?

              Read the other replies, do some research in BOL on pivot queries, find out what dynamic SQL is. You will not be spoon fed with code, we are here to help you increase your skills and LEARN, not do your job for you. I strongly suggest you get a book in SQL Server and start from there.

              Never underestimate the power of human stupidity RAH

              1 Reply Last reply
              0
              • A Ashfield

                It is to a certain extent, but what would happen if you had say 1000 booknames? You would not want 1000 columns - and most versions of RDBMS wouldn't allow that many any way.

                Bob Ashfield Consultants Ltd

                R Offline
                R Offline
                rrrriiizz
                wrote on last edited by
                #8

                Sir, This is my Mini Project I will only use 10 to 15 book's Only. Once i add a book name in One table, it will automatically add as another tables header. Please help me if there is any way? Thanks Fathima

                1 Reply Last reply
                0
                • R rrrriiizz

                  Heelo All, I want to arrange a result of query into Row header. Example : in one table i have the Names Of Books. I want to arrage each book name as heading in another Table. Select * from BookNames BookNames --------- Java Programming visual studio.net Operating Sytems ORDBMS System analysis and Design System Software I want to arrage the Result as a separate table Like Below JavaProgarmming visual studio.net OperatingSytems ORDBMS --------------- ----------------- ----------------- ------ IS it possible? Please Help me. Thanks RIZ.......

                  Z Offline
                  Z Offline
                  zahedonline
                  wrote on last edited by
                  #9

                  Hi there!!!, I have one idea for your question.. since this is ur small project try to do it in this way... this is just an alternative way.. Use your front end to for creation of tables... As you said this may have hardly 15 to 20 records... Assume that you are using VB.Net as your front end,, then use a for loop to loop through the values underit use table creation script which would greate dynamicly... based on the values. Hope this gives you a sight!!! Cheers, ZAK

                  ZAK

                  R 1 Reply Last reply
                  0
                  • Z zahedonline

                    Hi there!!!, I have one idea for your question.. since this is ur small project try to do it in this way... this is just an alternative way.. Use your front end to for creation of tables... As you said this may have hardly 15 to 20 records... Assume that you are using VB.Net as your front end,, then use a for loop to loop through the values underit use table creation script which would greate dynamicly... based on the values. Hope this gives you a sight!!! Cheers, ZAK

                    ZAK

                    R Offline
                    R Offline
                    rrrriiizz
                    wrote on last edited by
                    #10

                    Thank You So Much, this was working good in my Project. Thanks a lotttttttttt....

                    Z 1 Reply Last reply
                    0
                    • R rrrriiizz

                      Thank You So Much, this was working good in my Project. Thanks a lotttttttttt....

                      Z Offline
                      Z Offline
                      zahedonline
                      wrote on last edited by
                      #11

                      You are welcome dude!!!!!

                      ZAK

                      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