SQL如何实现这类问题(给建议者必给分)(200分)

  • 主题发起人 主题发起人 zhengjp
  • 开始时间 开始时间
Z

zhengjp

Unregistered / Unconfirmed
GUEST, unregistred user!
原始表:<br>&nbsp;Pname &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;Cdate &nbsp; &nbsp; &nbsp; Fnu &nbsp;Mnu &nbsp;Lnu Onu<br>AP0089/09 &nbsp; 2006-11-24 &nbsp; &nbsp; &nbsp; &nbsp;100 &nbsp; &nbsp;0 &nbsp; &nbsp;0 &nbsp; &nbsp; 0<br>AP0089/09 &nbsp; 2006-11-26 &nbsp; &nbsp; &nbsp; &nbsp;0 &nbsp; &nbsp; 150 &nbsp; 0 &nbsp; &nbsp; 0<br>AP0095/09 &nbsp; 2006-11-26 &nbsp; &nbsp; &nbsp; &nbsp;200 &nbsp; &nbsp;0 &nbsp; &nbsp;0 &nbsp; &nbsp; 0<br>GJ0189/07 &nbsp; 2006-11-27 &nbsp; &nbsp; &nbsp; &nbsp;0 &nbsp; &nbsp; &nbsp;0 &nbsp; &nbsp;0 &nbsp; &nbsp; 100<br>.................<br>结果:<br>&nbsp;Pname &nbsp; &nbsp; &nbsp; 2006-11-24 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;2006-11-25 &nbsp; &nbsp; &nbsp; &nbsp;2006-11-26 &nbsp; &nbsp; &nbsp; 2006-11-27 <br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Fnu &nbsp;Mnu Lnu Onu &nbsp; Fnu &nbsp;Mnu &nbsp;Lnu Onu &nbsp; ........... &nbsp; &nbsp;.. &nbsp; &nbsp; &nbsp; onu<br>AP0089/09 &nbsp; 100 &nbsp;0 &nbsp; 0 &nbsp; 0 &nbsp; &nbsp; 0 &nbsp; &nbsp;0 &nbsp; &nbsp;0 &nbsp; 0 &nbsp; &nbsp; &nbsp;0 &nbsp; 150 &nbsp;0 &nbsp;0 &nbsp; 0 &nbsp;0 &nbsp;0 &nbsp;0<br>AP0095/09 &nbsp; &nbsp;0 &nbsp; 0 &nbsp; 0 &nbsp; 0 &nbsp; &nbsp; 0 &nbsp; &nbsp;0 &nbsp; &nbsp;0 &nbsp; 0 &nbsp; &nbsp; 200 &nbsp; 0 &nbsp; 0 &nbsp;0 &nbsp; 0 &nbsp;0 &nbsp;0 &nbsp;0<br>GJ0189/07 &nbsp; &nbsp;0 &nbsp; 0 &nbsp; 0 &nbsp; 0 &nbsp; &nbsp; 0 &nbsp; &nbsp;0 &nbsp; &nbsp;0 &nbsp; 0 &nbsp; &nbsp; 0 &nbsp; &nbsp; 0 &nbsp; 0 &nbsp;0 &nbsp; 0 &nbsp;0 &nbsp;0 100<br>.....................<br>这样的结果能不能实现?结查中的例跟据日期来决定的
 
用动态SQL查询, 先查<br>select distinct Cdate &nbsp; &nbsp; &nbsp; from 表名 得到数据为记录1,记录2,记录3...<br><br>再生成<br>select A.Pname,<br>&nbsp; &nbsp; &nbsp; &nbsp;(select sum(Fnu)<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; from 表名 B<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;where B.Pname = A.Pname<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;and B.Cdate = 记录1) as 记录1 + Fnu,<br>&nbsp; &nbsp; &nbsp; &nbsp;(select sum(Mnu)<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; from 表名 B<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;where B.Pname = A.Pname<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;and B.Cdate = 记录1) as 记录1 + Mnu,<br>&nbsp; &nbsp; &nbsp; &nbsp;(select sum(Lnu)<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; from 表名 B<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;where B.Pname = A.Pname<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;and B.Cdate = 记录1) as 记录1 + Lnu,<br>&nbsp; &nbsp; &nbsp; &nbsp;(select sum(Onu)<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; from 表名 B<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;where B.Pname = A.Pname<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;and B.Cdate = 记录1) as 记录1 + Onu,<br>&nbsp; &nbsp; &nbsp; &nbsp;(select sum(Fnu)<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; from 表名 B<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;where B.Pname = A.Pname<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;and B.Cdate = 记录2) as 记录2 + Fnu,<br>&nbsp; &nbsp; &nbsp; &nbsp;.. .<br>&nbsp; from (select distinct Pname from 表名) A
 
LS的方法可行。。。<br>还有就是写成存储过程,生成动态临时表,往里面插数据,然后再查出来。。。
 
行转列,用case语句,
 
多谢各位的见意。。。<br>说详细点。。。
 
以前弄过,语句写得很长,后来用函数解决,因为表的列数一直在增大。<br><br>但是,再后来,觉得自己很多事,象这种表真的没有设计的价值,换一种思路表达出来,他好你也好。
 
多人接受答案了。
 

Similar threads

I
回复
0
查看
851
import
I
S
回复
0
查看
3K
SUNSTONE的Delphi笔记
S
后退
顶部