[英]Adding a total row to the end of query result
Im trying to have a Total row at the end of query result im using MS-SQL 2012, need some help heres my query 我试图在使用MS-SQL 2012查询结果的末尾有一个总行,需要一些帮助继承我的查询
SELECT
PropertyValue As Answer,Count(*) As rCount
FROM
QuestionerDetail AS Temp
where
QuestionId = 42 and FormId = 1
GROUP BY PropertyValue
Union All
SELECT 'Total',sum(rCount)
FROM
temp
Im doing something really wrong here. 我在这里做错了。
The Result should be like 结果应该是这样的
Answer rCount
-------------
One 10
Two 25
Total 35
Thanks. 谢谢。
You can't use the alias in another part of the union. 您不能在联合的另一部分中使用别名。
Try below instead: 请尝试以下方式:
SELECT
PropertyValue As Answer, Count(*) As rCount
FROM
QuestionerDetail AS Temp
where
QuestionId = 42 and FormId = 1
GROUP BY PropertyValue
Union All
SELECT 'Total', COUNT(*)
FROM
QuestionerDetail
where
QuestionId = 42 and FormId = 1
Since you're using SQL Server, you could do this with a CTE. 由于您使用的是SQL Server,因此可以使用CTE执行此操作。
;WITH Temp AS
(
SELECT PropertyValue As Answer, Count(*) As rCount
FROM QuestionerDetail
WHERE QuestionId = 42 and FormId = 1
GROUP BY PropertyValue
)
SELECT Answer, rCount
FROM Temp
UNION ALL
SELECT 'Total' as Answer, SUM(rCount) as rCount
FROM Temp
You can use this syntax : 您可以使用以下语法:
WITH Temp AS(
SELECT
PropertyValue As Answer
,Count(*) As rCount
FROM QuestionerDetail AS Temp
where QuestionId = 42 and FormId = 1
GROUP BY PropertyValue
)
SELECT Answer, rCount FROM Temp
UNION ALL
SELECT 'Total', SUM(rCount) FROM Temp
I hope it will help you!! 我希望它会对你有所帮助!! Good luck :) 祝好运 :)
Just add WITH ROLLUP: 只需添加WITH ROLLUP:
SELECT
PropertyValue As Answer, Count(*) As rCount
FROM
QuestionerDetail AS Temp
GROUP BY PropertyValue WITH ROLLUP
i have created this table with joins in RAILS so i want sub total of price_value at bottom line. 我在RAILS中创建了这个带连接的表,所以我想在底线总计price_value。
+----+----------------------------+---------+-------------+
| id | name | item_id | price_value |
+----+----------------------------+---------+-------------+
| | 20 packs | 281 | 500.0 |
| | kingfisher-3l | 145 | 899.0 |
| | triple sec | 185 | 299.0 |
| | jagermeister | 179 | 599.0 |
| | campari | 181 | 389.0 |
| | craganmore 12 yrs | 207 | 998.0 |
| | Tandoori Jhinga | 45 | 450.0 |
| | Chicken Makhmali Kebab | 43 | 320.0 |
| | Irani Fish Tikka | 41 | 400.0 |
| | Indonesian Udag Sambal | 93 | 1500.0 |
| | Tom Yum with veg | 3 | 160.0 |
| | Hot & Sour with veg | 6 | 160.0 |
| | Salad Nicoise | 15 | 255.0 |
| | Lamb Gilafi Seekh | 44 | 400.0 |
| | Spicy wild Prwan Curry | 108 | 500.0 |
| | Wild Mushroom | 2 | 160.0 |
| | Minestrone Alla | 1 | 320.0 |
| | Tom Yum with Chicken | 4 | 360.0 |
| | Wild Mushroom | 2 | 320.0 |
| | Prawn Dim Sum | 18 | 280.0 |
| | Seafood Biryani | 61 | 400.0 |
| | Coconut Panacotta | 130 | 250.0 |
| | singleton 12 yrs | 269 | 11030.0 |
| | don angel silver | 266 | 6354.0 |
| | johnnie walker -gold label | 272 | 13792.0 |
+----+----------------------------+---------+-------------+
this is the controller action view code 这是控制器动作视图代码
def daily_wise_report
@result = Item.select(:name, :price_value, :item_id). joins(:order_items)
respond_to do |format|
format.html
format.csv { send_data @result.to_csv }
format.xls
end
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.