2012-02-26 77 views
-1

我要總結一下我的模式和結果,我需要:連接表並獲得COUNT

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 

結果,我需要:

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

換句話說,每天,我需要得到每個狀態的總數。

我這樣做:

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' 

它的工作,現在,但我認爲這可能是一個有點草率。

這裏是我的全部怪物查詢此:

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 

我真的不認爲我應該做UNION ALL,而是構建一個更聰明的方式查詢。你能幫我簡化查詢,這樣我就不需要做UNION ALL了嗎?

回答

3

怎麼樣?

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

如果你有比在表A的2個狀態越多,你可以添加一個where子句來過濾。

我不認爲你可以運行從union路程。記錄位於不同的表格中,您需要從兩個表格中獲取它們。

而且它是一個union不是union all。後者將刪除重複項,並將採取更多的執行。我們不needit因爲有我寫的查詢,沒有重複......我不能答應你的問題粘貼巨大的東西,任何東西:P

+0

好主意!!!!!!現在我唯一想做的事情就是試着讓最後一個UNION ALL從懸而未決。任何想法:) :) – 2012-02-26 19:53:24

+1

我蒙蒂是這個,這是兩個不同的表 - 最簡單的(也可能是最快)的方式做到這一點是使用'UNION ALL'。這正是命令的目的。 – 2012-02-26 19:57:55

1

我的想法是你的工會在一個簡單的CTE來源,然後有一個「怪物」查詢:

; with cte as (--union all for source rows without joins and clauses 
    select * from A 
    union all 
    select *,'pending' from B 
) 
-- "monster" query without the union 
select [day] 
, [status] 
, statusCount=COUNT(*) 
from cte 
group by [day], [status] 
order by [day], [status]