遇到一个我想在查询中总结结果的问题。
示例如下:
NAME | FRUIT | PRICE
JOHN | APPLE | 2
JOHN | APPLE | 2
JOHN | APPLE | 2
JOHN | APPLE | 2
DAVE | GRAPE | 3
DAVE | GRAPE | 3
DAVE | GRAPE | 3
这是我现在的桌子,尽管我需要的是约翰斯业务的摘要,如下所示:
NAME | FRUIT | PRICE
JOHN | APPLE | 2
JOHN | APPLE | 2
JOHN | APPLE | 2
JOHN | APPLE | 2
JOHN | TOTAL | 8
DAVE | GRAPE | 3
DAVE | GRAPE | 3
DAVE | GRAPE | 3
我试图对信息进行分组,但是它不能反映我想要的信息,此外,如果约翰要获得不同的成果,则需要对其进行总结,然后再总结下一部分。
任何建议都会很棒
答案 0 :(得分:0)
为什么只想要约翰的摘要?您可以为此使用union all
:
select name, fruit, price
from ((select name, fruit, price, 1 as ord, fruit as f
from t
) union all
(select name, 'Total', sum(price), 2 as ord, fruit as f
from t
where name = 'John'
group by name, fruit
)
) x
order by name, f, ord;