1.请用SQL 语句实现:从account表中查询出所有月份的发生额都比101 科目相应月份的发生额高的科目。请注意:account 中有很多科目,都有1 -12 月份的发生额。
accID :科目代码,occMonth :发生额月份,debitOccur :发生额。
解析:
1.先查询101科目所对应的月份以及月份所对应的发生额
select occMonth,debitOccur from account where accID='101';
2.接着查询account表中除了101科目外,月发生额比101对应月份的发生额高的科目信息
select a.* from account a, (select occMonth,debitOccur from account where accID='101') b where a.occMonth=b.occMonth and a.debitOccur>b.debitOccur;
3.最后去掉不是每一个月份的发生额都比101科目对应月份的发生额高的科目
select accID from ( select a.* from account a, (select occMonth,debitOccur from account where accID='101') b where a.occMonth=b.occMonth and a.debitOccur>b.debitOccur ) c group by accID having count(occMonth)=12;