日期:2014-05-18  浏览次数:20491 次

关于连接的问题
有这样一张表
name                 date                   bal
    1           2004/08/31             1000
    1           2004/01/01             2000
    2           2004/08/31             3000
    2           2004/01/01             4000

得到如下结果
name                 date                   bal         utl       ytl
    1               2004/08/31         1000                   -1000
    2               2004/08/31         3000                   -1000
结果字段utl是比上月2004/07/31的差值(因为原表内没有2004/07/31的记录所以显示结果为空),ytl是比年初2004/01/01的差值.
能不能只写一句Sql语句得到要的结果啊
请大家帮帮忙!!!!!!!!!!!!!!!!

------解决方案--------------------
select name,date,bal,utl=bal-(select bal from table1 where name=a.name and date= '2004/07/31 '),ytl=bal-(select bal from table1 where name=a.name and date= '2004/01/01 ') from table1 a where date= '2004/08/31 '

好了,这么晚上来竟然还有人问问题,给你解决了吧,省得你睡不着。