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

グループ化されたカウントと一緒に合計カウントが必要なようです。FROMCLAUSEでインラインクエリを使用してCROSSJOINするだけです。

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(ケース..)OVER(チームテキストによるパーティション)を使用します
  • 総計にはSUM(Case ..)OVER(Parition by null)を使用します
  • ca.referteamid1 <= 0最初のSUMに移動します。


 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 に答える