0

我有一组这样的数据

date1       Price  Availability  ServiceID
2012-10-01  50     1             5
2012-10-02  60     1             5
2012-10-03  60     1             5
2012-10-04  60     1             5
2012-10-06  60     1             5
2012-10-07  60     0             5
2012-10-08  60     1             5

现在我想检查特定时期的总价格、最低可用性和 serviceid 是多少

例如

from 2012-10-01 to 2012-10-03 results would be 170, availability 1 and serviceID 5
from 2012-10-06 to 2012-10-08 results would be 180, availability 0! and serviceID 5
from 2012-10-04 to 2012-10-06 results would be 120, availability 1! and serviceID 5 BUT there is a date missing so availability is 0!

我尝试了类似的东西

select date1, sum(price), min(availability), service from #t
group by date1, price, availability, service
having count(date1) = datediff(day, @startdate, @enddate)

这不起作用,因为如果我按 date1 分组,则计数无效。不知道如何获取总和、最小可用性和检查连续日期。

编辑 如果我想要一个包含详细规范的结果集。并过滤掉 min(availability) = 0 或非连续日期的结果

For example
from 2012-10-01 to 2012-10-03 results would be 170, availability 1 and serviceID 5

date1       Price  Availability  ServiceID
    2012-10-01  50     1             5
    2012-10-02  60     1             5
    2012-10-03  60     1             5

from 2012-10-06 to 2012-10-08 results would be 180, availability 0! and serviceID 5
date1       Price  Availability  ServiceID

from 2012-10-04 to 2012-10-06 results would be 120, availability 1! and serviceID 5 BUT 
date1       Price  Availability  ServiceID
4

2 回答 2

2

试试这个:

select  sum(price) As TotalPrice, 
        service,
        Case When Count(*) = DateDiff(Day, @Startdate, @EndDate) + 1 
                Then Min(Availability)
                Else 0 End As Availability
from    #T
Where   Date1 >= @StartDate
        And Date1 <= @endDate
group by service

通过根据日期过滤 where 子句中的日期,而不是按日期分组,这允许您获取一个计数或行,然后您可以将其与日期差异匹配。请注意,您需要将 1 添加到日期差异,因为从昨天到今天的 DateDiff 仅为 1,但将代表源数据中的 2 行。

我还应该提到,这实际上并不是在检查连续日期。它只是寻找相应的行数来匹配计算的天数。

根据您的评论和编辑,您可能想尝试这个派生表解决方案。

Select  T.date1,
        SummaryData.TotalPrice,
        SummaryData.Availability,
        T.Service
From    #T T
        Inner Join (
            select  sum(price) As TotalPrice, 
            service,
                Case When Count(*) = DateDiff(Day, @Startdate, @EndDate) + 1 
                     Then Min(Availability)
                     Else 0 End As Availability
            from    #T
            Where   Date1 >= @StartDate
                    And Date1 <= @endDate
            group by service
            Having Case When Count(*) = DateDiff(Day, @Startdate, @EndDate) + 1 
                        Then Min(Availability)
                        Else 0 End = 1 
            ) As SummaryData
            On T.Service = SummaryData.Service
Where   Date1 >= @StartDate
        And Date1 <= @endDate
于 2013-03-28T13:07:00.117 回答
0

如果没有缺少日期的曲线球,查询将非常简单:

SELECT SUM(price), MIN(availability) AS Availibility, MIN(serviceID) AS serviceID FROM #t
WHERE date1 BETWEEN @Startdate AND @enddate

然而,查找丢失的日期需要一个额外的 CTE,我将使用t-sql 中的想法获取 2 个日期之间的所有日期,它看起来像这样:

;WITH dates AS (
  SELECT @startdate AS dt
  UNION ALL
  SELECT DATEADD(dd, 1, dt)
    FROM dates s
   WHERE DATEADD(dd, 1, dt) <= @enddate
)
SELECT SUM(price), 
CASE 
    WHEN EXISTS (SELECT * FROM dates WHERE dt NOT IN (SELECT date1 FROM #t WHERE date1 BETWEEN @Startdate AND @enddate)) THEN 0
    ELSE MIN(availability) 
END AS Availibility, 
MIN(serviceID) AS serviceID FROM #t
WHERE date1 BETWEEN @Startdate AND @enddate
于 2013-03-28T13:13:23.953 回答