SQL Server - 值乘以join

时间:2017-07-07 02:24:03

标签: sql sql-server

对这个有点麻烦......

由于左连接进一步向下,所以求和的分组列TotalStoreVisitTime正在相乘。我尝试过子查询但无法成功实现。我与之交谈的其他人尝试使用游标等,但似乎过于复杂。

SELECT t1.StaffName                 AS "Staff Name" 
       ,MIN(t1.ManagerName)         AS "Manager" 
       ,t1.Name                     AS "Store" 
       ,t1.StartDate                AS "Date" 
       ,SUM(t1.TotalStoreVisitTime) AS "Total Store Time" -- this value gets multiplied by the LEFT JOIN further down.
       ,SUM(t2.TotalClientTime)     AS "Total Client Time"  
FROM   t1 
       LEFT JOIN t2 
              ON t1.StaffName = 
                 t2.StaffName 
                 AND t1.Name = t2.Store 
                 AND t1.StartDate = 
                     t2.StartDate 
WHERE  t1.StartDate >= DATEADD(DAY, -7, GETDATE()) 
GROUP  BY t1.StaffName 
          ,t1.Name
          ,t1.StartDate 
ORDER  BY t1.StartDate DESC; 

输出如下:

| Staff Name |  Manager   |   Store    |   Date    | Total Store Time | Total Client Time |
|------------|------------|------------|-----------|------------------|-------------------|
| John Smith | Jane Smith | Test Store | 6/07/2017 |        25.899996 |          4.033332 |

所需的输出是这样的(即总存储时间只是将t1中的行相加而不是乘以t2中的行数):

| Staff Name |  Manager   |   Store    |   Date    | Total Store Time | Total Client Time |
|------------|------------|------------|-----------|------------------|-------------------|
| John Smith | Jane Smith | Test Store | 6/07/2017 |         4.316666 |          4.033332 |

2 个答案:

答案 0 :(得分:0)

您需要在join之前汇总。我认为这可能有用:

SELECT t1.StaffName                 AS "Staff Name" ,
       t1.Manager                  AS "Manager", 
       t1.Name                     AS "Store" ,
       t1.StartDate                AS "Date" ,
       t1.tot AS "Total Store Time", -- this value gets multiplied by the LEFT JOIN further down.
       SUM(t2.TotalClientTime)     AS "Total Client Time"  
FROM (SELECT t1.StaffName, t1.Name, t1.StartDate,
             SUM(t1.TotalStoreVisitTime) as tot,
             MIN(t1.ManagerName) as manager
      FROM t1
      WHERE t1.StartDate >= DATEADD(DAY, -7, GETDATE()) 
     ) t1 LEFT JOIN
     t2
     ON t1.StaffName = t2.StaffName AND
        t1.Name = t2.Store AND
        t1.StartDate = t2.StartDate 
GROUP BY t1.StaffName, t1.Name, t1.StartDate 
ORDER BY t1.StartDate DESC; 

答案 1 :(得分:0)

子查询很好:

SELECT t1.StaffName     AS "Staff Name" 
   ,MIN(t1.ManagerName) AS "Manager" 
   ,t1.Name             AS "Store" 
   ,t1.StartDate        AS "Date" 
   ,(SELECT SUM(t3.TotalStoreVisitTime)
     FROM TableA t3
     WHERE t1.StaffName = t3.StaffName 
       AND t1.Name = t3.Name 
       AND t1.StartDate = t3.StartDate) AS "Total Store Time" -- compare with primary key
   ,SUM(t2.TotalClientTime)     AS "Total Client Time"
FROM TableA  t1
LEFT JOIN TableB t2
ON t1.StaffName =t2.StaffName
AND t1.Name = t2.Store
AND t1.StartDate = t2.StartDate
WHERE t1.StartDate >= DATEADD(DAY, -7, GETDATE())
GROUP BY t1.StaffName, t1.Name, t1.StartDate
ORDER BY t1.StartDate DESC;