-1

承認済み、キャンセル済み、非表示、フラグ付きのテーブルの最後にある小計を取得しようとしています...グループ化を試みましたが、エラーがスローされました。他の方法や簡単な方法はありますか?

Select * From (
SELECT 
c.ComID, ISNULL(c.Client,'-') AS Driver_Name, GROUPING(c.MTNum) WHILE 1 THEN 'TOTAL' ELSE ISNULL(c.MTNum,'-'), ISNULL(Accepted,0) AS Accepted, ISNULL(Cancelled,0) AS Cancelled, ISNULL(NoShow,0) AS No_Show, ISNULL(Flagged,0) AS Flagged
FROM
(SELECT 
    HistoryLTD.CompanyID, HistoryLTD.Client, HistoryLTD.MTNum
FROM
    HistoryLTD
GROUP BY HistoryLTD.Client, HistoryLTD.CompanyID, HistoryLTD.MTNum) c
    LEFT JOIN
(SELECT 
    HistoryLTD.MTNum, count(HistoryLTD.MTNum) AS Cancelled
FROM
    HistoryLTD
WHERE
    HistoryLTD.Notes LIKE '%Cancelled%' AND TimeAssigned > '2011-08-28' AND TimeAssigned < '2011-08-30'
GROUP BY HistoryLTD.MTNum) c1 ON c.MTNum = c1.MTNum
    LEFT JOIN
(SELECT 
    HistoryLTD.MTNum, count(HistoryLTD.MTNum) AS NoShow
FROM
    HistoryLTD
WHERE
    HistoryLTD.Notes LIKE '%No Show%' AND TimeAssigned > '2011-08-28' AND TimeAssigned < '2011-08-30'
GROUP BY HistoryLTD.MTNum) c2 ON c.MTNum = c2.MTNum
    LEFT JOIN
(SELECT 
    HistoryLTD.MTNum, count(HistoryLTD.MTNum) AS Flagged
FROM
    HistoryLTD
WHERE
    HistoryLTD.Notes LIKE '%Flagged%' AND TimeAssigned > '2011-08-28' AND TimeAssigned < '2011-08-30'
GROUP BY HistoryLTD.MTNum) c3 ON c.MTNum = c3.MTNum
LEFT JOIN
(SELECT 
    HistoryLTD.MTNum, count(HistoryLTD.MTNum) AS Accepted
FROM
    HistoryLTD
where 
    Notes Not LIKE '%Cancelled%'
    OR Notes Not LIKE '%No Show%'
    OR Notes Not LIKE '%Flagged%'
    AND TimeAssigned > '2011-08-28' AND TimeAssigned < '2011-08-30'
GROUP BY HistoryLTD.MTNum) c4 ON c.MTNum = c4.MTNum ) firstQuery
UNION
Select Null, Null, 'Total', SUM(Accepted), SUM(Cancelled), SUM(No_Show), SUM(Flagged)
  FROM ???

探している結果...

ComID   Client_Name mtNum   acc canc    noshow  Flgd
CTBV    Brian       3834    19  0   5   1
CTBV    Central     3863    5   0   0   5
CTBV    Charles     3815    25  0   2   5
CTBV    Chris       3842    14  0   0   0
CTBV    Chris       3864    17  0   1   0
CTBV    Cory        3829    15  0   1   1
               TOTAL        95  0   9   12   <-- This total at bottom
4

1 に答える 1

1
  1. UNION を使用して 2 つのクエリを結合します。最初のクエリはグループごとの集計を表示し、2 番目のクエリは合計を表示します

  2. 合計が一番下になるようにするには、次のような式で並べ替えます。

    NULL の場合は ComID、それ以外の場合は 'ZZZZZZ'、それ以外の場合は ComID で並べ替えます end

于 2012-08-30T17:47:21.670 に答える