2011-08-06 55 views
5

我有一個包含多個日期字段的表。例如,在缺陷表中,我有以下內容:dateAssigned,dateCompleted,dateResolved。我試圖讓一個彙總缺陷這樣的查詢:將多個日期字段組合的SQL查詢

  | Number Assigned | Number Completed | Number Resolved 
-----------------------------+------------------+----------------- 
    Mar-2011 |  33   |  22  |  33   
    Apr-2011 |  10   |  11  |  22 
    May-2011 |  22   |  66  |  46 

我想出了以下無濟於事:

SELECT year(d.dateAssigned) 
     ,month(d.dateAssigned) 
     ,COUNT(d.dateAssigned) 
     ,COUNT(d.dateCompleted) 
     ,COUNT(d.dateResolved) 
    FROM defect d 
    GROUP BY year(d.dateAssigned), month(d.dateAssigned) 
    ORDER BY year(d.dateAssigned), month(d.dateAssigned) 

這正常工作的總結dateAssigned缺陷,但不適用於其他人。我意識到這可能是由於我按dateAssigned分組的事實,但我不知道如何去做。

任何幫助,將不勝感激。

+0

我沒有看到一個優雅的方式,但你可以將三個不同的子選擇結合在一起,依次分組。希望有人會發布更好的東西。 –

回答

1

這是一種方法。

SELECT YEAR(typedate), 
     MONTH(typedate), 
     SUM(CASE 
      WHEN TYPE = 'assinged' THEN 1 
      ELSE 0 
      END) number_assigned, 
     SUM(CASE 
      WHEN TYPE = 'completed' THEN 1 
      ELSE 0 
      END) number_completed, 
     SUM(CASE 
      WHEN TYPE = 'Resolved' THEN 1 
      ELSE 0 
      END) number_resolved 
FROM (SELECT dateassigned typedate, 
       'assinged' AS TYPE 
     FROM sampledata 
     WHERE dateassigned IS NOT NULL 
     UNION ALL 
     SELECT datecompleted typedate, 
       'completed' AS TYPE 
     FROM sampledata 
     WHERE datecompleted IS NOT NULL 
     UNION ALL 
     SELECT dateresolved typedate, 
       'Resolved' AS TYPE 
     FROM sampledata 
     WHERE dateresolved IS NOT NULL) data 
GROUP BY YEAR(typedate), 
      MONTH(typedate) 
ORDER BY YEAR(typedate), 
      MONTH(typedate) 

子選擇創建兩列typedate並鍵入

這將是這樣的

typedate type 
-------- --------- 
1/1/2011 assinged 
1/1/2011 assinged 
1/1/2011 assinged 
2/1/2011 completed 
2/1/2011 completed 
2/3/2011 Resolved 

然後我用SUM(CASE...轉動數據。如果你使用的是支持它的RDBMS,你可以使用pivot代替

最終輸出看起來是這樣的

     Number_Assigned Number_completed Number_Resolved 
----------- ----------- --------------- ---------------- --------------- 
2011  1   3    0    0 
2011  2   0    2    1 

我將讓你做的年份和月份

的格式
+0

這個伎倆!感謝一堆......我有很多東西要學。 – j0nnyf1ve

0

另一種方法是創建一個表months,其中的字段爲:year, month, firstDate, nextMonthFirstDate,可以在運行中或作爲臨時表或永久表(具有存儲100年和100x12行的表不會佔用太多空間並且可以是方便在其他查詢),並使用這樣的東西(這相當於monthsLEFT JOIN編三次,以表defect):

SELECT 
    (SELECT COUNT(*) 
     FROM defect AS d 
     WHERE m.firstDate <= d.dateAssigned 
     AND d.dateAssigned < m.nextMonthFirstDate 
    ) AS Number_Assigned 
    . (SELECT COUNT(*) 
     FROM defect AS d 
     WHERE m.firstDate <= d.dateCompleted 
     AND d.dateCompleted < m.nextMonthFirstDate 
    ) AS Number_Completed 
    . (SELECT COUNT(*) 
     FROM defect AS d 
     WHERE m.firstDate <= d.dateResolved 
     AND d.dateResolved < m.nextMonthFirstDate 
    ) AS Number_Resolved 
FROM months AS m 
ORDER BY m.year 
     , m.month 
2
SELECT year(defectDate), month(defectDate), SUM(assigned), SUM(resolved), SUM(completed) 
    FROM (
    SELECT d.dateAssigned AS defectDate, 1 AS assigned, 0 AS resolved, 0 AS completed 
     FROM defect d 
    UNION ALL 
    SELECT d.dateCompleted AS defectDate, 0 AS assigned, 1 AS resolved, 0 AS completed 
     FROM defect d 
    UNION ALL 
    SELECT d.dateResolved AS defectDate, 0 AS assigned, 0 AS resolved, 1 AS completed 
     FROM defect d 
) 
    GROUP BY year(defectDate), month(defectDate) 
    ORDER BY year(defectDate), month(defectDate) 
1
SELECT year(d.dateAssigned) 
     ,month(d.dateAssigned) 
     ,COUNT(d.dateAssigned) 
     ,(SELECT count(*) 
     FROM defect as dc 
     WHERE month(dc.dateCompleted) = month(d.dateAssigned) and 
       year(dc.dateCompleted) = year(d.dateAssigned)) 
     ,(SELECT count(*) 
     FROM defect as dr 
     WHERE month(dr.dateResolved) = month(d.dateAssigned) and 
       year(dr.dateResolved) = year(d.dateAssigned)) 
FROM defect d 
GROUP BY year(d.dateAssigned), month(d.dateAssigned) 
-1

如果我們在使用的情況下由羣體這是可能的別名

SELECT CASE 
     WHEN (year(Challan_Date) is null or year(TAX_INVOICE_Date)!=null) THEN year(TAX_INVOICE_Date) 
     WHEN (year(Challan_Date) != null or year(TAX_INVOICE_Date) is null) THEN YEAR(Challan_Date) 
     WHEN (year(Challan_Date) is null or year(TAX_INVOICE_Date)is null) THEN 'NA' 
     WHEN (year(Challan_Date) != null or year(TAX_INVOICE_Date)!= null) THEN year(Challan_Date) 
     ELSE year(TAX_INVOICE_Date) END as Dates_Record, TAX_INVOICE_Date, Challan_Date 
FROM TransactionRecords