私はSQL2008を実行しています。「ベンダー名」と特定の情報をグループ化したレポートを作成し、さらに会計年度と四半期ごとにグループ化するという任務を負っています。
会計開始が1月ではなく、11月であることを除けば、年ごとにグループ化は順調に進んでいます。'yyyy-mm-dd hh:mm;ss.mmm'の形式の'ExpectedArrivalDate'という日付フィールドがあります。グループ化して関連付ける方法がわかりません。
A.前年の11月から10月までの会計年度(例:2012年度= 2011年11月上旬〜2012年10月下旬)
B.会計四半期11月1日〜1月31日、2月1日〜4月30日、5月1日〜7月31日、8月1日〜10月31日
これが私がすでに持っているコードです
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