gpt4 book ai didi

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

转载 作者:太空宇宙 更新时间:2023-11-03 19:31:54 27 4
gpt4 key购买 nike

使用sqlite3、python

我有一个蹩脚的旧文件legacy_sales:

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

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

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

我在另一个表财政中有财政年度的开始日期

year | start_date
2011 2010-08-30

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

insert into sales from (insert magic here) 

不使用任何 UDF...

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

最佳答案

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
)

关于python - SQL 会计日期、平面文件、痛苦,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5386631/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com