How to write multiple count select statement in sql -
i need combine 2 count statement 1 select. can done?
select dealer ,firstname ,producttype ,date ,comments ,count(*) total select dealer ,firstname ,valid ,comments ,count(*) invalidtotal
you use union statement create single query, return 2 rows. howwever, column name count(*) column come first query, add flag, such isvalid column in example below
select vr.dealerid ,vr.commissionrunid ,vd.producttype ,vd.valid ,vd.comments, 1 isvalid, ,count(*) total union select vr.dealerid ,vr.commissionrunid ,vd.valid ,vd.comments 0 isvalid, ,count(*) invalidtotal
however, if looking 1 row, i.e.
select vr.dealerid ,vr.commissionrunid ,vd.producttype ,vd.valid ,vd.comments, ,count(*) total ,count(*) invalidtotal.
then need see how determine valid vs invalid counting purposes. doable, need create case statement returns 1 or 0 (for valid or invalid) , sum columns. without knowing sql dialect, following pseudo-code representation..
sum (case isvalid='y' 1 else 0 end) totalvalid, sum (case isvalid='n' 1 else 0 end) totalinvalid,
Comments
Post a Comment