求一个sql语句吧
tableA
UID Name Desc
1 Jack
2 Tom
3 Lucy
tableB
UID BID Mark Score
1 1 A 20
1 2 B 30
1 3 C 40
2 1 C 10
2 2 B 20
2 3 A 30
3 1 A 10
3 2 B 25
3 3 D 40
要求:
输入:顺序输入Mark值,如:A C
输出:同时包含A,C,且C的BID要大于A的BID,计算出Score的差值
比如上面数据的结果应该是
UID Result
1 20
其中UID=2不满足条件,因为C的BID小于A的BID
UID=3不满足条件,因为它没有同时包含A,C
最好不要用oracle特有的函数或者方法吧。谢谢。
效率要高,因为数据实在太多了。
------解决方案--------------------select a.uid ,c.score-a.score as result
from tableB a, tableB b
where a.mark = 'A '
and a.uid = b.uid
and a.bid < b.bid
and b.mark = 'C '
------解决方案--------------------select a.uid ,c.score-a.score as result
from tableB a, tableB b
where a.mark = 'A '
and a.uid = b.uid
and a.bid < b.bid
and b.mark = 'C '
------解决方案--------------------上面的二位都大意了,纠正一下。
select a.UID,a.Score-b.Score as Result
from tableB a,tableB b
where a.Mark= 'A ' and b.Mark= 'C ' and a.UID=b.UID and b.BID> a.BID
另外楼主没有提到tableA,如果加上tableA的Name就是
select a.UID,c.Name,a.Score-b.Score as Result
from tableB a,tableB b,tableA c
where a.Mark= 'A ' and b.Mark= 'C ' and a.UID=b.UID and c.UID=a.UID and b.BID> a.BID