sql 输出语句怎么写能输出下面的表

比如数据表结构如上图,怎样写sql语句可以输出下面的表

--如果是固定条件的这么写最简单
select
lrdate
,orgid
,pluid
,(select count from table where lrdate = a.lrdate and orgid = a.ordid and pluid = a.pluid and twtype='ps') as ps
,(select count from table where lrdate = a.lrdate and orgid = a.ordid and pluid = a.pluid and twtype='pd') as pd
,(select count from table where lrdate = a.lrdate and orgid = a.ordid and pluid = a.pluid and twtype='th') as th
from table as a
group by lrdate
,orgid
,pluid
温馨提示:答案为网友推荐,仅供参考
第1个回答  2014-12-17
行列转换, 试试这个:
select lrdate,orgid,pluid,
max(case when twtype='ps' then count end) ps,
max(case when twtype='pd' then count end) pd,
max(case when twtype='th' then count end) th
from tabname
group by lrdate,orgid,pluid
第2个回答  2014-12-17
对twtype进行行转列控制。
第3个回答  2014-12-17
select a.*,b.count,c.count from
(select lrdate, orgid,pluid,count from table1 where twtype='ps') a,
(select orgid,count from table1 where twtype='pd') b,
(select orgid,count from table1 where twtype='th') c
where a.orgid=b.orgid
and a.orgid=c.orgid
第4个回答  推荐于2017-09-19
MSSQL语句:
select lrdate,orgid,pluid,case when twtype = 'ps' then count else 0 end ps
,case when twtype = 'pd' then count else 0 end pd
,case when twtype = 'th' then count else 0 end th
from table
group by lrdate,orgid,pluid本回答被提问者和网友采纳