sql server - SQL query when result is empty -
i have table this
user itemnumber datebought (yyyymmdd) 1 20160101 b 2 20160202 c 3 20160903 d 4 20160101
now have show total number of items bought each user after date 20160202 (2 february 2016)
i used
select user, count(itemnumber)<br/> table<br/> datebought >= 20160202<br/> group user<br>
it gives me results
b 1 c 1
but want this
a 0 b 1 c 1 d 0
please tell me quick method / efficient method ?
try this,
declare @table table ( [user] varchar(1), itemnumber int, datebought date ) insert @table values ('a',1,'20160101'), ('b',2,'20160202'), ('b',2,'20160202'), ('b',2,'20160202'), ('c',3,'20160903'), ('d',4,'20160101') select * @table select [user], sum(case when datebought >= '20160202' 1 else 0 end) itemcount @table group [user]
Comments
Post a Comment