sql - Select records where the only exist 1 in a joined table -
i have following query:
select      a.postcard_id, a.stamp_id, b.end_dt     pst_vs_stamp join      stamp b on a.postcard_id = b.postcard_id      b.account 'aa%'     , b.end_dt = '9999-12-31' group      a.postcard_id, a.stamp_id, b.end_dt having      count(a.postcard_id) < 2 but wrong results.
i want postcards id's there 1 record (having < 2) in pst_vs_stamp table. how can query this?
do aggregation in subquery, on table want 1 row.  because there 1 row, can use aggregation function pull out value of column (for 1 row min(col) column's value):
select s.postcard_id, vs.stamp_id, s.end_dt stamp s join      (select vs.postcard_id, min(stamp_id) stamp_id       pst_vs_stamp vs       group vs.postcard_id       having count(*) = 1      ) s      on vs.postcard_id = s.postcard_id s.account 'aa%' , s.end_dt = '9999-12-31'; 
Comments
Post a Comment