sql server - sql query to find stores open current week and last year -


i trying write query in sql server stores open last year , current week in 1 single query. condition should stores should open full week , not have single day missing in week. below query doesn't display openlastyear = '1' though store open last year, happens because store hasn't transacted in current week. want rectify through query.

my tables are:

enter image description here

my query below:

select ss.datekey, iif ((select sum(tablea.net) net   tablea inner join tableb on tablea.datekey = tableb.datekey (tableb.weekcommencing = dbo.dimdate.weekcommencing) ,  (tablea.storeid = ss.storeid)) > 0,   iif ((select count(tablea.storeid)  wasopen  tablea inner join tableb on tablea.datekey = tableb.datekey     (tableb.weekcommencing = dbo.dimdate.weekcommencing) ,  (tablea.net not null) , (tablea.net > 0) , (tablea.storeid =  tablea.storeid)) = 7, 1, 0), 0) openthisweek,  iif ((select  count(tablea.storekey) wasopen   tablea inner join tableb on tablea.datekey = tableb.[datekeysametime]     (tableb.financialwc = dbo.dimdate.financialwc)  , (tablea.net not null) , (tablea.net > 0) , (tablea.storeid =  ss.storeid)) = 7, 1, 0) openlastyear, ss.storeid   tablea ss inner join dbo.dimdate on ss.datekey = dbo.dimdate.datekey 


Comments

Popular posts from this blog

angular - Ionic slides - dynamically add slides before and after -

minify - Minimizing css files -

Add a dynamic header in angular 2 http provider -