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

这个SQL要怎么实现?
TRADE_MONEY TERM_ID ORG_ID ORG_DESC
50 938750 1010203 开去营业
50 938750 1010203 开去营业
50 938750 1010203 开去营业
50 983594 1010202 配额营业
50 983594 1010202 配额营业
50 983594 1010202 配额营业
50 983594 1010202 配额营业
50 938750 1010203 开去营业
50 938750 1010203 开去营业
50 938750 1010203 开去营业
50 938750 1010203 开去营业
50 938750 1010203 开去营业
50 938750 1010203 开去营业
50 938750 1010203 开去营业
50 938750 1010203 开去营业
50 938750 1010203 开去营业

想得到如下这张表
org_id org_desc count sum
1010203 开去营业 12 600
1010202 配额营业 4 200

*count为所选机构的记录数,sum为所选机构的trade_moeny和

传入的查询值为org_id   in   1010203,1010202

谢谢

------解决方案--------------------
select org_id,org_desc,count(*),sum(trade_moeny) from table
where org_id in ( '1010203 ', '1010202 ')
group by org_id,org_desc

这样就可以了吧!
------解决方案--------------------
SELECT org_id, MIN(org_desc) AS org_desc, COUNT(*) AS cnt,
SUM(trade_money) AS sm
FROM table1
GROUP BY org_id
UNION ALL
SELECT NULL, NULL, COUNT(*), SUM(trade_money) FROM table1
------解决方案--------------------
都取别名,上下一致
select org_id,org_desc,count(*) as count,sum(trade_moeny) as sum from table
where org_id in ( '1010203 ', '1010202 ')
group by org_id,org_desc
union all
select ' ' as org_id, ' ' as org_desc,COUNT(*) as count,SUM(trade_money) as sum FROM table1