更改日期并按财政月末分组

发布于 2024-07-25 16:07:40 字数 1527 浏览 2 评论 0原文

我创建了下表(通过 UNION ALL):

ID        Date         Hour   Weight    Fiscal
AAA       1/27/2009     0      10         0
AAA       1/30/2009     0      20         0
AAA       2/14/2009     0      10         0
AAA       2/18/2009     0      20         0
AAA       2/27/2009     0      20         0
AAA       2/28/2009     0      20         0
AAA       1/14/2009     10     0          0
AAA       2/14/2009     20     0          0
AAA       2/16/2009     10     0          0
AAA       2/25/2009     10     0          0
AAA       2/26/2009     10     0          0
AAA       3/3/2009          20     0          0
NULL          0                 0      0          1/28/2009
NULL          0                 0      0          2/27/2009
NULL          0                 0      0          3/29/2009

我想检索:

    ID      Date        Hour    Weight
    AAA     1/28/2009   10  10
    AAA     2/27/2009   50  50
    AAA     3/29/2009   20  20

它应该首先将 DATE 列转换为下一个财政月末 FISCAL (例如 1/27/2007 -> 1/28/2009, 1/30/2009 -> 2/27/2009 等),然后按该值分组。

我能够使用:

    SELECT  ID
    ,       left(CONVERT(VARCHAR(10)
    ,       date, 111),7) as Date
    ,       round(sum(Hours),0) as Hours
    ,       round(sum(Weight),0) as Weight
    FROM    ...
    GROUP   BY ID
    ,       left(CONVERT(VARCHAR(10), date, 111),7)
    ORDER   by ID ASC
    ,       left(CONVERT(VARCHAR(10), date, 111),7) ASC

但这只是按日历日期分组,而不是根据我在FISCAL列中的财政月结束。

I have created the following table (via UNION ALL):

ID        Date         Hour   Weight    Fiscal
AAA       1/27/2009     0      10         0
AAA       1/30/2009     0      20         0
AAA       2/14/2009     0      10         0
AAA       2/18/2009     0      20         0
AAA       2/27/2009     0      20         0
AAA       2/28/2009     0      20         0
AAA       1/14/2009     10     0          0
AAA       2/14/2009     20     0          0
AAA       2/16/2009     10     0          0
AAA       2/25/2009     10     0          0
AAA       2/26/2009     10     0          0
AAA       3/3/2009          20     0          0
NULL          0                 0      0          1/28/2009
NULL          0                 0      0          2/27/2009
NULL          0                 0      0          3/29/2009

And I would like to retrieve:

    ID      Date        Hour    Weight
    AAA     1/28/2009   10  10
    AAA     2/27/2009   50  50
    AAA     3/29/2009   20  20

It should first convert the DATE column to the next Fiscal month end FISCAL (for example 1/27/2007 -> 1/28/2009, 1/30/2009 -> 2/27/2009, etc.) and then group by that value.

I was able to use:

    SELECT  ID
    ,       left(CONVERT(VARCHAR(10)
    ,       date, 111),7) as Date
    ,       round(sum(Hours),0) as Hours
    ,       round(sum(Weight),0) as Weight
    FROM    ...
    GROUP   BY ID
    ,       left(CONVERT(VARCHAR(10), date, 111),7)
    ORDER   by ID ASC
    ,       left(CONVERT(VARCHAR(10), date, 111),7) ASC

But that just groups by calendar dates, not according to the Fiscal month ends I have in the FISCAL column.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

客…行舟 2024-08-01 16:07:41

您不应将会计日期合并到表中。 但是对于您现在拥有的表,您可以首先创建一个包含财务“期间”的表(使用WITH,因此不需要特殊权限)并将该表与您的主表连接起来。

我称你的桌子为SO。 我还必须将您的列重命名为 cHour 和 CDate,因为在 Oracle 中至少 Date 是保留字。
您很可能直接从源表创建表“main”和表“fiscal”以加快速度:

with fiscal_part as (
            select fiscal
            from   SO
            where  fiscal is not null)
,    fiscal as (
            select fb.fiscal         fiscal_begin
            ,      min(fe.fiscal)    fiscal_end
            from   fiscal_part fb right join 
                   fiscal_part fe
                on fe.fiscal > fb.fiscal
            group by fb.fiscal)
,    main as (
            select *
            from   SO
            where  fiscal is null)
select main.ID
,      fiscal.fiscal_end
,      sum(main.cHour)
,      sum(main.weight)
from   main
join   fiscal on main.cdate >= coalesce(fiscal.fiscal_begin, main.cdate)
             and main.cdate <  fiscal.fiscal_end
group by main.ID
,      fiscal.fiscal_end
order by fiscal.fiscal_end

另请注意,您的示例数据有错误; 您上次经期的体重应为 40(日期为 2/27 和 2/28 的行)。

You should not union the fiscal dates into your table. But with the table as you have now, you could first create a table (using WITH, so no special priviliges are necessary) with the fiscal 'periods' and join this table with your main table.

I called your Table SO. I also had to rename your columns to cHour and CDate because in Oracle at least Date is a reserved word.
You could very probably create the table 'main' and the table 'fiscal' directly from your source tables to speed things up:

with fiscal_part as (
            select fiscal
            from   SO
            where  fiscal is not null)
,    fiscal as (
            select fb.fiscal         fiscal_begin
            ,      min(fe.fiscal)    fiscal_end
            from   fiscal_part fb right join 
                   fiscal_part fe
                on fe.fiscal > fb.fiscal
            group by fb.fiscal)
,    main as (
            select *
            from   SO
            where  fiscal is null)
select main.ID
,      fiscal.fiscal_end
,      sum(main.cHour)
,      sum(main.weight)
from   main
join   fiscal on main.cdate >= coalesce(fiscal.fiscal_begin, main.cdate)
             and main.cdate <  fiscal.fiscal_end
group by main.ID
,      fiscal.fiscal_end
order by fiscal.fiscal_end

Also note that your sample data has an error; the weight for your last period should be 40 (the rows with dates 2/27 and 2/28).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文