sql - TSQL Find Missing Values -
i have 2 tables:
tablea calendar of possible weeks should have sales data per company.
tableb has financial data sales per company per week have been reported.
tablea columns
[cmp_code, year, week]
tableb columns
[cmp_code, year, week, sales]
query criteria:
1) list weeks missing tableb current year , previous year companies
2) list previous year sales value company , week if exists. (example cmp 1234 missing week 13, 2013, show value of week in 2012)
i've tried joins either 0 values returned or millions of values returned. don't know start.
i'm new sql , appreciate offered. in advance.
this 1 way:
select tablea.*, bprev.sales tablea left outer join tableb on tablea.cmp_code = tableb.cmp_code , tablea.[year] = tableb.[year] , tablea.[week] = tableb.[week] left outer join tableb bprev on tablea.cmp_code = bprev.cmp_code , tablea.[year] = bprev.[year]+1 , tablea.[week] = bprev.[week] tableb.cmp_code null
Comments
Post a Comment