MS Access / SQL Server JOIN返回左表中的所有值,如果右侧缺少则返回零

问题描述 投票:2回答:2

首先道歉,因为我认为标题可能使这个问题听起来比它需要的要困难得多。

基本上,我有两张桌子。从基本的角度来说,它是一个员工表和一个“工作完成”表。我正在尝试创建一个查询,它以摘要行格式返回所有工作人员以及他们完成的工作总和。

两个表:

STAFF_TABLE

+------------+-------------+----------------+--------------+
| TM_StaffID | TM_TeamName | TM_TeamManager | TM_StaffName |
+------------+-------------+----------------+--------------+
|          1 | HA11        | David A        | Paul A       |
|          2 | HA11        | David A        | John A       |
|          3 | HA11        | David A        | Simon A      |
|          4 | HA11        | David A        | Peter A      |
+------------+-------------+----------------+--------------+

工作表

+-------------+------------+----------+-------------+------------+---------------+-----------+
| PS_TeamName | PS_WorkID  | PS_Staff | PS_WorkType |  PS_Date   | PS_WeekEnding | PS_Points |
+-------------+------------+----------+-------------+------------+---------------+-----------+
| HA11        | 2066944422 |        1 | Dev         | 02/08/2018 | 05/08/2018    |         1 |
| HA11        | 1869145859 |        1 | * Misc.     | 02/08/2018 | 05/08/2018    |      0.75 |
| HA11        | 2063035829 |        3 | Plan        | 01/08/2018 | 05/08/2018    |         1 |
| HA11        | 2036440149 |        3 | * Misc.     | 02/08/2018 | 05/08/2018    |       0.5 |
| HA11        | 2063023753 |        3 | Patching    | 03/08/2018 | 05/08/2018    |      0.25 |
| HA11        | 2012358108 |        4 | Plan        | 03/08/2018 | 05/08/2018    |      0.25 |
| HA11        | 2074311499 |        4 | Dev         | 30/07/2018 | 05/08/2018    |         1 |
| HA11        | 2075770157 |        4 | Patching    | 01/08/2018 | 05/08/2018    |      0.75 |
| HA11        | 2059475039 |        4 | Patching    | 03/08/2018 | 05/08/2018    |      0.75 |
| HA11        | 2062057110 |        4 | Plan        | 30/07/2018 | 05/08/2018    |      0.25 |
| HA11        | 2043715055 |        4 | * Misc.     | 02/08/2018 | 05/08/2018    |      0.25 |
+-------------+------------+----------+-------------+------------+---------------+-----------+

我写的有效的查询在这里:

SELECT 
    a.TM_StaffName AS [Staff], Nz(SUM(p.PS_Points), 0) AS [Total]
FROM 
    STAFF_TABLE AS a
LEFT JOIN 
    WORK_TABLE AS p ON (((a.TM_StaffID = p.PS_Staff) 
                    AND cdate(ps.PS_Date) >= #2018-07-30#) 
                    AND cdate(ps.PS_Date) <= #2018-08-05#)
WHERE 
    a.TM_TeamName = 'HA11'
GROUP BY 
    a.TM_StaffName;

问题是,转移到SQL Server,CDate不被识别为函数并且无法通过查询。删除CDate括号然后说“不支持JOIN功能”。我也尝试在日期周围保留#而不是',但这会返回零。

这似乎是一个奇怪的怪癖但是从列中删除CDate会将查询打破为MS Access中不受支持的连接。

从本质上讲,是否有更好的方法来编写此查询,以便显示左表中的所有人员名称,如果右表中没有匹配的记录,它会显示零?

写一个有效而简单的方法会导致下面的表格在左边返回3个名字和3个总数。但是PS_Staff'2'也应该显示并显示0而不是根本不显示。

+---------+-------+
|  Staff  | Total |
+---------+-------+
| Paul A  |  1.75 |
| Simon A |  1.75 |
| Peter A |  3.25 |
+---------+-------+

(Need John A - 0 to show in here)

如何实现此操作以使用SQL Server并将查询放在MS Access VBA中?

谢谢

sql sql-server ms-access access-vba ms-access-2010
2个回答
1
投票

使用查询在sql中使用查询创建视图,然后链接视图以访问db。总的来说,聚合已经完成了

CREATE VIEW dbo.Data
AS
SELECT 
a.TM_TeamName as [Staff], 
a.TM_StaffName,
isnull(Sum(p.PS_Points),0) AS [Total]
FROM @STAFF_TABLE as a
LEFT JOIN @WORK_TABLE as p ON a.TM_StaffID = p.PS_Staff
AND p.PS_Date >= '2018-07-30' AND p.PS_Date <= '2018-08-05'
 WHERE a.TM_TeamName = 'HA11'
GROUP BY a.TM_TeamName ,a.TM_StaffName

0
投票

试试这个(这是为sql server)

DECLARE @STAFF_TABLE TABLE (TM_StaffID INT, TM_TeamName VARCHAR(100), TM_TeamManager VARCHAR(100), TM_StaffName VARCHAR(100))
INSERT INTO @STAFF_TABLE VALUES
 (1,'HA11','David A' ,'Paul A'),
(2,'HA11','David A' ,'John A'),
(3,'HA11','David A','Simon A'),
(4,'HA11','David A' ,'Peter A');
DECLARE @WORK_TABLE TABLE 
(PS_TeamName VARCHAR(100),PS_WorkID INT, PS_Staff INT,PS_WorkType VARCHAR(100), PS_Date DATE,  PS_WeekEnding DATE, PS_Points DECIMAL(9,2))
INSERT INTO @WORK_TABLE VALUES 
('HA11',2066944422,1,'Dev','2018-08-02',  '2018-08-05',   1),
('HA11',1869145859,1,'* Misc','2018-08-02',  '2018-08-05', 0.75),
('HA11',2063035829,3,'Plan','2018-08-01',  '2018-08-05', 1),
('HA11',2036440149,3,'* Misc','2018-08-02',  '2018-08-05', 0.5),
('HA11',2063023753,3,'Patching','2018-08-03',  '2018-08-05', 0.25),
('HA11',2012358108,4,'Plan','2018-08-03',  '2018-08-05', 0.25),
('HA11',2074311499,4,'Dev','2018-07-30',  '2018-08-05',1),
('HA11',2075770157,4,'Patching','2018-08-01',  '2018-08-05',0.75),
('HA11',2059475039,4,'Patching','2018-08-03',  '2018-08-05',  0.75),
('HA11',2062057110,4,'Plan','2018-07-30',  '2018-08-05',0.25),
('HA11',2043715055,4,'* Misc.','2018-08-02',  '2018-08-05', 0.25);


SELECT 
a.TM_TeamName as [Staff], 
a.TM_StaffName,
isnull(Sum(p.PS_Points),0) AS [Total]
FROM @STAFF_TABLE as a
LEFT JOIN @WORK_TABLE as p ON a.TM_StaffID = p.PS_Staff
AND p.PS_Date >= '2018-07-30' AND p.PS_Date <= '2018-08-05'
 WHERE a.TM_TeamName = 'HA11'
GROUP BY a.TM_TeamName ,a.TM_StaffName

产量

Staff   TM_StaffName    Total
HA11    John A  0.00
HA11    Paul A  1.75
HA11    Peter A 3.25
HA11    Simon A 1.75
© www.soinside.com 2019 - 2024. All rights reserved.