我有一张桌子
name test count
----------------------------
sam test1 10
sam test2 2
sam passcount 5
riz test4 3
riz test5 4
riz passxount 6
我想显示结果
name test pass count fail count total count
-------------------------------------------------------------------
sam test1 7 10 17
sam test2 15 2 17
riz test 4 10 3
riz test 5 9 4 13
答案 0 :(得分:0)
好的,花了一点时间才意识到列标题有点误导
/**
pass = total for name minus that for current test
fail = total count for that test in src table
total = total count for that name
*/
select
t1.name,
t1.test,
t2.total-t1.count as pass_count,
t1.count as fail_count,
t2.total as total_count
from Kamrams_table as t1
inner join
(
select
k1.name,
sum(k1.count) as total
from Kamrams_table as k1
group by k1.name
) as t2
on t1.name = t2.name
;
注意: 我相信输出表中test4和test 5之间的额外空间是一个错字? 还有其他一些有用的结构:CTE和窗口函数。