RLLewis
asked on
need to print zeros when no count exists
how can i print 0 when no counts exist, as opposed to just the blank lines:
select BuySell, count(*) AS '#',CONVERT(char(16),SUM(L astFillQua ntity)) AS 'SUM'
FROM db.dbo.table
WHERE user=@userid
AND timeofexecution >= @starttime
group by buysell
select BuySell, count(*) AS '#',CONVERT(char(16),SUM(L
FROM db.dbo.table
WHERE user=@userid
AND timeofexecution >= @starttime
group by buysell
ASKER
no, it still doesn't return anything at all when the rowcount is zero
Just to clarify what you mean. You want to return all BuySell for the given user for all BuySell values even if the user has no record for it? If this is the case, then you have to join it with a table that contains all BuySell values.
select A.BuySell, count(*) AS '#',CONVERT(char(16),SUM(I SNULL(B.La stFillQuan tity, 0))) AS 'SUM'
FROM db.dbo.BuySell A left outer join db.dbo.table B
on A.BuySell = B.BuySell
WHERE user=@userid
AND timeofexecution >= @starttime
group by A.BuySell
select A.BuySell, count(*) AS '#',CONVERT(char(16),SUM(I
FROM db.dbo.BuySell A left outer join db.dbo.table B
on A.BuySell = B.BuySell
WHERE user=@userid
AND timeofexecution >= @starttime
group by A.BuySell
ASKER
no, i am doing the count/quantity for each user. it is returned as this:
B S
50 110
if there are no buys or sells, i just want it to return this:
B S
0 0
B S
50 110
if there are no buys or sells, i just want it to return this:
B S
0 0
I don't think it is possible with just a single SELECT statement. If it's ok with you not to use a single SELECT statement, this can do the trick:
IF EXISTS (SELECT 'X' FROM db.dbo.table WHERE user = @userid AND timeofexecution >= @starttime)
select BuySell, count(*) AS '#',CONVERT(char(16),SUM(L astFillQua ntity)) AS 'SUM'
FROM db.dbo.table
WHERE user=@userid
AND timeofexecution >= @starttime
group by buysell
ELSE
SELECT 'B' AS BuySell, 0, '0'
UNION ALL
SELECT 'S' AS BuySell, 0, '0'
IF EXISTS (SELECT 'X' FROM db.dbo.table WHERE user = @userid AND timeofexecution >= @starttime)
select BuySell, count(*) AS '#',CONVERT(char(16),SUM(L
FROM db.dbo.table
WHERE user=@userid
AND timeofexecution >= @starttime
group by buysell
ELSE
SELECT 'B' AS BuySell, 0, '0'
UNION ALL
SELECT 'S' AS BuySell, 0, '0'
ASKER
well that gives me my zeros, but the '#' and 'SUM' is no longer output
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
SELECT 'B' AS BuySell, 0 as '#' , '0' as 'SUM'
UNION ALL
SELECT 'S' AS BuySell, 0 AS '#', '0' as 'SUM'
UNION ALL
SELECT 'S' AS BuySell, 0 AS '#', '0' as 'SUM'
ASKER
yep, got it. thank you very much
ASKER
rafrancisco, if there are no buys or no sells, i'd like to do the same.
B 1000 1892
S 0 0
but right now if there are no buys/sells, I’m getting just:
B 1000 1892
what's the best way to do this?
B 1000 1892
S 0 0
but right now if there are no buys/sells, I’m getting just:
B 1000 1892
what's the best way to do this?
select BuySell, count(*) AS '#',CONVERT(char(16),SUM(I
FROM db.dbo.table
WHERE user=@userid
AND timeofexecution >= @starttime
group by buysell