我有以下SQL查询,它是从选定期间总结旅游税。
SELECT
BTR_DESCRIPTION AS TOURIST_TAX_NAME,
ISNULL(BREAG_TOURIST_TAX_PRICE,0) AS PRICE,
SUM(DATEDIFF(d, BRE_DATEFROM, BRE_DATETO)) AS QUANTITY,
ISNULL(BREAG_TOURIST_TAX_PRICE,0) * SUM(DATEDIFF(d, BRE_DATEFROM, BRE_DATETO)) AS FINAL_PRICE
FROM BOS_RESERVATION
LEFT OUTER JOIN BOS_RESADDGUEST ON BREAG_BRE_ID = BRE_ID
LEFT OUTER JOIN BOS_TAX_REASONS ON BTR_ID = BREAG_BTR_ID
WHERE BREAG_DATEFROM >= '2018-02-28' AND BREAG_DATETO <= '2018-03-31'
GROUP BY BTR_DESCRIPTION, BREAG_TOURIST_TAX_PRICE
查询结果:
现在我要对FINAL_PRICE行中的所有值求和。所以结果必须是:5,652 但我不知道该怎么做。我尝试了以下SQL:
SUM(ISNULL(BREAG_TOURIST_TAX_PRICE,0) * SUM(DATEDIFF(d, BRE_DATEFROM, BRE_DATETO))) AS FINAL_PRICE_SUM
但是这给了我错误:无法对包含聚合或子查询的表达式执行聚合函数。
你能帮我解决一下如何从FINAL_PRICE行汇总所有值吗?
谢谢!
答案 0 :(得分:1)
如果您想要总的最终价格,请使用CTE或子查询:
WITH r as (
SELECT BTR_DESCRIPTION AS TOURIST_TAX_NAME,
COALESCE(BREAG_TOURIST_TAX_PRICE, 0) AS PRICE,
SUM(DATEDIFF(day, BRE_DATEFROM, BRE_DATETO)) AS QUANTITY,
COALESCE(BREAG_TOURIST_TAX_PRICE, 0) * SUM(DATEDIFF(day, BRE_DATEFROM, BRE_DATETO)) AS FINAL_PRICE
FROM BOS_RESERVATION LEFT OUTER JOIN
BOS_RESADDGUEST
ON BREAG_BRE_ID = BRE_ID LEFT OUTER JOIN
BOS_TAX_REASONS
ON BTR_ID = BREAG_BTR_ID
WHERE BREAG_DATEFROM >= '2018-02-28' AND BREAG_DATETO <= '2018-03-31'
GROUP BY BTR_DESCRIPTION, BREAG_TOURIST_TAX_PRICE
)
SELECT SUM(FINAL_PRICE)
FROM r;
答案 1 :(得分:1)
你可以这样做:
<span id="type"></span>
<span id="number"></span>
<span data-ix-tag="__ix_data_shared_uint_80_" id="valve" data-ix-refresh="interval"></span>
<script>
function GetValveTypeString(tag) {
if (tag.value == 0) {
document.getElementById("type").innerHTML = "";
}
else if (tag.value == 1) {
document.getElementById("type").innerHTML = "DV";
}
else if (tag.value == 2) {
document.getElementById("type").innerHTML = "AV";
}
}
iX.createTag('__ix_data_shared_int_80_', GetValveTypeString, 'interval')
</script>
<script>
var valveID = document.getElementById("valve").innerHTML;
function GetValveID(tag) {
if (tag.value == 0) {
document.getElementById("number").innerHTML = "";
}
else if (tag.value == 1) {
document.getElementById("number").innerHTML = valveID;
}
else if (tag.value == 2) {
document.getElementById("number").innerHTML = valveID * 2;
}
}
iX.createTag('__ix_data_shared_int_80_', GetValveID, 'interval')
</script>