对多个字段的 SSRS 报告进行分组
Grouping SSRS report on multiple fields
我有一个我们将称为 ReportOne 的报告,在这个 ReportOne 中,我正在使用存储过程查询此报告的数据。存储过程查询 returns 两个值,即 'TravelDate' 和 'Status'。
我的报告有四个字段,'BeginDate'、'EndDate'、'Status' 和 'Days'。
我的问题是,我需要按 'Status' 天和连续天对报告进行分组。来自 TravelDate 的连续天数。
'BeginDate' 将是第一个新日期
'EndDate' 将是最后一个连续日期。
'Status' 将是状态。
'Days' 将是连续的天数。
示例,
TravelDate | Status
1/1/2001 | Leave
1/2/2001 | Leave
1/3/2001 | Leave
1/5/2001 | Leave
1/6/2001 | Travel
报告将如下所示。
BeginDate | EndDate | Status | Days
1/1/2001 | 1/3/2001 | Leave | 3
1/5/2001 | 1/5/2001 | Leave | 1
1/6/2001 | 1/6/2001 | Travel | 1
例子
Declare @YourTable Table ([TravelDate] date,[Status] varchar(50))
Insert Into @YourTable Values
('1/1/2001','Leave')
,('1/2/2001','Leave')
,('1/3/2001','Leave')
,('1/5/2001','Leave')
,('1/6/2001','Travel')
Select BeginDate=min(TravelDate)
,EndDate =max(TravelDate)
,Status =max(Status)
,Days =datediff(DAY,min(TravelDate),max(TravelDate))+1
From (
Select *
,Grp = DateDiff(DAY,'1900-01-01',TravelDate) - row_number() over (partition by status order by TravelDate)
From @YourTable
) A
Group By Grp
Order By BeginDate
Returns
BeginDate EndDate Status Days
2001-01-01 2001-01-03 Leave 3
2001-01-05 2001-01-05 Leave 1
2001-01-06 2001-01-06 Travel 1
编辑 -- 从存储过程中捕获 -- @YourTable 结构必须与存储过程的结构相匹配
Declare @YourTable Table ([TravelDate] date,[Status] varchar(50))
Insert Into @YourTable
Exec youStoredProcedure
Select BeginDate=min(TravelDate)
,EndDate =max(TravelDate)
,Status =max(Status)
,Days =datediff(DAY,min(TravelDate),max(TravelDate))+1
From (
Select *
,Grp = DateDiff(DAY,'1900-01-01',TravelDate) - row_number() over (partition by status order by TravelDate)
From @YourTable
) A
Group By Grp
Order By BeginDate
编辑 - 嵌套子查询
Select BeginDate=min(TravelDate)
,EndDate =max(TravelDate)
,Status =max(Status)
,Days =datediff(DAY,min(TravelDate),max(TravelDate))+1
From (
Select *
,Grp = DateDiff(DAY,'1900-01-01',TravelDate) - row_number() over (partition by status order by TravelDate)
From (
-- Your Query Here ---
) A
) A
Group By Grp
Order By BeginDate
编辑 - 从 TVF
消耗
Select BeginDate=min(TravelDate)
,EndDate =max(TravelDate)
,Status =max(Status)
,Days =datediff(DAY,min(TravelDate),max(TravelDate))+1
From (
Select *
,Grp = DateDiff(DAY,'1900-01-01',TravelDate) - row_number() over (partition by status order by TravelDate)
From [dbo].[YourTableValedFunction](Param1,Param2) src
) A
Group By Grp
Order By BeginDate
我有一个我们将称为 ReportOne 的报告,在这个 ReportOne 中,我正在使用存储过程查询此报告的数据。存储过程查询 returns 两个值,即 'TravelDate' 和 'Status'。
我的报告有四个字段,'BeginDate'、'EndDate'、'Status' 和 'Days'。
我的问题是,我需要按 'Status' 天和连续天对报告进行分组。来自 TravelDate 的连续天数。
'BeginDate' 将是第一个新日期
'EndDate' 将是最后一个连续日期。
'Status' 将是状态。
'Days' 将是连续的天数。
示例,
TravelDate | Status
1/1/2001 | Leave
1/2/2001 | Leave
1/3/2001 | Leave
1/5/2001 | Leave
1/6/2001 | Travel
报告将如下所示。
BeginDate | EndDate | Status | Days
1/1/2001 | 1/3/2001 | Leave | 3
1/5/2001 | 1/5/2001 | Leave | 1
1/6/2001 | 1/6/2001 | Travel | 1
例子
Declare @YourTable Table ([TravelDate] date,[Status] varchar(50))
Insert Into @YourTable Values
('1/1/2001','Leave')
,('1/2/2001','Leave')
,('1/3/2001','Leave')
,('1/5/2001','Leave')
,('1/6/2001','Travel')
Select BeginDate=min(TravelDate)
,EndDate =max(TravelDate)
,Status =max(Status)
,Days =datediff(DAY,min(TravelDate),max(TravelDate))+1
From (
Select *
,Grp = DateDiff(DAY,'1900-01-01',TravelDate) - row_number() over (partition by status order by TravelDate)
From @YourTable
) A
Group By Grp
Order By BeginDate
Returns
BeginDate EndDate Status Days
2001-01-01 2001-01-03 Leave 3
2001-01-05 2001-01-05 Leave 1
2001-01-06 2001-01-06 Travel 1
编辑 -- 从存储过程中捕获 -- @YourTable 结构必须与存储过程的结构相匹配
Declare @YourTable Table ([TravelDate] date,[Status] varchar(50))
Insert Into @YourTable
Exec youStoredProcedure
Select BeginDate=min(TravelDate)
,EndDate =max(TravelDate)
,Status =max(Status)
,Days =datediff(DAY,min(TravelDate),max(TravelDate))+1
From (
Select *
,Grp = DateDiff(DAY,'1900-01-01',TravelDate) - row_number() over (partition by status order by TravelDate)
From @YourTable
) A
Group By Grp
Order By BeginDate
编辑 - 嵌套子查询
Select BeginDate=min(TravelDate)
,EndDate =max(TravelDate)
,Status =max(Status)
,Days =datediff(DAY,min(TravelDate),max(TravelDate))+1
From (
Select *
,Grp = DateDiff(DAY,'1900-01-01',TravelDate) - row_number() over (partition by status order by TravelDate)
From (
-- Your Query Here ---
) A
) A
Group By Grp
Order By BeginDate
编辑 - 从 TVF
消耗Select BeginDate=min(TravelDate)
,EndDate =max(TravelDate)
,Status =max(Status)
,Days =datediff(DAY,min(TravelDate),max(TravelDate))+1
From (
Select *
,Grp = DateDiff(DAY,'1900-01-01',TravelDate) - row_number() over (partition by status order by TravelDate)
From [dbo].[YourTableValedFunction](Param1,Param2) src
) A
Group By Grp
Order By BeginDate