按日期范围的SQL查询组

时间:2016-05-19 22:24:10

标签: sql sql-server

我有以下数据表 -

StartDate   EndDate    Amount    
2/1/2016    4/30/2016   2.265
2/1/2016    12/31/2099  16.195
5/1/2016    12/31/2099  37.75

我正在尝试编写一个查询来总结日期范围内的金额,并给我以下结果

StartDate   EndDate    Amount
2/1/2016    4/30/2016   18.46
5/1/2016    12/31/2099  53.945

结果需要是与该日期范围相加的不同日期范围。如上例所示,第2行的日期与第1行和第3行重叠。因此需要在第1行和第3行添加第2行。

我在sql server 2012上写这个查询,请告知我应采取什么方法。

以下是生成样本数据的查询

 SELECT * INTO #tmp_GridResults_1
FROM (
SELECT N'2016-02-01 00:00:00.000' AS [StartDate], N'2016-04-30 00:00:00.000' AS [EndDate], N'2.265' AS [Amount] UNION ALL
SELECT N'2016-02-01 00:00:00.000' AS [StartDate], N'2099-12-31 00:00:00.000' AS [EndDate], N'16.195' AS [Amount] UNION ALL
SELECT N'2016-05-01 00:00:00.000' AS [StartDate], N'2099-12-31 00:00:00.000' AS [EndDate], N'37.75' AS [Amount] ) t;
SELECT [StartDate], [EndDate], [Amount]
FROM #tmp_GridResults_1

3 个答案:

答案 0 :(得分:0)

我认为您无法同时使用StartDate和EndDate进行分组。但是,如果您只使用StartDate和Amount,则可以尝试:

  

选择StartDate,sum(Amount)作为#tmp_GridResults_1 group by StartDate的金额

这将为您提供StartDate的金额分组。

答案 1 :(得分:0)

从我的预览评论我认为您想要通过StartDate进行分组,在这种情况下您可以使用:

ContextMenu

但是,您需要在问题中指定要如何对其进行分组,因为有2个日期字段,您可以按以下方式进行分组:

SELECT
mt.StartDate,
SUM(Amount) AS 'Amount'
FROM MyTable mt
WHERE mt.StartDate BETWEEN
'2016-05-18 00:00:00.000' AND '2016-06-20 00:00:00.000' --your date range
GROUP BY StartDate

还有2个日期字段,您的日期范围可能会有很大差异。

GROUP BY StartDate --groups records that have the same StartDate

GROUP BY EndDate   --groups records that have the same EndDate

GROUP BY StartDate, EndDate --groups records that have same StartDate and EndDate

基于此,你可以构建你需要的查询,但是你问题中提供的数据仍然是你想要实现的模糊,你需要更具体,并提供更多的数据,以便产生一个确切的答案你的问题。

要补充的另一件事是,如果考虑到时间,如果日期相同但时间不同,则不会对它们进行分组。

WHERE StartDate BETWEEN @sd AND @ed --will get records whose start date is inside the provided range

WHERE EndDate BETWEEN @sd AND @ed --will get records whose end date is inside the provided range

WHERE StartDate >= @sd AND EndDate <= @ed -- will get records that started and ended inside the provided date range

WHERE StartDate BETWEEN @sd AND @ed OR EndDate BETWEEN @sd AND @ed
--this last one will get records whose StartDate or EndDate are inside the provided date range

答案 2 :(得分:0)

以下查询将提供所需的结果: -

select StartDate,EndDate,SUM(Amount) over (partition by StartDate) AS Amount into #t1    
from table_name
select StartDate,EndDate,SUM(Amount) over (partition by EndDate) AS Amount  into #t2 
from table_name


select * from (

select distinct P.StartDate,P.EndDate,P.Amount from (
select StartDate,MIN(EndDate) EndDate,Amount from #t1
group by StartDate,Amount) P
JOIN (
select MIN(StartDate) StartDate,EndDate,Amount from #t2
group by EndDate,Amount) Q on P.StartDate=Q.StartDate OR P.EndDate=Q.EndDate
where P.Amount>=Q.Amount

UNION

select distinct P.StartDate,P.EndDate,P.Amount from (
select MIN(StartDate) StartDate,EndDate,Amount from #t2
group by EndDate,Amount) P
JOIN (
select StartDate,MIN(EndDate) EndDate,Amount from #t1
group by StartDate,Amount) Q on P.StartDate=Q.StartDate OR  P.EndDate=Q.EndDate
where P.Amount>=Q.Amount

) A

只需将table_name替换为您想要结果的表格,如果有任何混淆,请告诉我