E
E
Eugene2015-11-07 23:18:15
SQL
Eugene, 2015-11-07 23:18:15

How to make COUNT(*) return zero?

Hello! MsSql Server 2003 DBMS, there is a query:
SELECT roomtype, COUNT(*)
FROM rooms
WHERE roomtype = 'lux' AND dirty = 'Y'
GROUP BY roomtype
Well, actually, the problem is that if the condition is not met, the query does not return anything at all. And you need to get zero:
lux 0
I tried to fool around with CASE, but it didn’t work out. Does anyone know how to make it return null?

Answer the question

In order to leave comments, you need to log in

3 answer(s)
M
Max, 2015-11-07
@MaxDukov

SELECT roomtype, ISNULL(COUNT(*),0)
FROM rooms
WHERE roomtype = 'люкс' AND dirty = 'Y'
GROUP BY roomtype

M
Melkij, 2015-11-08
@melkij

Why are you grouping by field if you know in advance that there will be exactly one different value?

SELECT COUNT(*)
FROM rooms
WHERE roomtype = 'люкс' AND dirty = 'Y'

V
Vitaly Vitrenko, 2015-11-08
@Vestail

SELECT 'люкс' as roomtype, COALESCE( (SELECT COUNT(*)
FROM rooms
WHERE roomtype = 'люкс' AND dirty = 'Y'
GROUP BY roomtype), 0)

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question