Forums Register Login

SQL question

+Pie Number of slices to send: Send
Dear all,

Supposed that we have a table Person that contains two column (Name, Car).
Supposed that we have 3 kind of car: Car1, Car2, Car3.
How can we count the number of each kind of car that a person have without using 3 different separated SQL queries like that:
Select Name, count(*) from Person where Car = 'Car1' Group By Name .
Select Name, count(*) from Person where Car = 'Car2' Group By Name .
Select Name, count(*) from Person where Car = 'Car3' Group By Name .

I would like to use only 1 SQL query to do this.
Thanks you.
+Pie Number of slices to send: Send
The easiest way:

If you want only one row for each person, use a case expression (conforming ANSI SQL-92)

Similar to the second query but using outer joins:

+Pie Number of slices to send: Send
 

Ireneusz Kordal wrote:The easiest way:

If you want only one row for each person, use a case expression (conforming ANSI SQL-92)



Thank you, the second approach is exactly what I want, not the first approach.
However, I wonder if this approach works well with all database?
+Pie Number of slices to send: Send
 

Thank you, the second approach is exactly what I want, not the first approach.
However, I wonder if this approach works well with all database?


You must try. And read your database documentation.
Oracle support the above queries, as I remember from version 9i.
BWA HA HA HA HA HA HA! Tiny ad:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com


reply
reply
This thread has been viewed 1158 times.
Similar Threads
Using final
hashCode and equals implementation
Creating multi object in a loop without using list
Bumper car program
how to import custom made class in source file
More...

All times above are in ranch (not your local) time.
The current ranch time is
Mar 28, 2024 07:23:09.