我希望得到' Price_amount'的总和。属性结果集。 答案是100 + 150 = 250但是如何将其显示为结果集?这是我到目前为止的查询:
SELECT *
FROM tBoard
INNER JOIN tPrice
ON tBoard.PriceBand_ID = tPrice.PriceBand_ID
WHERE Board_start_date BETWEEN '20150101' AND '20150131'
结果集:
Board_ID|Board_start_date|Board_end_date|PriceBand_ID|Price_amount
001 |01-01-2015 |16-01-2015 |1 |100
002 |01-01-2015 |28-01-2015 |15 |150
我正在使用SQL SERVER MANAGEMENT STUDIO 2012 Express
答案 0 :(得分:1)
如果要查看detalis,可以使用窗口聚合函数。
使用窗口函数的一个好处是,与分组查询不同,窗口查询不会隐藏detali。它们为每个基础查询的行返回一行。
使用OVER子句定义函数的行窗口。
你可以试试这个:
SELECT *, sum(Price_amount) over(order by Board_ID rows between UNBOUNDED PRECEDING and current row) AS [SUM]
FROM tBoard
INNER JOIN tPrice
ON tBoard.PriceBand_ID = tPrice.PriceBand_ID
WHERE Board_start_date BETWEEN '20150101' AND '20150131'
或者您可以使用基本的group by子句(没有详细信息)
SELECT Board_ID,Board_start_date,Board_end_date,PriceBand_ID,sum(Price_amount) AS [SUM]
FROM tBoard
INNER JOIN tPrice
ON tBoard.PriceBand_ID = tPrice.PriceBand_ID
WHERE Board_start_date BETWEEN '20150101' AND '20150131'
GROUP BY Board_ID,Board_start_date,Board_end_date,PriceBand_ID
答案 1 :(得分:0)
只需使用窗口功能:
SELECT *, SUM(price_amount) OVER () as total_price_amount
FROM tBoard b INNER JOIN
tPrice p
ON b.PriceBand_ID = p.PriceBand_ID
WHERE Board_start_date BETWEEN '20150101' AND '20150131';