2012-07-11 55 views
0

我加入了某些標準的一些表。我想知道是否可以顯示從連接中排除的那些記錄的計數?加入和計數在相同的查詢沒有雙字段計算

我的查詢:

SELECT... 

FROM   dbo.QuickLabDump 
      ... 

      left outer join hermes.lom.dbo.lom_specimen Lom 
       on QuickLabDump.[specimen id] = lom.specimen 
       LEFT outer join SalesDWH.dbo.Isomers Isomers 
       on QuickLabDump.[specimen id] = Isomers.[accession id] 



WHERE  lom.specimen is null 
       and isomers.[accession id] is null... 

,你可以看到我有兩個左外連接。

除了此查詢的結果之外,我還希望返回排除的hermes.lom.dbo.lom_specimenSalesDWH.dbo.Isomers中的那些記錄的計數。

如何獲得排除字段的計數?

這裏是完整的查詢:

USE [SalesDWH] 
GO 

/****** Object: View [dbo].[TurnAround] Script Date: 07/10/2012 20:46:40 ******/ 
SET ANSI_NULLS ON 
GO 

SET QUOTED_IDENTIFIER ON 
GO 






ALTER VIEW [dbo].[TurnAround] 
AS 
SELECT  dbo.QuickLabDump.[Specimen ID], DATEPART(yyyy, dbo.QuickLabDump.[Date Entered]) AS [Year Entered], 
DATEPART(yyyy, dbo.QuickLabDump.[Date Completed]) AS [year completed], 

CAST(CAST(dbo.QuickLabDump.[Date Entered] AS DATE) 
         AS DATETIME) + CAST(dbo.QuickLabDump.[Time Entered] AS TIME) AS [DateTime Entered], CAST(CAST(dbo.QuickLabDump.[Date Completed] AS DATE) AS DATETIME) 
         + CAST(dbo.QuickLabDump.[Time Completed] AS TIME) AS [DateTime Completed], dbo.QuickLabDump.[Practice Code] AS [QL Practuce Code], 
         dbo.QLMLISMapping.[MLIS Practice ID], DATEPART(m, dbo.QuickLabDump.[Date Entered]) AS [Month Entered], 
         DATEPART(m, dbo.QuickLabDump.[Date Completed]) AS [Month Completed], 
         ROUND(CAST(DATEDIFF(mi, 
         CAST(CAST(dbo.QuickLabDump.[Date Entered] AS DATE) AS DATETIME) + CAST(dbo.QuickLabDump.[Time Entered] AS TIME), 
         CAST(CAST(dbo.QuickLabDump.[Date Completed] AS DATE) AS DATETIME) + CAST(dbo.QuickLabDump.[Time Completed] AS TIME)) AS FLOAT)/60, 0) AS TAT, 
         ROUND(CAST(DATEDIFF(mi, CAST(CAST(dbo.QuickLabDump.[Date Entered] AS DATE) AS DATETIME) + CAST('9:00AM' AS TIME), 


         CAST(CAST(dbo.QuickLabDump.[Date Completed] AS DATE) AS DATETIME) 
         + CAST(dbo.QuickLabDump.[Time Completed] AS TIME)) AS float)/60, 0) 

         AS [Hours TurnAround Since 9AM], TAT9AMStateAdjusted=     



         ROUND(CAST(DATEDIFF(mi, CAST(CAST(dbo.QuickLabDump.[Date Entered] AS DATE) AS DATETIME) 
         + CAST('9:00AM' AS TIME),            
         CAST(CAST(dbo.QuickLabDump.[Date Completed] AS DATE) AS DATETIME) 
         + CAST(dbo.QuickLabDump.[Time Completed] AS TIME)) AS float)/60, 0)       


         +dbo.adjustedtime.timedifference 
         ,dbo.QuickLabDump.[Order Count], CONVERT(VARCHAR(8), DATEADD(D, - (1 * DATEPART(dw, dbo.QuickLabDump.[Date Entered])) + 6, 
         dbo.QuickLabDump.[Date Entered]), 1) AS [Week Ending] 
FROM   dbo.QuickLabDump 
      INNER JOIN dbo.QLMLISMapping 
      ON dbo.QuickLabDump.[Practice Code] = dbo.QLMLISMapping.[Quicklab ID] 
      left outer join dbo.AccountState 
      on dbo.QuickLabDump.[Practice Code]=dbo.AccountState.[Account] 
      left outer join dbo.AdjustedTime 
      ON dbo.AccountState.[state]=dbo.adjustedtime.[state] 

      left outer join hermes.lom.dbo.lom_specimen Lom 
       on QuickLabDump.[specimen id] = lom.specimen 
       LEFT outer join SalesDWH.dbo.Isomers Isomers 
       on QuickLabDump.[specimen id] = Isomers.[accession id] 



WHERE  lom.specimen is null 
       and isomers.[accession id] is null 

       and (dbo.QuickLabDump.[Practice Code] NOT LIKE 'test%') 
       AND (dbo.QuickLabDump.[Specimen ID] NOT LIKE 'of%') 
       AND (dbo.QuickLabDump.Outcome <> 'rejected') 
GROUP BY dbo.QuickLabDump.[Specimen ID], dbo.QuickLabDump.[Date Entered], dbo.QuickLabDump.[Time Entered], dbo.QuickLabDump.[Date Completed], 
         dbo.QuickLabDump.[Time Completed], dbo.QuickLabDump.[Practice Name], dbo.QuickLabDump.[Practice Code], dbo.QuickLabDump.[Order Count], 
         dbo.QLMLISMapping.[MLIS Practice ID],dbo.adjustedtime.timedifference 
HAVING  (dbo.QuickLabDump.[Order Count] = MAX(dbo.QuickLabDump.[Order Count])) OR 
         (dbo.QuickLabDump.[Order Count] IS NULL) 






GO 
+0

你選擇了哪些列? – Bohemian 2012-07-11 04:22:26

+0

即時消息不知道如何可以幫助,但包括整個查詢 – 2012-07-11 04:23:53

+0

你可以用一個組,但所有這些列... – Bohemian 2012-07-11 04:28:52

回答

1

你在找這個?

SELECT  ..., 
      (
       SELECT COUNt(*) FROM hermes.lom.dbo.lom_specimen WHERE specimen = lom.specimen 
      ) AS ColCount 

FROM   dbo.QuickLabDump  
      left outer join hermes.lom.dbo.lom_specimen Lom 
       on QuickLabDump.[specimen id] = lom.specimen 
       LEFT outer join SalesDWH.dbo.Isomers Isomers 
       on QuickLabDump.[specimen id] = Isomers.[accession id] 
WHERE  lom.specimen is null 
       and isomers.[accession id] is null 
1

運行沒有WHERE子句相同的查詢和使用全外連接將讓你在選擇宇宙中的所有行。

+0

非常感謝您的回答,請您詳細說明一下嗎? – 2012-07-11 15:04:07