如何加入兩個sql語句

[英]How to join two sql statements


I have two queries which return correct results. One of them is:

我有兩個查詢返回正確的結果。其中之一是:

select distinct epr.DepartmentCode 
from [dbo].[EmployeePayRates] epr 
join [dbo].[EmployeeTimecards] et on epr.EmployeeID = et.EmployeeID 
where StoreID = 89

It checks all the distinct departments employees ever checked in in the store 89. Query returns:

它檢查在商店89中簽入的所有不同部門員工。查詢返回:

DepartmentCode
1
2
5

Second query is:

第二個查詢是:

SELECT DISTINCT 
    DepartmentCode, count(DISTINCT EmployeeID) 
FROM [dbo].[EmployeeTimecards] 
WHERE EmployeeTimecardDate = '2013-11-25' 
  AND StoreID = 89 
GROUP BY 
   DepartmentCode

It returns the number of employees in each department today at the store 89. Table looks like:

它返回商店89中每個部門的員工數量。表格如下:

DepartmentCode          EmployeeCount
1                       17
5                       7

And I'm trying to make it look like:

而我正試圖讓它看起來像:

DepartmentCode          EmployeeCount
1                       17
2                       0
5                       7

I will really appreciate if somebody will show me how to join these queries. Thanks in advance.

如果有人會告訴我如何加入這些查詢,我將非常感激。提前致謝。

2 个解决方案

#1


3  

You can move your first query into a subquery, then LEFT JOIN back to your table, and perform the same count:

您可以將第一個查詢移動到子查詢中,然后將LEFT JOIN移回到您的表中,並執行相同的計數:

WITH DepartmentCodes AS
(   SELECT  DISTINCT epr.DepartmentCode 
    FROM    [dbo].[EmployeePayRates] epr 
            JOIN [dbo].[EmployeeTimecards] et 
                ON epr.EmployeeID = et.EmployeeID 
    WHERE   StoreID = 89
)
SELECT  dc.DepartmentCode,
        EmployeeCount = COUNT(DISTINCT c.EmployeeID)
FROM    DepartmentCodes dc
        LEFT JOIN [dbo].[EmployeeTimecards]  c
            ON dc.DepartmentCode = c.DepartmentCode
            AND EmployeeTimecardDate = '20131125' 
            AND StoreID = 89 
GROUP BY dc.DepartmentCode;

As an aside I changed your string literal date to the culture invariant format yyyyMMdd because even the ISO Standard yyyy-MM-dd can be misinterpreted as yyyy-dd-MM in some regions. Although not the main point of the article Aaron Bertrand covers this formatting issue in his article mis-handling date / range queries

另外,我將字符串文字日期更改為文化不變格式yyyyMMdd,因為即使是ISO標准yyyy-MM-dd也可能在某些地區被誤解為yyyy-dd-MM。雖然不是文章的主要內容Aaron Bertrand在他的文章錯誤處理日期/范圍查詢中涵蓋了這種格式問題

#2


0  

I choose to use a Common Table expression to join the two queries. Check out my CTE article on my blog - http://craftydba.com/?p=1234.

我選擇使用Common Table表達式來連接兩個查詢。在我的博客上查看我的CTE文章 - http://craftydba.com/?p=1234。

--
-- Join cteDeptCode 2 cteEmpPerDept
--
;
with cteDeptCode (DeptCode) as
(
select distinct epr.DepartmentCode 
from [dbo].[EmployeePayRates] epr 
join [dbo].[EmployeeTimecards] et 
on epr.EmployeeID = et.EmployeeID 
where StoreID = 89
),
cteEmpPerDept(DeptCode, EmpCnt) as
(
SELECT DISTINCT 
DepartmentCode, count(DISTINCT EmployeeID) 
FROM [dbo].[EmployeeTimecards] 
WHERE 
EmployeeTimecardDate = '2013-11-25' AND StoreID = 89 
GROUP BY DepartmentCode
)
select d.DeptCode, a.EmpCnt 
from cteDeptCode d 
left join cteEmpPerDept e 
on d.DeptCode = e.DeptCode

注意!

本站翻译的文章,版权归属于本站,未经许可禁止转摘,转摘请注明本文地址:https://www.itdaan.com/blog/2013/11/25/7256193781dd1cfe7c1395dcee75ea06.html



 
粤ICP备14056181号  © 2014-2021 ITdaan.com