Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7555179
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T11:32:15+00:00 2026-05-30T11:32:15+00:00

I am going to summarize my schema and the result that I need: Table

  • 0

I am going to summarize my schema and the result that I need:

Table A(released and rejected):
day, specimenid, status,bunch of other unneeded fields....
1,AA123, released
1,AA132, released
2,BB234, rejected
3,BB444, released
3,AA444, rejected

Table B(pending):
day, specimenid, bunch of other unneeded columns
1, BB333
1, H234
2, C333
3, F333

Result that I need:

day, count, status
1, 2, released
1, 2, pending
2, 1, rejected
2, 1, pending
3, 1, released
3, 1, rejected
3, 1, pending

another words, for every day, i need to get the total count for each status.

I am doing this:

select day, count(specimenid) from tableA where status='released', 'released'

union all

select day, count(specimenid) from tableA where status='rejected', 'rejected'

union all

select day, count(specimenid) from tableB, 'pending'

It’s working for now but I think it might be a little sloppy.

Here’s my full monster query for this:

WITH cte(rejected) AS (SELECT DISTINCT [Specimen ID]
                                                 FROM         dbo.QuickLabDump
                                                 WHERE     (Outcome = 'REJECTED') AND ([Specimen ID] IS NOT NULL))

    SELECT     CONVERT(VARCHAR(8), a.[Date Entered], 1) AS [Full Date], DATEPART(yy, a.[Date Entered]) AS [Year Entered], LEFT(DATENAME(MONTH, a.[Date Entered]), 3) 
                            AS [Month Entered], DATEPART(dd, a.[Date Entered]) AS [Day Entered], CASE WHEN DATEPART(WEEKDAY, [DATE entered]) 
                            = 1 THEN 'Sun' WHEN DATEPART(WEEKDAY, [DATE entered]) = 2 THEN 'Mon' WHEN DATEPART(WEEKDAY, [DATE entered]) 
                            = 3 THEN 'Tus' WHEN DATEPART(WEEKDAY, [DATE entered]) = 4 THEN 'Wed' WHEN DATEPART(WEEKDAY, [DATE entered]) 
                            = 5 THEN 'Thu' WHEN DATEPART(WEEKDAY, [DATE entered]) = 6 THEN 'Fri' WHEN DATEPART(WEEKDAY, [DATE entered]) = 7 THEN 'Sat' END AS DOW, 
                            CONVERT(VARCHAR(8), DATEADD(D, - (1 * DATEPART(dw, a.[Date Entered])) + 6, a.[Date Entered]), 1) AS [Week Ending], COUNT(DISTINCT a.[Specimen ID]) 
                            AS CountAccns, c.SALESREP AS [Sales Rep], c.NPI AS MLNPI, e.NPIb AS IMSNPI, e.SpecialtyPrimaryCodeb AS [IMS Specialty Primary Code],
                            sm1.specialtydesc as [Specialty Primary Description],
                            e.SpecialtySecondaryCodeb AS [IMS Specialty Secondary Code], 
                            sm2.specialtydesc as [Specialty Secondary Description],                            
                            e.SpecialtyTertiaryCodeb AS [IMS Specialty Tertiary Code], 
                            sm3.specialtydesc as [Specialty Tertiary Description],
                            e.ProfessionalID1b AS [IMS Professional ID 1], a.[Requesting Physician] AS Physician, a.[Practice Code], b.[MLIS Practice ID] AS [MLIS Code], a.[Practice Name], 
                            c.DATEESTABLISHED AS [Date Established], c.PRACTICEADDRESS1 AS Address, c.PRACTICEADDRESS2 AS Address2, c.PRACTICECITY AS City, 
                            c.PRACTICESTATE AS State, d.[MLIS Status] AS Status, MAX(a.[Order Count]) AS [order count],
                            'Released' as [Release Status]
     FROM         dbo.QuickLabDump AS a LEFT OUTER JOIN
                            dbo.qlmlismapping AS b ON b.[Quicklab ID] = a.[Practice Code] LEFT OUTER JOIN
                            dbo.PracticeandPhysician AS c ON a.[Requesting Physician] = c.DOCTORFIRSTNAME + ' ' + c.DOCTORLASTNAME AND 
                            a.[Practice Code] = c.PRACTICECODE LEFT OUTER JOIN
                            dbo.IMSData AS e ON c.NPI = e.NPIb LEFT OUTER JOIN
                            dbo.QLMLISInfo AS d ON b.[MLIS Practice ID] = d.[MLIS Practice ID]
                            left outer JOIN
                                dbo.SpecialtyMapping sm1 ON e.SpecialtyPrimaryCodeb = sm1.specialtyabbrev
                            left outer JOIN
                                dbo.SpecialtyMapping sm2 ON e.SpecialtySecondaryCodeb = sm2.specialtyabbrev
                            left outer JOIN
                                dbo.SpecialtyMapping sm3 ON e.SpecialtyTertiaryCodeb = sm3.specialtyabbrev
     WHERE     (a.[Date Entered] > '20110101') AND (NOT EXISTS
                                (SELECT     1 AS Expr1
                                  FROM          cte AS cte_1
                                  WHERE      (rejected = a.[Specimen ID]))) AND (NOT EXISTS
                                (SELECT     1 AS Expr1
                                  FROM          dbo.PendingSpecimens
                                  WHERE      ([Specimen ID] = a.[Specimen ID])))
     GROUP BY a.[Date Entered], c.SALESREP, c.NPI, e.NPIb, e.SpecialtyPrimaryCodeb, e.SpecialtySecondaryCodeb, e.SpecialtyTertiaryCodeb, e.ProfessionalID1b, 
                            a.[Requesting Physician], a.[Practice Code], b.[MLIS Practice ID], a.[Practice Name], c.DATEESTABLISHED, c.PRACTICEADDRESS1, c.PRACTICEADDRESS2, 
                            c.PRACTICECITY, c.PRACTICESTATE, d.[MLIS Status],sm1.specialtydesc,sm2.specialtydesc,sm3.specialtydesc


                            union all

                            select 
                                CONVERT(VARCHAR(8), a.[Date Entered], 1) AS [Full Date], 
                                DATEPART(yy, a.[Date Entered]) AS [Year Entered], 
                                LEFT(DATENAME(MONTH, a.[Date Entered]), 3) AS [Month Entered], 
                                DATEPART(dd, a.[Date Entered]) AS [Day Entered], 
                                CASE WHEN DATEPART(WEEKDAY, [DATE entered]) 
                                = 1 THEN 'Sun' WHEN DATEPART(WEEKDAY, [DATE entered]) = 2 THEN 'Mon' WHEN DATEPART(WEEKDAY, [DATE entered]) 
                                = 3 THEN 'Tus' WHEN DATEPART(WEEKDAY, [DATE entered]) = 4 THEN 'Wed' WHEN DATEPART(WEEKDAY, [DATE entered]) 
                                = 5 THEN 'Thu' WHEN DATEPART(WEEKDAY, [DATE entered]) = 6 THEN 'Fri' WHEN DATEPART(WEEKDAY, [DATE entered]) = 7 THEN 'Sat' END AS DOW, 
                                CONVERT(VARCHAR(8), DATEADD(D, - (1 * DATEPART(dw, a.[Date Entered])) + 6, a.[Date Entered]), 1) AS [Week Ending], 
                                COUNT(DISTINCT a.[Specimen ID]) AS CountAccns, 
                                null AS [Sales Rep], 
                                null AS MLNPI, 
                                null AS IMSNPI, 
                                null AS [IMS Specialty Primary Code],
                                null as [Specialty Primary Description],
                                null AS [IMS Specialty Secondary Code], 
                                null as [Specialty Secondary Description],  
                                null AS [IMS Specialty Tertiary Code], 
                                null as [Specialty Tertiary Description],
                                null AS [IMS Professional ID 1], 
                                null as Physician, 
                                SUBSTRING(agency,1,charindex(' - ',agency,1)-1) as [Practice Code], 
                                b.[MLIS Practice ID] AS [MLIS Code], 
                                SUBSTRING(agency,charindex(' - ',agency,1)+3,len(agency)) as [Practice Name], 
                                c.DATEESTABLISHED AS [Date Established], 
                                c.PRACTICEADDRESS1 AS Address, 
                                c.PRACTICEADDRESS2 AS Address2, 
                                c.PRACTICECITY AS City, 
                                c.PRACTICESTATE AS State, 
                                d.[MLIS Status] AS Status, 
                                null AS [order count],
                                'Pending' as [Release Status]
   from dbo.pendingspecimens AS a LEFT OUTER JOIN
                            dbo.qlmlismapping AS b ON b.[Quicklab ID] = SUBSTRING(agency,1,charindex(' - ',agency,1))
                             LEFT OUTER JOIN
                            dbo.PracticeandPhysician AS c ON  SUBSTRING(agency,1,charindex(' - ',agency,1)) = c.PRACTICECODE LEFT OUTER JOIN
                            dbo.IMSData AS e ON c.NPI = e.NPIb LEFT OUTER JOIN
                            dbo.QLMLISInfo AS d ON b.[MLIS Practice ID] = d.[MLIS Practice ID]
                         WHERE     (a.[Date Entered] > '20110101') 

                           group by  agency,a.[Date Entered], c.SALESREP, c.NPI, e.NPIb, e.SpecialtyPrimaryCodeb, e.SpecialtySecondaryCodeb, e.SpecialtyTertiaryCodeb, e.ProfessionalID1b, 
                             b.[MLIS Practice ID], c.DATEESTABLISHED, c.PRACTICEADDRESS1, c.PRACTICEADDRESS2, 
                            c.PRACTICECITY, c.PRACTICESTATE, d.[MLIS Status]

                            union all

                            SELECT     CONVERT(VARCHAR(8), a.[Date Entered], 1) AS [Full Date], DATEPART(yy, a.[Date Entered]) AS [Year Entered], LEFT(DATENAME(MONTH, a.[Date Entered]), 3) 
                            AS [Month Entered], DATEPART(dd, a.[Date Entered]) AS [Day Entered], CASE WHEN DATEPART(WEEKDAY, [DATE entered]) 
                            = 1 THEN 'Sun' WHEN DATEPART(WEEKDAY, [DATE entered]) = 2 THEN 'Mon' WHEN DATEPART(WEEKDAY, [DATE entered]) 
                            = 3 THEN 'Tus' WHEN DATEPART(WEEKDAY, [DATE entered]) = 4 THEN 'Wed' WHEN DATEPART(WEEKDAY, [DATE entered]) 
                            = 5 THEN 'Thu' WHEN DATEPART(WEEKDAY, [DATE entered]) = 6 THEN 'Fri' WHEN DATEPART(WEEKDAY, [DATE entered]) = 7 THEN 'Sat' END AS DOW, 
                            CONVERT(VARCHAR(8), DATEADD(D, - (1 * DATEPART(dw, a.[Date Entered])) + 6, a.[Date Entered]), 1) AS [Week Ending], COUNT(DISTINCT a.[Specimen ID]) 
                            AS CountAccns, c.SALESREP AS [Sales Rep], c.NPI AS MLNPI, e.NPIb AS IMSNPI, e.SpecialtyPrimaryCodeb AS [IMS Specialty Primary Code],
                            sm1.specialtydesc as [Specialty Primary Description],
                            e.SpecialtySecondaryCodeb AS [IMS Specialty Secondary Code], 
                            sm2.specialtydesc as [Specialty Secondary Description],                            
                            e.SpecialtyTertiaryCodeb AS [IMS Specialty Tertiary Code], 
                            sm3.specialtydesc as [Specialty Tertiary Description],
                            e.ProfessionalID1b AS [IMS Professional ID 1], a.[Requesting Physician] AS Physician, a.[Practice Code], b.[MLIS Practice ID] AS [MLIS Code], a.[Practice Name], 
                            c.DATEESTABLISHED AS [Date Established], c.PRACTICEADDRESS1 AS Address, c.PRACTICEADDRESS2 AS Address2, c.PRACTICECITY AS City, 
                            c.PRACTICESTATE AS State, d.[MLIS Status] AS Status, MAX(a.[Order Count]) AS [order count],
                            'Rejected' as [Release Status]
     FROM         dbo.QuickLabDump AS a LEFT OUTER JOIN
                            dbo.qlmlismapping AS b ON b.[Quicklab ID] = a.[Practice Code] LEFT OUTER JOIN
                            dbo.PracticeandPhysician AS c ON a.[Requesting Physician] = c.DOCTORFIRSTNAME + ' ' + c.DOCTORLASTNAME AND 
                            a.[Practice Code] = c.PRACTICECODE LEFT OUTER JOIN
                            dbo.IMSData AS e ON c.NPI = e.NPIb LEFT OUTER JOIN
                            dbo.QLMLISInfo AS d ON b.[MLIS Practice ID] = d.[MLIS Practice ID]
                            left outer JOIN
                                dbo.SpecialtyMapping sm1 ON e.SpecialtyPrimaryCodeb = sm1.specialtyabbrev
                            left outer JOIN
                                dbo.SpecialtyMapping sm2 ON e.SpecialtySecondaryCodeb = sm2.specialtyabbrev
                            left outer JOIN
                                dbo.SpecialtyMapping sm3 ON e.SpecialtyTertiaryCodeb = sm3.specialtyabbrev
                                join cte on cte.rejected=a.[Specimen ID]
     WHERE     (a.[Date Entered] > '20110101')
     GROUP BY a.[Date Entered], c.SALESREP, c.NPI, e.NPIb, e.SpecialtyPrimaryCodeb, e.SpecialtySecondaryCodeb, e.SpecialtyTertiaryCodeb, e.ProfessionalID1b, 
                            a.[Requesting Physician], a.[Practice Code], b.[MLIS Practice ID], a.[Practice Name], c.DATEESTABLISHED, c.PRACTICEADDRESS1, c.PRACTICEADDRESS2, 
                            c.PRACTICECITY, c.PRACTICESTATE, d.[MLIS Status],sm1.specialtydesc,sm2.specialtydesc,sm3.specialtydesc

I really don’t think I should be doing UNION ALL, but instead structuring the query in a smarter way. Can you please help me simplify the query so that I dont need to do UNION ALLs?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-30T11:32:16+00:00Added an answer on May 30, 2026 at 11:32 am

    What about?

    select day, count(*) as count, status from TableA
    group by day, status
    union
    select day, count(*) as count, 'pending' from TableB
    group by day
    

    If you have more than those 2 status in TableA you can add a where clause to filter them.

    I don’t think you can run away from the union. The records are in different tables and you need to get them from both of them.

    Also it is a union not a union all. The latter will remove duplicates and will take more to perform. We don’t needit because there are no duplicates in the queries I wrote… I can’t promise anything about the huge stuff you pasted in the question 😛

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a new table I'm going to add to a bunch of other
Going mad now. I have a MVC solution that i've upgraded from MVC 1
Going off of my other question and its pair . I'm looking to grab
I am going to design a schema for a checklist application where different branch
Going from Rails 2.3.x to Rails 3.0.7 requires a change in ERB syntax that
I am going to summarize my problem into the following code snippet. List<int> list
I am going nuts trying to figure out this all day but without success.
I'm just realising after 24 hours of jquery/javascript pain, that maybe i'm going the
Going back to my previous question on OCSP, does anybody know of reliable OCSP
Going through some of my older Delphi projects and upgrading them to D2009, as

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.