2

我正在运行 SQL 2008,我的任务是创建一个将“供应商名称”和某些信息组合在一起的报告,此外还按会计年度及其季度对其进行分组。

除了我们的财政开始不是在 1 月而是在 11 月之外,它逐年上升。我有一个名为 'Expected Arrival Date' 的日期字段,格式为 'yyyy-mm-dd hh:mm;ss.mmm' 我不确定如何将其分组为关联:

A.从​​前一年 11 月开始的财政年度到 10 月结束的剩余财政年度(例如 2012 财政年度 = 2011 年 11 月开始 - 2012 年 10 月结束

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
4

3 回答 3

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
;
于 2012-12-13T16:53:13.087 回答
3

Richard Deeming 的 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

见 SqlFiddle

于 2014-01-17T17:38:27.523 回答
1

对于那些有兴趣查询 FY 的人从 7 月开始

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
于 2016-10-30T03:46:52.073 回答