Q:

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 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?

A:

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
)

