简体   繁体   English

如何使用SQL语句在指定的日期获取汇总的总和

[英]How to Get Aggregated Sums At Specified Dates Using SQL Statement

I am trying to generate sums of transactions which is contained in a SQL Server table using this script: 我正在尝试使用以下脚本生成SQL Server表中包含的交易总和:

Select Category,Sum(Items) as CategoryTotals 
from TransactionsTable  
where TransDate <=ReportingDate
Group By Category

I also have a list of dates for which this information is required as: 我还有一个日期列表,需要该信息的日期是:

Select ReportingDates From AllMyDatesTable

Can anyone assist me with how I can structure a query to join the two queries to get a list of CategoryTotals at Specified dates similar to 任何人都可以协助我如何构造一个查询以将两个查询结合起来以获取指定日期的CategoryTotal列表,类似于

Select ReportingDates,CategoryTotals from (My Two Tables)?

Any help appreciated 任何帮助表示赞赏

Something like this? 像这样吗 If you want to show dates even if no transaction is found go for left join instead of inner join 如果即使未找到任何交易也要显示日期,请选择left join而不是inner join

Select d.ReportingDates,t.Category,Sum(d.Items)
  from AllMyDatesTable d inner join TransactionsTable t 
    on d.ReportingDates = t.TransDate 
   and d.TransDate <= ReportingDate
 group by d.ReportingDates,t.Category

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

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