求个SQL语句,请高手指点(100分)

  • 主题发起人 主题发起人 lili365
  • 开始时间 开始时间
L

lili365

Unregistered / Unconfirmed
GUEST, unregistred user!
样本:
user_id type_code fee
--------------------------------------------
3207022825537395 aa 5
3207030625569066 aa 30
3207030625569066 bb 2569
3207030625569066 cc 2569
3207030625569066 dd 2569
3207032725708063 cc 980
3207032725708063 dd 2569
3207052826118345 dd 3000
3207052826118385 aa 3000
3207052826118785 cc 100
3207062826287178 aa 2277
3207062826287178 dd 2569
3207062826287732 aa 17
3207072826478148 bb 35
要求通过SQL语句完成如下格式,非程序实现,简单先答对者奖励:
user_id aa bb cc dd
---------------------------------------------------------
3207022825537395 5 0 0 0
3207030625569066 30 2569 2569 2569
3207032725708063 0 0 980 2569
3207052826118345 0 0 0 3000
 
select a.user_id, isnull(b.fee, 0) aa, isnull(c.fee, 0) bb, isnull(d.fee, 0) cc, isnull(e.fee, 0) dd from (select distinct user_id from T_Test) a
left join T_Test b on b.user_id = a.user_id and b.type_code = 'aa'
left join T_Test c on c.user_id = a.user_id and c.type_code = 'bb'
left join T_Test d on d.user_id = a.user_id and d.type_code = 'cc'
left join T_Test e on e.user_id = a.user_id and e.type_code = 'dd'
 
to lzh1983:
经验证有问题
 
select a.user_id, nvl(b.fee, 0) aa, nvl(c.fee, 0) bb, nvl(d.fee, 0) cc, nvl(e.fee, 0) dd from (select distinct user_id,fee from T_Test) a
left join T_Test b on b.user_id = a.user_id and b.type_code = 'aa'
left join T_Test c on c.user_id = a.user_id and c.type_code = 'bb'
left join T_Test d on d.user_id = a.user_id and d.type_code = 'cc'
left join T_Test e on e.user_id = a.user_id and e.type_code = 'dd'
----------
忘记告诉是在oracle环境,修改后成功。
 
放分吧。
 
接受答案了.
 
后退
顶部