2012-12-13 154 views
1

我正在運行SQL 2008,我的任務是創建一個報告,將「供應商名稱」和某些信息分組在一起,並按財年和他們的季度分組。SQL如何按財政季度和年份與日期字段

除了我們的財政開始不是在一月份,而是在十一月,它逐年順利地進行了分組。我有一個叫做'預期到達日期'的日期字段,格式爲'yyyy-mm-dd hh:mm; ss.mmm'我不知道如何將它分組到關聯:

A.財年開始與11月份之前,在10月結束的財年的剩餘部分(例如2012財年年初= 2011年11月 - 10月底2012

B.財政季度Nov1- Jan31,Feb1 - Apr30,MAY1 -July31,Aug1-Oct31

這裏是我已經有的代碼

SELECT 
    YEAR([Expected Arrival Date])as 'Year', 
    MONTH([Expected Arrival Date]) as 'Month', 
    [Vendor Name], 
    (SUM(CASE WHEN ([Days Past Due] = 0) THEN [Qty Received] ELSE 0 END)/SUM([Qty Received])) * 100 AS 'On-Time %', 
    (SUM(CASE WHEN ([Days Past Due] < 0) THEN [Qty Received] ELSE 0 END)/SUM([Qty Received])) * 100 AS 'Early %', 
    (SUM(CASE WHEN ([Days Past Due] > 0) THEN [Qty Received] ELSE 0 END)/SUM([Qty Received])) * 100 AS 'Late %', 
    SUM([Qty Received]) aS 'TOTAL' 

FROM data_view 
GROUP BY YEAR([Expected Arrival Date]),MONTH([Expected Arrival Date]),[Vendor Name] 
ORDER by YEAR([Expected Arrival Date]),MONTH([Expected Arrival Date]) 

如果您有任何有用的指針來創建一個很好的查詢它也將不勝感激。

編輯** 的data_view的結構如下

SELECT  [Receipt #], [Vendor Code], [Vendor Name], [PO Applied], [PO Line #], [Item Code], [Item Description], [Expected Arrival Date], [Receiving Date], [Receiving Date Filter], 
         [Qty Received], [Qty Ordered], [% Received], [PO Date], [Receipt User], DATEDIFF(dd, CAST(CAST([Expected Arrival Date] AS CHAR) AS DATETIME), 
         CAST(CAST([Receiving Date] AS CHAR) AS DATETIME)) AS [Days Past Due] 
FROM   (SELECT  T1.RCPNUMBER AS [Receipt #], T1.VDCODE AS [Vendor Code], T1.VDNAME AS [Vendor Name], T4.PONUMBER AS [PO Applied], 
                T3.DETAILNUM AS [PO Line #], T2.ITEMNO AS [Item Code], T2.ITEMDESC AS [Item Description], 
                CASE WHEN T3.EXPARRIVAL = 0 THEN CASE WHEN T4.EXPARRIVAL = 0 THEN CAST(CAST(T4.DATE AS CHAR) AS DATETIME) 
                ELSE CAST(CAST(T4.EXPARRIVAL AS CHAR) AS DATETIME) END ELSE CAST(CAST(T3.EXPARRIVAL AS CHAR) AS DATETIME) 
                END AS [Expected Arrival Date], CAST(CAST(T1.DATE AS CHAR) AS DATETIME) AS [Receiving Date], CAST(CAST(T1.DATE AS CHAR) AS DATETIME) 
                AS [Receiving Date Filter], T2.RQRECEIVED AS [Qty Received], T3.OQORDERED AS [Qty Ordered], 
                ROUND(CASE WHEN T2.OQORDERED <> 0 THEN (T2.RQRECEIVED/T2.OQORDERED) * 100 ELSE 100 END, 2) AS [% Received], 
                CAST(CAST(T4.DATE AS CHAR) AS DATETIME) AS [PO Date], T5.ENTEREDBY AS [Receipt User] 
          FROM   canada.dbo.PORCPH1 AS T1 INNER JOIN 
                canada.dbo.PORCPH2 AS T5 ON T1.RCPHSEQ = T5.RCPHSEQ INNER JOIN 
                canada.dbo.PORCPL AS T2 ON T2.RCPHSEQ = T1.RCPHSEQ INNER JOIN 
                canada.dbo.POPORL AS T3 ON T3.PORHSEQ = T2.PORHSEQ AND T3.PORLSEQ = T2.PORLSEQ INNER JOIN 
                canada.dbo.POPORH1 AS T4 ON T4.PORHSEQ = T3.PORHSEQ) AS tblReceips 
+0

你能解釋一下'data_view'結構? –

+0

嗨哈姆雷特感謝您的時間,data_view是很久以前由其他人創建的視圖,並通過兩個數據庫服務器從多個表中提取數據。目前他們使用Excel來訪問視圖,併發送了數千個條目。 這個新視圖旨在儘可能地分組儘可能多的信息(大約只有600個條目),我也將視圖放在編輯中,我不希望更改該視圖,因爲其他人仍然使用它。 – jnoel10

回答

4

像這樣的東西應該工作:

SELECT 
    YEAR([Expected Arrival Date]) + CASE 
     WHEN MONTH([Expected Arrival Date]) IN (11, 12) THEN 1 
     ELSE 0 
    END As FiscalYear, 
    CASE 
     WHEN MONTH([Expected Arrival Date]) IN (11, 12, 1) THEN 1 
     WHEN MONTH([Expected Arrival Date]) IN (2, 3, 4) THEN 2 
     WHEN MONTH([Expected Arrival Date]) IN (5, 6, 7) THEN 3 
     ELSE 4 
    END As FiscalQuarter, 
    ... 
GROUP BY 
    YEAR([Expected Arrival Date]) + CASE 
     WHEN MONTH([Expected Arrival Date]) IN (11, 12) THEN 1 
     ELSE 0 
    END, 
    CASE 
     WHEN MONTH([Expected Arrival Date]) IN (11, 12, 1) THEN 1 
     WHEN MONTH([Expected Arrival Date]) IN (2, 3, 4) THEN 2 
     WHEN MONTH([Expected Arrival Date]) IN (5, 6, 7) THEN 3 
     ELSE 4 
    END, 
    [Vendor Name] 
ORDER BY 
    FiscalYear, 
    FiscalQuarter 
; 
+0

謝謝Richard迴應我的問題, 我是否也可以按供應商名稱進行GROUP?我可以在哪裏放置這個? – jnoel10

+0

@ jnoel10:是的,您應該可以將'[Vendor Name]'添加到'GROUP BY'子句中。我用一個例子更新了我的答案。 –

+0

如果可以的話,我會多+1,它的工作完美。再次感謝! – jnoel10

2

理查德推定的SQL完美的作品在MySQL,但我卻高興不起來與查詢的大小有關,所以我使用基本概念創建使用內置MySQL IF()函數的更小的查詢:

SELECT 
    YEAR(date), 
    MONTH(date), 
    YEAR(date) + IF(MONTH(date)>10, 1, 0) as 'FiscalYear', 
    IF(MONTH(date)>10, 1, CEIL((MONTH(date)+2)/3)) as 'FiscalQuarter' 
FROM dates 

See SqlFiddle

1

對於那些誰感興趣的查詢財政年度從七月開始

SELECT 
    YEAR(date), 
    MONTH(date), 
    YEAR(date) + IF(MONTH(date)>6, 1, 0) as FiscalYear, 
    IF(MONTH(date)>6, FLOOR((MONTH(date)-4)/3), CEIL((MONTH(date)+6)/3)) as FiscalQuarter 
FROM dates 
+0

看到這一點,我 - 非SQL專家^^ - 瞭解在哪裏更改變量以獲取選擇的開始月份。 :) – Brainfeeder

相關問題