0

我有一个包含 3 个字段“开始日期”和“结束日期”和“分配百分比”的表格。我需要找出该表中从当前日期起 3 个月内的所有记录,

并且(这个很棘手)只有 Sum(Allocation %) > 100 的日期范围重叠的记录。

请帮我提出一个查询。

这是表模式。

Table (ResourceAssignment)
   - ResourceAssignmentID (PK)
   - ResourceID (FK)
   - Assigned To (FK)
   - Start Date
   - End Date
   - Allocation %

我基本上需要在一定时期内(从今天到3个月)找到所有分配的资源。

提前致谢!

4

4 回答 4

1

你有两个通用的解决方案。哪个最好取决于 ResourceAllocation 的大小、冲突的频率以及您打算如何呈现数据。

解决方案 1:列出未来 3 个月内的日期,并对照该列表确定哪些日期分配过多

WITH
  datelist AS (
    SELECT CAST(GETDATE() AS date) [d] UNION ALL SELECT DATEADD(day,1,[d]) FROM datelist WHERE [d] < DATEADD(month,3,GETDATE())
  ),
  allocations AS (
    SELECT *,SUM([Allocation %]) OVER(PARTITION BY [ResourceID],[d]) AS [Total Allocation %]
    FROM datelist
    INNER JOIN ResourceAssignment ON ([d] BETWEEN [Start Date] AND [End Date])
  )
SELECT *
FROM allocations
WHERE [Total Allocation %] >= 100

解决方案 2:在 ResourceAssignment 上使用具有自联接的范围冲突查询

SELECT *
FROM (
  SELECT
    t1.*,SUM(t1.[Allocation %]) OVER(PARTITION BY t1.[ResourceID]) AS [Total Allocation %]
  FROM ResourceAssignment t1
  INNER JOIN ResourceAssignment t2 ON (
    t1.[ResourceID] = t2.[ResourceID] AND
    t1.[ResourceAssignmentID] <> t2.[ResourceAssignmentID] AND
    t1.[Start Date] < t2.[End Date] AND
    t2.[Start Date] < t1.[End Date]
  )
  WHERE
    t1.[Start Date] <= CAST(GETDATE()+30 as date) AND
    t2.[Start Date] <= CAST(GETDATE()+30 as date) AND
    t1.[End Date] > CAST(GETDATE() as date) AND
    t2.[End Date] > CAST(GETDATE() as date)
) t
WHERE [Total Allocation %] >= 100
于 2013-07-17T20:57:16.523 回答
1

不完全确定这是否是您所追求的,但这会向您显示分配高于 100% 的所有 ResourceID,它们的开始或结束日期在过去 3 个月内:

SELECT ResourceID 
FROM ResourceAssignment
WHERE StartDate BETWEEN DATEADD(month,-3,GETDATE())  AND GETDATE()
  OR EndDate BETWEEN DATEADD(month,-3,GETDATE())  AND GETDATE()
GROUP BY ResourceID 
HAVING SUM([Allocation %]) > 100
于 2013-07-17T19:32:08.403 回答
0

我认为这是您要实现的目标:

SELECT SUM(Allocation), ResourceID
FROM ResourceAssignment
WHERE EndDate BETWEEN GETDATE() AND  DATEADD(m,3,GETDATE())
GROUP BY ResourceID
HAVING SUM(Allocation) > 100
于 2013-07-17T19:32:17.493 回答
0

我假设开始日期和结束日期被格式化为日期时间或日期。

我不确定您是否要确定开始日期记录是否在过去 3 个月内或结束日期记录内。

您还需要一个限制来确定总和(分配百分比)。你想总结什么独特的记录。由于您想对分配百分比求和,然后只选择大于 100 的那些,因此您必须弄清楚要拉出哪些然后求和。

您需要使用 With 语句或创建分阶段提取所需信息的子“表”(查询)来完成此操作。我将介绍子查询语句。

为 Sql-Server 2008 R2 编写

首先选择过去 3 个月内的数据

Select ResourceAssignmentID, ResourceID, [Assigned To], [Allocation %]
From ResourceAssignment
Where [Start Date] >= dateadd(Month,-3,Now)

然后使用此数据对分配百分比求和。(我假设基于2个外键的组合)

Select Query1.ResourceID, Query1.[Assigned To], sum(Query1.[Allocation %]) as Sumofallocation
From
(Select ResourceAssignmentID, ResourceID, [Assigned To], [Allocation %]
From ResourceAssignment
Where [Start Date] >= dateadd(Month,-3,Now)) Query1
Group By Query1.ResourceID, Query1.[Assigned To], sum(Query1.[Allocation %]) 

您也许可以在 group by 之前添加 Where 语句以过滤出您的 >100 要求,但我只会添加另一个 Select 语句将它们拉到一起

Select * From
(Select Query1.ResourceID, Query1.[Assigned To], sum(Query1.[Allocation %]) as Sumofallocation
From
(Select ResourceAssignmentID, ResourceID, [Assigned To], [Allocation %]
From ResourceAssignment
Where [Start Date] >= dateadd(Month,-3,Now)) Query1
Group By Query1.ResourceID, Query1.[Assigned To], sum(Query1.[Allocation %]) 
) Query2
Where Query2.Sumofallocation > 100

此外,您似乎正在从事项目管理类型的程序。微软项目不会工作吗?您将能够在您希望的任何时间范围内查看谁被过度分配。如果您有多个项目,您可以将它们合并到一个大的项目文件中并以这种方式查看...

无论如何,我希望这会有所帮助。SQL 可能并不完美,但它应该为您指明一个可行的方向。

于 2013-07-17T19:55:23.393 回答