日期:2014-05-17  浏览次数:20855 次

sql中同事满足两个条件的,求助


1,查出每个name1并且每个月的cost1都是大于等于450的,2,查出每个月cost1最少的name1,

------解决方案--------------------
select * from t t3 where not existis(select 1 from (select distinct name1 nm1 from t where t.cost1<450) t2 where t3.name1=t2.nm1)

SELECT T2.*
  FROM (SELECT MIN(COST1) cost1, month1
          FROM (SELECT name1, month1, SUM(COST1) COST1 FROM T GROUP BY T.name1, T.month1)
         GROUP BY month1) T1,
       (SELECT name1, month1, SUM(COST1) COST1 FROM T GROUP BY T.name1, T.month1) T2
 WHERE T1.COST1 = T2.COST1
   AND T1.month1= T2.month1;