SQL 会计日期、平面文件、痛苦

发布于 2024-10-25 20:41:24 字数 617 浏览 1 评论 0原文

使用 sqlite3、python

我有一个蹩脚的遗留文件legacy_sales

item | year | week | mon | tue | wed | thu | fri | sat |sun 
4444   2011     29    10    0     4    15     18    25   30

我有一个很棒的新文件sales(这是上面“mon”条目的示例):

item | units |     date
4444   10       2011-03-14

我在另一个表中有会计年度的开始日期 fiscal

year | start_date
2011   2010-08-30

有了这些信息,实现这一目标的最佳和最有效的方法是什么:

insert into sales from (insert magic here) 

不使用任何 UDF...

有这样的胡言乱语的经验吗?

Using sqlite3, python

I have a crappy legacy file legacy_sales:

item | year | week | mon | tue | wed | thu | fri | sat |sun 
4444   2011     29    10    0     4    15     18    25   30

And I have an awesome new file sales (this is an example of the 'mon' entry from above):

item | units |     date
4444   10       2011-03-14

I have the start date of fiscal years in another table fiscal

year | start_date
2011   2010-08-30

With this information what is the best and most efficient way to accomplish this:

insert into sales from (insert magic here) 

without using any UDFs...

Any experience with such drivel?

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

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

发布评论

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

评论(2

绮筵 2024-11-01 20:41:26
insert into sales from (
    -- here be magic
    select  item, units, DATEADD(day, DayInYear, start_date)
    from    (
        select  item,
                year,
                (week - 1) * 7 + 0 as DayInYear,
                mon as Units
        from    legacy_sales
        union all
        select  item, year, (week - 1) * 7 + 1, tue
        from    legacy_sales
        union all
        select  item, year, (week - 1) * 7 + 2, wed
        from    legacy_sales
        union all
        select  item, year, (week - 1) * 7 + 3, thu
        from    legacy_sales
        union all
        select  item, year, (week - 1) * 7 + 4, fri
        from    legacy_sales
        union all
        select  item, year, (week - 1) * 7 + 5, sat
        from    legacy_sales
        union all
        select  item, year, (week - 1) * 7 + 6, sun
        from    legacy_sales
        ) ls
        inner join fiscal on
            fiscal.year = ls.year
)
insert into sales from (
    -- here be magic
    select  item, units, DATEADD(day, DayInYear, start_date)
    from    (
        select  item,
                year,
                (week - 1) * 7 + 0 as DayInYear,
                mon as Units
        from    legacy_sales
        union all
        select  item, year, (week - 1) * 7 + 1, tue
        from    legacy_sales
        union all
        select  item, year, (week - 1) * 7 + 2, wed
        from    legacy_sales
        union all
        select  item, year, (week - 1) * 7 + 3, thu
        from    legacy_sales
        union all
        select  item, year, (week - 1) * 7 + 4, fri
        from    legacy_sales
        union all
        select  item, year, (week - 1) * 7 + 5, sat
        from    legacy_sales
        union all
        select  item, year, (week - 1) * 7 + 6, sun
        from    legacy_sales
        ) ls
        inner join fiscal on
            fiscal.year = ls.year
)
一花一树开 2024-11-01 20:41:26

从:legacy_sales 转换

item | year | week | mon | tue | wed | thu | fri | sat |sun 
4444   2011     29    10    0     4    15     18    25   30

为:sales

item | units |     date
4444   10       2011-03-14

需要用英语 python 编写:

for row in legacy_sales:
    year = row['year']
    week = row['week']
    for day_name in "mon | tue | wed | thu | fri | sat |sun".split(" | "):
        some how turn (year, week, day_name) into year_month_day
        insert into sales values (row['item'], row[day_name], year_month_day)

converting from: legacy_sales

item | year | week | mon | tue | wed | thu | fri | sat |sun 
4444   2011     29    10    0     4    15     18    25   30

to: sales

item | units |     date
4444   10       2011-03-14

with needs this about in English python:

for row in legacy_sales:
    year = row['year']
    week = row['week']
    for day_name in "mon | tue | wed | thu | fri | sat |sun".split(" | "):
        some how turn (year, week, day_name) into year_month_day
        insert into sales values (row['item'], row[day_name], year_month_day)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文