![]() | |
#1
| |||
| |||
|
#2
| |||
| |||
|
|
Hi There! is there a way to make the count(*) return zero . because the default behavoir is that it will skip this value if there is no records so a simple query like this select userid,count(*) as count from users where userid in (select val from sometable) group by userid if userid 1 has no records . it wont be returned in the query instead i want it to show zero so userid count 1 0 2 3 3 1 4 0 Any hints? Thanks! Guto. |
#3
| |||
| |||
|
|
Hi There! is there a way to make the count(*) return zero . because the default behavoir is that it will skip this value if there is no records so a simple query like this select userid,count(*) as count from users where userid in (select val from sometable) group by userid if userid 1 has no records . it wont be returned in the query instead i want it to show zero so userid count 1 0 2 3 3 1 4 0 Any hints? Thanks! Guto. |
#4
| |||
| |||
|
|
if userid 1 has no records . it wont be returned in the query instead i want it to show zero so userid count 1 0 2 3 3 1 4 0 |
#5
| |||
| |||
|
|
if userid 1 has no records . it wont be returned in the query instead i want it to show zero so userid count 1 0 2 3 3 1 4 0 Scalar subqueries as already said or left join as follows: select userid, count(sometable.val) from users left join sometable on (users.userid = sometable.val) group by userid Gints Plivnahttp://www.gplivna.eu |
#6
| |||
| |||
|
|
On Dec 9, 7:41 pm, Gints Plivna <gints.pli... (AT) gmail (DOT) com> wrote: if userid 1 has no records . it wont be returned in the query instead i want it to show zero so userid count 1 0 2 3 3 1 4 0 Scalar subqueries as already said or left join as follows: select userid, count(sometable.val) from users left join sometable on (users.userid = sometable.val) group by userid Gints Plivnahttp://www.gplivna.eu Thank you all for your reply, but actually I need to be more precise on the issue: 1) I have a list of ID 2) I need to return the count() of these ID even if they are 0 3) Its everything on the same table. My sql sentence is going like this: select ID,count(*) from Service where ID in (51,55,73) and (InicialDate is null or InicialDate > '09/12/2008') group by ID resulting in : ID Column2 ------------ ---------- 55 11454 73 1 What I need is : ID Column2 ------------ ---------- 51 0 55 11454 73 1 Thank you all. |
#7
| |||
| |||
|
|
Thank you all for your reply, but actually I need to be more precise on the issue: 1) I have a list of ID 2) I need to return the count() of these ID even if they are 0 3) Its everything on the same table. My sql sentence is going like this: select ID,count(*) from Service where ID in (51,55,73) and (InicialDate is null or InicialDate > '09/12/2008') group by ID |
#8
| |||
| |||
|
|
On Thu, 10 Dec 2009 04:12:33 -0800 (PST), Guto wrote: (snip) Thank you all for your reply, but actually I need to be more precise on the issue: 1) I have a list of ID 2) I need to return the count() of these ID even if they are 0 3) Its everything on the same *table. My sql sentence is going like this: select ID,count(*) * *from Service * *where ID in (51,55,73) * *and (InicialDate is null or InicialDate > '09/12/2008') group by ID Hi Guto, As Bob says, you need a table source for these ID values, otherwise the DBMS can't produce rows for them. But you can include such a source in your query: In Microsoft SQL Server, you can use something this: WITH MyIDs(ID) *AS (SELECT 51 UNION ALL SELECT 55 UNION ALL SELECT 73) SELECT * * s.ID, COUNT(*) FROM * * * Service AS s INNER JOIN MyIDs AS m * * * ON * m.ID = s.ID WHERE * * *m.InicialDate IS NULL OR * * * * m.InicialDate > '20081209'; -- 09/12/2008 is ambiguous! In other DBMSs, you can use something like this, which is ANSI standard syntax. I use SQL Server, which does not implement this feature, so I might have the syntax wrong. WITH MyIDs(ID) *AS (VALUES ((51), (53), (73)) SELECT * * s.ID, COUNT(*) FROM * * * Service AS s INNER JOIN MyIDs AS m * * * ON * m.ID = s.ID WHERE * * *m.InicialDate IS NULL OR * * * * m.InicialDate > '20081209'; -- 09/12/2008 is ambiguous! Best, Hugo |
#9
| |||
| |||
|
|
On Dec 9, 7:41 pm, Gints Plivna<gints.pli... (AT) gmail (DOT) com> wrote: if userid 1 has no records . it wont be returned in the query instead i want it to show zero so userid count 1 0 2 3 3 1 4 0 Scalar subqueries as already said or left join as follows: select userid, count(sometable.val) from users left join sometable on (users.userid = sometable.val) group by userid Gints Plivnahttp://www.gplivna.eu Thank you all for your reply, but actually I need to be more precise on the issue: 1) I have a list of ID 2) I need to return the count() of these ID even if they are 0 3) Its everything on the same table. My sql sentence is going like this: select ID,count(*) from Service where ID in (51,55,73) and (InicialDate is null or InicialDate> '09/12/2008') group by ID resulting in : ID Column2 ------------ ---------- 55 11454 73 1 What I need is : ID Column2 ------------ ---------- 51 0 55 11454 73 1 Thank you all. |
![]() |
| Thread Tools | |
| Display Modes | |
| |