开发者

SQL fiscal dates, flat files, misery

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 开发者_StackOverflow社区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?


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
)


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)
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜