※ 引述《statue (statue)》之銘言:
: 我有一張表格, 表格內容大概如下
: source type dest count
: B damage A 1000
: C heal A 800
: D heal A 600 (溢捕 400)
: E damage C 400
: D heal C 600 (溢捕 200)
: damage 表示傷害, heal 表示治療
: 如果我想用 SQL 知道 D 的溢捕量, 可以用 SQL 求得嗎?
一些前提如下:
1、戰鬥紀錄應該是有時間順序的,所以假設順序由上到下先後發生。
2、我記得艾大用的是mysql? 先假設是好了。
3、新增一個「num」的流水號欄位,表時間性。
4、type和count為mysql關鍵字,先假設改為type1、count1
5、假設Table Name是test1
求得所有人溢補量之SQL如下:
select c.source, greatest(sum(c.heal)-sum(c.damage), 0) overheal from
(
select a.source, a.num, max(a.count1) heal,
greatest
(
sum(case when b.type1='damage' then b.count1 else b.count1*-1 end), 0
) damage
from test1 a, test1 b
where a.dest=b.dest and b.num<a.num and a.type1='heal'
group by a.num, a.source
) c group by c.source
執行完,可得結果:
source overheal
C 0
D 600
簡單說明一下:
內層的子查詢,是自己join自己,設法找出較早發生的damage和heal來計算之,
外層則針對source來group。
--
※ 發信站: 批踢踢實業坊(ptt.cc)
◆ From: 118.166.129.119
※ 編輯: diamondking 來自: 118.166.129.119 (05/17 12:53)