Im trying to have a Total row at the end of query result im using MS-SQL 2012, need some help heres my query
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.
;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:
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.
+----+----------------------------+---------+-------------+
| 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
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.