SQL: Comparing count of 2 fields with specific value -
i have 2 tables, 1 (jobs) contains list of jobs , second contains details of records in each job.
jobs jobid count 2 b 3 records jobid recordid tobeprocessed isprocessed a1 1 1 a2 1 1 b b1 1 1 b b2 1 0 b b3 1 0
how able create query list jobs have count of tobeprocessed has value of 1 equal count of isprocessed has value of 1? in advance. appreciated.
start calculation of number of items tobeprocessed
set 1
or isprocessed
set one:
select jobid , sum(case when tobeprocessed=1 1 else 0 end) tobeprocessedisone , sum(case when isprocessed=1 1 else 0 end) isprocessedisone records group jobid
this gives counts, not ones tobeprocessedisone
equal isprocessedisone
. make sure records 2 same, use either having
clause, or nested subquery:
-- having clause select jobid , sum(case when tobeprocessed=1 1 else 0 end) tobeprocessedisone , sum(case when isprocessed=1 1 else 0 end) isprocessedisone records group jobid having sum(case when tobeprocessed=1 1 else 0 end)=sum(case when isprocessed=1 1 else 0 end) -- nested subquery condition select * ( select jobid , sum(case when tobeprocessed=1 1 else 0 end) tobeprocessedisone , sum(case when isprocessed=1 1 else 0 end) isprocessedisone records group jobid ) tobeprocessedisone = isprocessedisone
note: if tobeprocessed
, isprocessed
of type not allow values other 0 or one, can replace case
statement name of column, example:
select jobid , sum(tobeprocessed) tobeprocessedisone , sum(isprocessed) isprocessedisone records group jobid having sum(tobeprocessed)=sum(isprocessedd)
Comments
Post a Comment