0

我需要将子查询生成的单列表连接为父查询生成的表的最后一列。

SELECT tn.teamtext, sum(case when CONVERT(smalldatetime,dModLast,101) BETWEEN '2012-03-01' AND '2012-03-10' then 1 else 0 end) AS cnt,
    (SELECT sum(case when CONVERT(smalldatetime,dModLast,101) BETWEEN '2012-03-01' AND '2012-03-10' then 1 else 0 end)
      FROM teamnames AS tn 
        JOIN caseaudit AS ca
        ON tn.teamID = ca.referteamID2
      WHERE ca.referteamid1 <> ca.referteamid2
      AND isactive = 1
      AND groupid = 18
      AND accountid = 2) AS cnt2
  FROM teamnames AS tn 
    JOIN caseaudit AS ca
    ON tn.teamID = ca.referteamID2
  WHERE ca.referteamid1 <= 0
  AND ca.referteamid1 <> ca.referteamid2
  AND isactive = 1
  AND groupid = 18
  AND accountid = 2
  GROUP BY tn.teamtext
  ORDER BY tn.teamtext

给我:

4H BOSS                   55    430
4H BSG                     0    430
4H SALES AND MKTG          0    430
ACCOUNTS RECEIVABLE        0    430
ASSET MANAGEMENT           9    430
AUDIT                      0    430
BOSS                       4    430
CORPORATE BSG              0    430
CUSTOMER SUPPORT          87    430
NETWORK ENGINEERING       11    430
PRODUCTION ENGINEERING   116    430
PRODUCTION OPERATIONS      0    430
SECURITY                   2    430
SNFAL PRODUCT TEAM         0    430
VOICE SERVICES            18    430
XEROX                      4    430

“430”是 100% 不正确的。

4

2 回答 2

2

看起来您希望总计数与分组计数一起。只需在 FROM CLAUSE 和 CROSS JOIN 中使用内联查询即可。

SELECT tn.teamtext, 
       Sum(CASE 
             WHEN CONVERT(smalldatetime, dModLast, 101) BETWEEN 
                  '2012-03-01' AND '2012-03-10' THEN 1 
             ELSE 0 
           END) AS cnt, 
       t.TotalCnt 
FROM   teamnames AS tn 
       JOIN caseaudit AS ca 
         ON tn.teamID = ca.referteamID2 
       CROSS JOIN (SELECT Sum(CASE 
                                WHEN CONVERT(smalldatetime, dModLast, 101) 
                                     BETWEEN 
                                     '2012-03-01' AND '2012-03-10' THEN 1 
                                ELSE 0 
                              END) AS TotalCnt 
                   FROM   teamnames AS tn 
                          JOIN caseaudit AS ca 
                            ON tn.teamID = ca.referteamID2 
                   WHERE  ca.referteamid1 <> ca.referteamid2 
                          AND isactive = 1 
                          AND groupid = 18 
                          AND accountid = 2) t 
WHERE  ca.referteamid1 <= 0 
       AND ca.referteamid1 <> ca.referteamid2 
       AND isactive = 1 
       AND groupid = 18 
       AND accountid = 2 
GROUP  BY tn.teamtext , t.TotalCnt 
ORDER  BY tn.teamtext 

简化演示

当然,如果您想要的只是每个团队文本的 TeamText 以及您可以的总数

  • 删除组
  • 添加不同的
  • 对每个团队文本使用 SUM(Case ..) OVER(Parition by teamtext)
  • 使用 SUM(Case ..) OVER(Parition by null) 作为总计
  • 移动ca.referteamid1 <= 0到第一个 SUM.. OVER


 SELECT DISTINCT tn.teamtext, 
       Sum(CASE 
             WHEN CONVERT(smalldatetime, dModLast, 101) BETWEEN '2012-03-01' AND '2012-03-10' AND ca.referteamid1 <= 0 THEN 1 
             ELSE 0 
           END) OVER (PARTITION BY tn.teamtext) AS cnt, 
       Sum(CASE 
             WHEN CONVERT(smalldatetime, dModLast, 101) BETWEEN '2012-03-01' AND '2012-03-10'  THEN 1 
             ELSE 0 
           END) OVER (PARTITION BY NULL) AS Totalcnt

FROM   teamnames AS tn 
       JOIN caseaudit AS ca 
         ON tn.teamID = ca.referteamID2 
WHERE 
        ca.referteamid1 <> ca.referteamid2 
       AND isactive = 1 
       AND groupid = 18 
       AND accountid = 2 
ORDER  BY tn.teamtext 

简化演示

于 2012-06-05T21:27:10.170 回答
1

这有效:

SELECT a.teamtext, a.cnt, b.cnt2
FROM

(SELECT tn.teamtext, tn.teamid, sum(case when CONVERT(smalldatetime,dModLast,101) BETWEEN '2012-03-01' AND '2012-03-10' then 1 else 0 end) AS cnt
 FROM teamnames as tn
    LEFT OUTER JOIN caseaudit AS ca
    ON tn.teamID = ca.referteamID2
 WHERE ca.referteamid1 <= 0 AND ca.referteamid1 <> ca.referteamid2 AND isactive = 1 AND groupid = 18 AND accountid = 2
 GROUP BY tn.teamtext, tn.teamid) AS a

LEFT OUTER JOIN

 (SELECT tn.teamtext, tn.teamid, sum(case when CONVERT(smalldatetime,dModLast,101) BETWEEN '2012-03-01' AND '2012-03-10' then 1 else 0 end) AS cnt2
  FROM teamnames AS tn 
    LEFT OUTER JOIN caseaudit AS ca
    ON tn.teamID = ca.referteamID2
  WHERE ca.referteamid1 <> ca.referteamid2 AND isactive = 1 AND groupid = 18 AND accountid = 2
  GROUP BY tn.teamtext, tn.teamid) AS b
ON a.teamid = b.teamid
ORDER BY a.teamtext
于 2012-06-05T21:50:05.567 回答