我需要将子查询生成的单个列表连接为父查询生成的表的最后一列。
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%不正确。
答案 0 :(得分:2)
看起来您希望总计数与Grouped计数一致。只需在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以及您可以
的总数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
答案 1 :(得分: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