我有下面的表格。我想总结部分列并在最后一条记录后写下总数。如何在sql查询中指定总数和结果。
Model parts
Model1 4
Model2 2
Model3 6
Model4 5
Model5 7
Total 24
答案 0 :(得分:2)
您可以使用UNION
查询来显示详细数据,并附加一行来显示摘要。
SELECT 0 AS order_by, Model, parts
FROM YourTable
UNION ALL
SELECT 1 AS order_by, 'Total' AS Model, Sum(parts) AS parts
FROM YourTable
ORDER BY 1, 2;
答案 1 :(得分:2)
试试这个query
SELECT models, parts
FROM your_table
UNION
SELECT 'Total' AS models, SUM(parts) AS parts from your_table