sql - Combining SUM and CASE returns 0? -


against sql server, i'm trying calculate value based on year date, want sum values july 16, 2012 , prior , display them. i'm using following query (note i've replaced parameters simple integers calculate value today):

select sum(case          when (             (                 dns.oday <= 16                 , (dns.fiscalyear + 1) = 13                 , dns.omonth = 7             )             or                (                 (dns.fiscalyear + 1) = 13                 , dns.omonth < 7                 )             )          dns.qtyshipped         else 0         end) shipped_units  mytable dns 

however, query returning 0 rows. if replace dns.qtyshipped integer, 1, still returns 0. case statement isn't being evaluated correctly. logic flawed? or syntax issue (e.g. need more parentheses)?

thanks!

additional comments:

to test, i've ran following query:

select sum(dns.qtyshipped) mytable dns                      (dns.oday <= 16                     , (dns.fiscalyear + 1) = 13                     , dns.omonth = 7)                      or                     ((dns.fiscalyear + 1) = 13                     , dns.omonth < 7) 

which returns large number. confusing.

the code mentioned earlier working absolutely fine. please double check values using evaluate conditions. example, please confirm if fiscalyear value 2013 or 13. i've used variables instead of column names in code mentioned below , returning expected results:

declare @oday integer set @oday=17 declare @fiscalyear int set @fiscalyear=12 declare @omonth int  set @omonth=8  select sum(case      when (         (             @oday <= 16             , (@fiscalyear + 1) = 13             , @omonth = 7         )         or            (             (@fiscalyear + 1) = 13             , @omonth < 7             )         )      1     else 0     end) shipped_units 

Comments

Popular posts from this blog

php - Calling a template part from a post -

Firefox SVG shape not printing when it has stroke -

How to mention the localhost in android -