读书人

关于连接的有关问题

发布时间: 2012-02-02 23:57:14 作者: rapoo

关于连接的问题
有这样一张表
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 '

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

读书人网 >SQL Server

热点推荐