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. A Grouping

A Grouping

Scheduled Pinned Locked Moved Database
databasehelpquestion
6 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.
  • R Offline
    R Offline
    RadioButton
    wrote on last edited by
    #1

    Hello, And thanks in advance for the help. I am using this query to group exams and count: SELECT BPG.[GroupName], COUNT(A.[ProcedureID]) AS [Count] FROM [DB].[dbo].[Accessions] A INNER JOIN [DB].[dbo].[table1] BP ON A.[ProcedureID] = BP.[ProcedureID] INNER JOIN [DB].[dbo].[table2] BPG ON BP.[ProcedureGroup] = BPG.[ProcedureGroupID] INNER JOIN [DB].[dbo].[table3] O ON A.[OrderID] = O.[OrderID] INNER JOIN [DB].[dbo].[table4] F ON O.[OrderingFacilityID] = F.[FacilityID] WHERE --XRAY,US,PICC,EKG (BPG.[GroupName] IN ('XRAY','EKG','US','PICC')) AND (F.[Name] LIKE '%' + @Facility + '%') AND (A.[ExamDate] >= CONVERT(DATETIME, @StartDate + @Year) AND A.[ExamDate] < CONVERT(DATETIME, @EndDate + @Year) AND (A.[Status] != 'Cancelled' AND A.[Status] != 'ADMIN CANCEL' AND A.[Status] != 'Cancelled - Dry Run')) GROUP BY BPG.[GroupName] Let's say the query only returns 'XRAY' and 'EKG' with a count. How can I also return that 'US' and 'PICC' have zero count. :doh: Thanks for the help.

    B C D 3 Replies Last reply
    0
    • R RadioButton

      Hello, And thanks in advance for the help. I am using this query to group exams and count: SELECT BPG.[GroupName], COUNT(A.[ProcedureID]) AS [Count] FROM [DB].[dbo].[Accessions] A INNER JOIN [DB].[dbo].[table1] BP ON A.[ProcedureID] = BP.[ProcedureID] INNER JOIN [DB].[dbo].[table2] BPG ON BP.[ProcedureGroup] = BPG.[ProcedureGroupID] INNER JOIN [DB].[dbo].[table3] O ON A.[OrderID] = O.[OrderID] INNER JOIN [DB].[dbo].[table4] F ON O.[OrderingFacilityID] = F.[FacilityID] WHERE --XRAY,US,PICC,EKG (BPG.[GroupName] IN ('XRAY','EKG','US','PICC')) AND (F.[Name] LIKE '%' + @Facility + '%') AND (A.[ExamDate] >= CONVERT(DATETIME, @StartDate + @Year) AND A.[ExamDate] < CONVERT(DATETIME, @EndDate + @Year) AND (A.[Status] != 'Cancelled' AND A.[Status] != 'ADMIN CANCEL' AND A.[Status] != 'Cancelled - Dry Run')) GROUP BY BPG.[GroupName] Let's say the query only returns 'XRAY' and 'EKG' with a count. How can I also return that 'US' and 'PICC' have zero count. :doh: Thanks for the help.

      B Offline
      B Offline
      Blue_Boy
      wrote on last edited by
      #2

      Try to change join condition and see if you get result which you need.


      I Love T-SQL "Don't torture yourself,let the life to do it for you." If my post helps you kindly save my time by voting my post. www.cacttus.com

      R 1 Reply Last reply
      0
      • B Blue_Boy

        Try to change join condition and see if you get result which you need.


        I Love T-SQL "Don't torture yourself,let the life to do it for you." If my post helps you kindly save my time by voting my post. www.cacttus.com

        R Offline
        R Offline
        RadioButton
        wrote on last edited by
        #3

        Thank you. I am not sure what to change and that is why I asked for help.

        1 Reply Last reply
        0
        • R RadioButton

          Hello, And thanks in advance for the help. I am using this query to group exams and count: SELECT BPG.[GroupName], COUNT(A.[ProcedureID]) AS [Count] FROM [DB].[dbo].[Accessions] A INNER JOIN [DB].[dbo].[table1] BP ON A.[ProcedureID] = BP.[ProcedureID] INNER JOIN [DB].[dbo].[table2] BPG ON BP.[ProcedureGroup] = BPG.[ProcedureGroupID] INNER JOIN [DB].[dbo].[table3] O ON A.[OrderID] = O.[OrderID] INNER JOIN [DB].[dbo].[table4] F ON O.[OrderingFacilityID] = F.[FacilityID] WHERE --XRAY,US,PICC,EKG (BPG.[GroupName] IN ('XRAY','EKG','US','PICC')) AND (F.[Name] LIKE '%' + @Facility + '%') AND (A.[ExamDate] >= CONVERT(DATETIME, @StartDate + @Year) AND A.[ExamDate] < CONVERT(DATETIME, @EndDate + @Year) AND (A.[Status] != 'Cancelled' AND A.[Status] != 'ADMIN CANCEL' AND A.[Status] != 'Cancelled - Dry Run')) GROUP BY BPG.[GroupName] Let's say the query only returns 'XRAY' and 'EKG' with a count. How can I also return that 'US' and 'PICC' have zero count. :doh: Thanks for the help.

          C Offline
          C Offline
          Chris Meech
          wrote on last edited by
          #4

          RadioButton wrote:

          INNER JOIN [DB].[dbo].[table1] BP ON A.[ProcedureID] = BP.[ProcedureID]

          I think this should be LEFT OUTER JOIN instead. Also you should change the count to be

          count(nvl(a.ProcedureID,0))

          so the NULL values will be counted as zero. :)

          Chris Meech I am Canadian. [heard in a local bar] In theory there is no difference between theory and practice. In practice there is. [Yogi Berra] posting about Crystal Reports here is like discussing gay marriage on a catholic church’s website.[Nishant Sivakumar]

          R 1 Reply Last reply
          0
          • C Chris Meech

            RadioButton wrote:

            INNER JOIN [DB].[dbo].[table1] BP ON A.[ProcedureID] = BP.[ProcedureID]

            I think this should be LEFT OUTER JOIN instead. Also you should change the count to be

            count(nvl(a.ProcedureID,0))

            so the NULL values will be counted as zero. :)

            Chris Meech I am Canadian. [heard in a local bar] In theory there is no difference between theory and practice. In practice there is. [Yogi Berra] posting about Crystal Reports here is like discussing gay marriage on a catholic church’s website.[Nishant Sivakumar]

            R Offline
            R Offline
            RadioButton
            wrote on last edited by
            #5

            I tried your suggestion and that did not change anything. I am using MS SQL 2008 and the NVL function does not appear to be avaible so I tried the ISNULL function. I believe it does the same thing. Thanks again.

            1 Reply Last reply
            0
            • R RadioButton

              Hello, And thanks in advance for the help. I am using this query to group exams and count: SELECT BPG.[GroupName], COUNT(A.[ProcedureID]) AS [Count] FROM [DB].[dbo].[Accessions] A INNER JOIN [DB].[dbo].[table1] BP ON A.[ProcedureID] = BP.[ProcedureID] INNER JOIN [DB].[dbo].[table2] BPG ON BP.[ProcedureGroup] = BPG.[ProcedureGroupID] INNER JOIN [DB].[dbo].[table3] O ON A.[OrderID] = O.[OrderID] INNER JOIN [DB].[dbo].[table4] F ON O.[OrderingFacilityID] = F.[FacilityID] WHERE --XRAY,US,PICC,EKG (BPG.[GroupName] IN ('XRAY','EKG','US','PICC')) AND (F.[Name] LIKE '%' + @Facility + '%') AND (A.[ExamDate] >= CONVERT(DATETIME, @StartDate + @Year) AND A.[ExamDate] < CONVERT(DATETIME, @EndDate + @Year) AND (A.[Status] != 'Cancelled' AND A.[Status] != 'ADMIN CANCEL' AND A.[Status] != 'Cancelled - Dry Run')) GROUP BY BPG.[GroupName] Let's say the query only returns 'XRAY' and 'EKG' with a count. How can I also return that 'US' and 'PICC' have zero count. :doh: Thanks for the help.

              D Offline
              D Offline
              dbrenth
              wrote on last edited by
              #6

              Connect to the BPG table and all of its inner joins first. Then do a LEFT OUTER JOIN to your Accessions (A) table and it's joins. This will give you all of the group names and the 0's if there is nothing for that group.

              SELECT BPG.[GroupName], COUNT(A.[ProcedureID]) AS [Count]
              FROM
              [DB].[dbo].[table2] BPG

              LEFT OUTER JOIN

              ([DB].[dbo].[Accessions] A
              INNER JOIN [DB].[dbo].[table1] BP ON A.[ProcedureID] = BP.[ProcedureID]
              INNER JOIN [DB].[dbo].[table3] O ON A.[OrderID] = O.[OrderID]
              INNER JOIN [DB].[dbo].[table4] F ON O.[OrderingFacilityID] = F.[FacilityID]

              )

              ON BP.[ProcedureGroup] = BPG.[ProcedureGroupID]

              WHERE
              .....

              GROUP BY BPG.[GroupName]

              Brent

              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