I have 2 tables, Requests and Responses
Requests:
RequestId UserId InsertDate
1 1 5/4/2013
2 2 6/4/2012
. . .
. . .
Responses:
Responseid Requestid(FK) ResponseCode
1 1 A
2 1 V
3 1 M
4 2 A
5 2 S
6 2 D
. . .
. . .
Request is considered as "Passed" if response codes A and D are received (like request with id 2 in my example). I want to write a sql queries that will return 3 things:
- number of "Passed" requests
- RequestIDs of those requests that pass
- RequestIDs of those requests that didn't pass
I wrote something but I don't like it and I suppose there is better way. My query is:
SELECT COUNT(*)
FROM
(
SELECT count(*) as c, req.RequestID
FROM Responses res inner join Requests req
on req.RequestID = res.RequestID
where
res.ResponseCode = 'A' or
res.ResponseCode = 'D'
group by req.RequestID
)cc
where c = 2
Thanks in advance.