Link to home
Start Free TrialLog in
Avatar of RLLewis
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(LastFillQuantity)) AS 'SUM'
FROM db.dbo.table
WHERE user=@userid
AND timeofexecution >= @starttime
group by buysell
Avatar of rafrancisco
rafrancisco

Try this:

select BuySell, count(*) AS '#',CONVERT(char(16),SUM(ISNULL(LastFillQuantity, 0))) AS 'SUM'
FROM db.dbo.table
WHERE user=@userid
AND timeofexecution >= @starttime
group by buysell
Avatar of RLLewis

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(ISNULL(B.LastFillQuantity, 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
Avatar of RLLewis

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
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(LastFillQuantity)) 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'
Avatar of RLLewis

ASKER

well that gives me my zeros, but the '#' and 'SUM' is no longer output
ASKER CERTIFIED SOLUTION
Avatar of rafrancisco
rafrancisco

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of RLLewis

ASKER

SELECT 'B' AS BuySell, 0 as '#' , '0' as 'SUM'
UNION ALL
SELECT 'S' AS BuySell, 0 AS '#', '0' as 'SUM'
Avatar of RLLewis

ASKER

yep, got it.  thank you very much
Avatar of RLLewis

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?