[英]Sum of different sums
Hello there community,你好社区,
I have problems to combine the value of 3 different sum queries.我在组合 3 个不同总和查询的值时遇到问题。 We have a table with several columns, like:
customer_id, amount_hours, start_date, end_date, one_time
.我们有一个包含几列的表,例如:
customer_id, amount_hours, start_date, end_date, one_time
。
There are 3 different categories/conditions to sum them up.有 3 种不同的类别/条件来总结它们。
one_time is true one_time 是真的
end_date is NULL and one_time is false end_date 为 NULL,one_time 为 false
end_date is not NULL and one_time is false end_date 不是 NULL 并且 one_time 是假的
I got those 3 queries done.我完成了这 3 个查询。 They work perfectly.
他们工作得很好。 Now I want to add the results into a single column next to customer_id of course.
现在我当然想将结果添加到 customer_id 旁边的单个列中。 What the table represents is the amount of "free hours" (sorry, English isn't my native language and I cannot think of a better expression).
表格代表的是“空闲时间”的数量(抱歉,英语不是我的母语,我想不出更好的表达方式)。 Either the customer bought a certain amount of that as a one-time payment or it has a contract that adds amount of X hours per month.
客户购买了一定数量的一次性付款,或者有一份每月增加 X 小时的合同。
Let's say customer random-store has bought 20 hours as one-time payment, paid the whole last year for 5 hours per month and during this current year has changed it to 8 hours per month.假设客户 random-store 购买了 20 小时作为一次性付款,去年全年支付了每月 5 小时的费用,而今年已将其更改为每月 8 小时。 Then it would need to sum 20 + (12months * 5 hours = 60) + (8months {as it is August now} * 8 hours = 64) = 144.
然后它需要求和 20 + (12months * 5 hours = 60) + (8months {as it is August now} * 8 hours = 64) = 144。
So far I got the 3 queries to get 20, 60 and 64. But if I try something like select sum ((select sum(....)+select sum(....)+select sum(....))
, then it is not working. I'm getting syntax-erros and I have the feeling that I'm trying something that just isn't how the correct syntax structure should look like, adding the three different sum-queries. Tried googling it, no luck. Could someone help?到目前为止,我得到了 3 个查询以获得 20、60 和 64。但是如果我尝试类似
select sum ((select sum(....)+select sum(....)+select sum(....))
,然后它不起作用。我得到了语法错误,我有一种感觉,我正在尝试一些不是正确语法结构应该是什么样子的东西,添加了三个不同的求和查询。试过谷歌搜索,没有运气。有人可以帮忙吗?
1st sum:第一个总和:
select sum (amount_hours) from [myTable] where customer_id=100112 AND one_time= 'True'
2nd sum:第二总和:
select sum ((datediff(month, Start_Date, End_Date)+1)* amount_hours) from [myTable] where customer_id = 100112 AND one_time = 'False' and end_date is not NULL
3rd sum:第三笔款项:
select sum ((datediff(month, Start_Date, Getdate())+1) * amount_hours) from [myTable] where customer_id = 100112 AND one_time = 'False' and end_date is NULL
My samples include a customer_id on purpose.我的样本特意包含了一个 customer_id。 Will do without them and a group by later.
以后不用他们和一群人就行了。
1.one_time is true
1.one_time 为真
2.end_date is NULL and one_time is false
2.end_date 为 NULL,one_time 为 false
3.end_date is not NULL and one_time is false
3.end_date 不为 NULL,one_time 为 false
No problems.没问题。
SELECT customer_id,
SUM(CASE WHEN one_time = 'True'
THEN amount_hours
END) sum1,
SUM(CASE WHEN end_date IS NULL and one_time = 'False'
THEN (DATEDIFF(MONTH, Start_Date, End_Date) + 1) * amount_hours
END) sum2,
SUM(CASE WHEN end_date IS NOT NULL and one_time = 'False'
THEN (DATEDIFF(MONTH, Start_Date, Getdate()) + 1) * amount_hours
END) sum3
FROM myTable
GROUP BY customer_id
You may add ELSE 0
to CASEs if needed.如果需要,您可以将
ELSE 0
添加到 CASE。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.