简体   繁体   English

SQL - 逐行计算组的总值

[英]SQL - calculate total value of group, row by row

I have a fact sales table that is structured like the below.我有一个结构如下的事实销售表。 This contains information on subscriptions (sales) by customer.这包含有关客户订阅(销售)的信息。

CustomerKey客户密钥 SubscriptionKey订阅密钥 StartDate开始日期 End date结束日期 Value价值
385884 385884 1256 1256 2020-01-17 00:00:00.000 2020-01-17 00:00:00.000 2025-06-17 00:00:00.000 2025-06-17 00:00:00.000 200 200
385884 385884 2346 2346 2020-03-11 00:00:00.000 2020-03-11 00:00:00.000 2022-06-10 00:00:00.000 2022-06-10 00:00:00.000 400 400
385884 385884 5433 5433 2022-10-17 00:00:00.000 2022-10-17 00:00:00.000 2024-07-17 00:00:00.000 2024-07-17 00:00:00.000 500 500

I want to add a row which shows me at the time of end date, the total value of a customer so the business can use the total value to determine whether the customer is worth renewing or not.我想添加一行,在结束日期显示客户的总价值,以便企业可以使用总价值来确定客户是否值得续订。 So based on the above table it would look like this..所以根据上表,它看起来像这样..

CustomerKey客户密钥 SubscriptionKey订阅密钥 StartDate开始日期 End date结束日期 Value价值 ValueAtEndDate ValueAtEndDate
385884 385884 1256 1256 2020-01-17 00:00:00.000 2020-01-17 00:00:00.000 2025-06-17 00:00:00.000 2025-06-17 00:00:00.000 200 200 200 200
385884 385884 2346 2346 2020-03-11 00:00:00.000 2020-03-11 00:00:00.000 2022-06-10 00:00:00.000 2022-06-10 00:00:00.000 400 400 600 600
385884 385884 5433 5433 2022-10-17 00:00:00.000 2022-10-17 00:00:00.000 2024-07-17 00:00:00.000 2024-07-17 00:00:00.000 500 500 700 700

So what it needs to do is look at "active" subscription by CustomerKey.. and then calculate the total value of any active subscriptions at the time of [End date] .所以它需要做的是查看 CustomerKey 的“活跃”订阅......然后计算[End date]时任何活跃订阅的总价值。 This is done if the [End fate] falls between the [startdate] and end date of another subscription associated to that contract.如果[结束命运]介于与该合同关联的另一个订阅的[开始日期]和结束日期之间,则会执行此操作。

I have tried the method below but really can't get my head around the best way of even approaching this.. so any tips or just pointers in the right direction would be appreciated.我已经尝试了下面的方法,但真的无法理解甚至接近这个的最佳方法。所以任何提示或正确方向的指针将不胜感激。

SQL Server LAG() function to calculate differences between rows SQL Server LAG() 函数计算行之间的差异

Image example of below solution..以下解决方案的图像示例.. 在此处输入图像描述

select *
from T t1 cross apply (
    select sum(Value) from T t2
    where t2.CustomerKey = t1.CustomerKey
        and t1.EndDate between t2.StartDate and t2.EndDate
) v(ValueAtEndDate);

This could be just a scalar subquery.这可能只是一个标量子查询。 Either way is essentially the same.无论哪种方式,本质上都是一样的。

https://dbfiddle.uk/?rdbms=sqlserver_2019&fiddle=aa500f79e17b5681410f1e6ce8464551 https://dbfiddle.uk/?rdbms=sqlserver_2019&fiddle=aa500f79e17b5681410f1e6ce8464551

You can use a scalar subquery to compute the extra column.您可以使用标量子查询来计算额外的列。 For example:例如:

select t.*,
  t.value + (
    select sum(value)
    from t u
    where u.end_date between t.startdate and t.enddate 
      and u.susbscriptionkey <> t.susbscriptionkey 
      and u.customerkey = t.customerkey
  ) as vaed
from t

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM