I'm new to SQL and can't seem to ask the right question, as in every time I formulate it differently I end up with the same results: A query that groups data by several properties (users that share more than one property).
I'm writing a JSP-based website on the Glassfish server, and manage the database from MS Access.
What I'm interested in, is basically grouping by several distinct properties. Say I have a table that shows which items a user has. Something like this:
id | name | item1 | item2 | item3 | item4 |
--------------------------------------------------
1 | name1 | yes | yes | | yes |
----+-------+---------+--------+--------+--------+
2 | name2 | yes | | | yes |
----+-------+---------+--------+--------+--------+
3 | name3 | | yes | | yes |
----+-------+---------+--------+--------+--------+
4 | name4 | yes | yes | | yes |
----+-------+---------+--------+--------+--------+
5 | name5 | | | yes | yes |
.
.
.
The query that I need, would return the following:
ItemID | Number of users with this item
-----------------------------------------
item1 | 3
item2 | 3
item3 | 1
item4 | 5
I don't see how GROUP BY
can be used here, as the result I'm looking for basically demands that the properties of the original table, will now appear as the values in each row of the resulting table.
What is the right query, and how such operation is called (it's not grouping by each property, it's something else...)?