advanced sql question
Discussion
hi.
i've got a sql report to produce but i've got no idea how to go about it.
i have 3 tables Machine, User and Wins.
the Wins table has 2 foreign keys which links to 1 record in the User table and 1 in the Machine table.
I need to produce a query which lists machines with 3 columns which are the number of wins for certain date range which are calculated on user.DateOfBirth.
i.e.
Number of plays per machine
Machine---0-17 years-----18-21 years-----21 years+
Machine 1---50----1000-----12
Machine 2---0------5-------299
hope that makes sense!
does anyone know how i can do this or where i can find out?
i've got a sql report to produce but i've got no idea how to go about it.
i have 3 tables Machine, User and Wins.
the Wins table has 2 foreign keys which links to 1 record in the User table and 1 in the Machine table.
I need to produce a query which lists machines with 3 columns which are the number of wins for certain date range which are calculated on user.DateOfBirth.
i.e.
Number of plays per machine
Machine---0-17 years-----18-21 years-----21 years+
Machine 1---50----1000-----12
Machine 2---0------5-------299
hope that makes sense!
does anyone know how i can do this or where i can find out?
.SELECT M.MACHINE_NR,
. SUM(
. CASE
. U.DOB <= 17 THEN 1
. ELSE 0
. END AS ZEROTO17),
. SUM(
. CASE
. U.DOB >17 AND U.DOB <= 21 THEN 1
. ELSE 0
. END) AS EIGHTEENTO21,
. SUM(
. CASE
. U.DOB >21 THEN 1
. ELSE 0
. END) AS OVER21
.FROM MACHINE M
. INNER JOIN WINS W ON (W.MACHINE_NR = M.MACHINE_NR)
. INNER JOIN USER U ON (U.USER_NR = W.USER_NR)
.GROUP BY M.MACHINE_NR
>> Edited by Don on Wednesday 13th October 16:14
. SUM(
. CASE
. U.DOB <= 17 THEN 1
. ELSE 0
. END AS ZEROTO17),
. SUM(
. CASE
. U.DOB >17 AND U.DOB <= 21 THEN 1
. ELSE 0
. END) AS EIGHTEENTO21,
. SUM(
. CASE
. U.DOB >21 THEN 1
. ELSE 0
. END) AS OVER21
.FROM MACHINE M
. INNER JOIN WINS W ON (W.MACHINE_NR = M.MACHINE_NR)
. INNER JOIN USER U ON (U.USER_NR = W.USER_NR)
.GROUP BY M.MACHINE_NR
>> Edited by Don on Wednesday 13th October 16:14
Gassing Station | Computers, Gadgets & Stuff | Top of Page | What's New | My Stuff



)