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. Complex Select Query

Complex Select Query

Scheduled Pinned Locked Moved Database
database
8 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.
  • G Offline
    G Offline
    Girish481
    wrote on last edited by
    #1

    There is a table name "ResultData" which following data : Rollno Result SchoolCode 1 First 1 2 First 1 3 Second 2 4 First 1 5 Fail 2 6 Third 2 7 Second 1 8 Fail 3 9 Fail 1 10 Second 2 I want the output like this : (School code wise count of Result) SchoolCode First Second Third Fail Total 1 3 1 0 1 5 2 0 2 1 1 4 3 0 0 0 1 1 Please write a single select query to get output. Thanks in advance Girish Kumar Sharma

    D T 2 Replies Last reply
    0
    • G Girish481

      There is a table name "ResultData" which following data : Rollno Result SchoolCode 1 First 1 2 First 1 3 Second 2 4 First 1 5 Fail 2 6 Third 2 7 Second 1 8 Fail 3 9 Fail 1 10 Second 2 I want the output like this : (School code wise count of Result) SchoolCode First Second Third Fail Total 1 3 1 0 1 5 2 0 2 1 1 4 3 0 0 0 1 1 Please write a single select query to get output. Thanks in advance Girish Kumar Sharma

      D Offline
      D Offline
      DQNOK
      wrote on last edited by
      #2

      I believe you want a "crosstab query". Apart from Microsoft products, I don't know if other vendors have it built-in. I don't believe it's part of the SQL standard... Here is what Microsoft Access help says about cross tab queries. " Syntax TRANSFORM aggfunction selectstatement PIVOT pivotfield [IN (value1[, value2[, ...]])] The TRANSFORM statement has these parts: Part Description aggfunction An SQL aggregate function that operates on the selected data. selectstatement A SELECT statement. pivotfield The field or expression you want to use to create column headings in the query's result set. value1, value2 Fixed values used to create column headings. "

      G 1 Reply Last reply
      0
      • D DQNOK

        I believe you want a "crosstab query". Apart from Microsoft products, I don't know if other vendors have it built-in. I don't believe it's part of the SQL standard... Here is what Microsoft Access help says about cross tab queries. " Syntax TRANSFORM aggfunction selectstatement PIVOT pivotfield [IN (value1[, value2[, ...]])] The TRANSFORM statement has these parts: Part Description aggfunction An SQL aggregate function that operates on the selected data. selectstatement A SELECT statement. pivotfield The field or expression you want to use to create column headings in the query's result set. value1, value2 Fixed values used to create column headings. "

        G Offline
        G Offline
        Girish481
        wrote on last edited by
        #3

        So, is it not possible to write a select statement in SQL Sever ?

        D 2 Replies Last reply
        0
        • G Girish481

          So, is it not possible to write a select statement in SQL Sever ?

          D Offline
          D Offline
          DQNOK
          wrote on last edited by
          #4

          Well, I thought it was included in SQL Server... Looks like didn't show-up until 2005. Here is one article that may help. http://builder.com.com/5100-6388_14-6143761.html[^] Once upon a time I wrote a lot of Perl code to dynamically do the same thing that the crosstab query does, but when I discovered crosstabs, I never went back. I don't know how to do it using CASE statements. sorry I can't help more. Maybe someone else can...

          J 1 Reply Last reply
          0
          • G Girish481

            So, is it not possible to write a select statement in SQL Sever ?

            D Offline
            D Offline
            DQNOK
            wrote on last edited by
            #5

            Also check out http://www.oreillynet.com/pub/a/network/2004/12/17/crosstab.html?CMP=ILC-FV7511446129&ATT=2411[^] for a good explanation, and (possibly) a pre-written procedure you (might) be able to use.

            G 1 Reply Last reply
            0
            • D DQNOK

              Well, I thought it was included in SQL Server... Looks like didn't show-up until 2005. Here is one article that may help. http://builder.com.com/5100-6388_14-6143761.html[^] Once upon a time I wrote a lot of Perl code to dynamically do the same thing that the crosstab query does, but when I discovered crosstabs, I never went back. I don't know how to do it using CASE statements. sorry I can't help more. Maybe someone else can...

              J Offline
              J Offline
              Jerry Hammond
              wrote on last edited by
              #6

              You're correct, PIVOTs don't show up until 2005 in MSSQL

              “Some have an idea that the reason we in this country discard things so readily is because we have so much. The facts are exactly opposite - the reason we have so much is simply because we discard things so readily. We replace the old in return for something that will serve us better.”--Alfred P. Sloan

              1 Reply Last reply
              0
              • D DQNOK

                Also check out http://www.oreillynet.com/pub/a/network/2004/12/17/crosstab.html?CMP=ILC-FV7511446129&ATT=2411[^] for a good explanation, and (possibly) a pre-written procedure you (might) be able to use.

                G Offline
                G Offline
                Girish481
                wrote on last edited by
                #7

                Thanks to all of you and finally i used this query : SELECT SCHCODE,COUNT(CASE WHEN RESULT = 'First' THEN Result END) AS First,COUNT(CASE WHEN Result = 'Second' THEN Result END) AS Second,COUNT(CASE WHEN Result = 'Third' THEN Result END) AS Third,COUNT(CASE WHEN Result = 'Fail' THEN Result END) AS Fail,count(*) as Total FROM resultdata GROUP BY schcode

                1 Reply Last reply
                0
                • G Girish481

                  There is a table name "ResultData" which following data : Rollno Result SchoolCode 1 First 1 2 First 1 3 Second 2 4 First 1 5 Fail 2 6 Third 2 7 Second 1 8 Fail 3 9 Fail 1 10 Second 2 I want the output like this : (School code wise count of Result) SchoolCode First Second Third Fail Total 1 3 1 0 1 5 2 0 2 1 1 4 3 0 0 0 1 1 Please write a single select query to get output. Thanks in advance Girish Kumar Sharma

                  T Offline
                  T Offline
                  The_Server
                  wrote on last edited by
                  #8

                  I'm not much of an SQL query expert, But I think that I have a solution SELECT SchoolCode, COUNT(First) As TotalFirst, COUNT(Second) As TotalSecond, COUNT(Third) As TotalThird, COUNT(Fail) As TotalFail, (COUNT(First) + COUNT(Second) + COUNT(Third) +COUNT(Fail)) As TotalTotal FROM ResultData GROUP BY (SchoolCode) I havent tested it. Post a reply to let every body know if it works.

                  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