sql server 2008 - Calculate number of grouped rows in SQL -
sql server 2008 - Calculate number of grouped rows in SQL -
i have 2 tables, requests , responses
requests:
requestid userid insertdate 1 1 5/4/2013 2 2 6/4/2012 . . . . . .
responses:
responseid requestid(fk) responsecode 1 1 2 1 v 3 1 m 4 2 5 2 s 6 2 d . . . . . .
request considered "passed" if response codes , d received (like request id 2 in example). want write sql queries homecoming 3 things:
number of "passed" requests requestids of requests pass requestids of requests didn't passi wrote don't , suppose there improve way. query is:
select count(*) ( select count(*) c, req.requestid responses res inner bring together requests req on req.requestid = res.requestid res.responsecode = 'a' or res.responsecode = 'd' grouping req.requestid )cc c = 2
thanks in advance.
pass
select r1.requestid responses r1 bring together responses r2 on r2.requestid = r1.requestid , r1.responsecode = 'a' , r2.responsecode = 'd' compute count(r1.requestid)
(not positive on compute syntax)
not pass
select distinct (r1.requestid) responses r1 total outter bring together responses r2 on r2.requestid = r1.requestid , r1.responsecode = 'a' , r2.responsecode = 'd' r2.requestid null or r1.requestid null
pass
select r1.requestid responses r1 r1.responsecode = 'a' intersect select r1.requestid responses r1 r1.responsecode = 'd'
not pass
select r1.requestid responses r1 except select r1.requestid responses r1 r1.responsecode = 'd' or r1.responsecode = 'a'
sql sql-server-2008 tsql count group-by
Comments
Post a Comment