需要帮助将列转移到具有计数的SQL Server行

时间:2016-06-28 14:02:02

标签: sql-server pivot

这是我想在SQL Server中实现的但没有运气。

我的表格如下

ID  Start Dt    End Dt  Count
1   3/1/2016    6/1/2016    3

这就是我需要的,如果Count列为3,那么我需要生成三行

ID  Start Dt    End Dt  Count
1   3/1/2016    4/1/2016    1
1   4/1/2016    5/1/2016    1
1   5/1/2016    6/1/2016    1

感谢您的帮助..

2 个答案:

答案 0 :(得分:1)

我使用UDF创建动态日期范围(见下文)。我在示例数据中添加了另一行来说明多对多。

Declare @Table table (ID int,StartDt Date,EndDate Date,Count int)
Insert into @Table values
(1,'2016-03-01','2016-06-01',3),
(2,'2016-02-01','2016-07-01',22)


Declare @MinDate Date,@MaxDate Date
Select @MinDate=min(StartDt),@MaxDate=max(EndDate) from @Table

Select ID,DateR1,DateR2,Count=cast((Count+0.0)/DateDiff(MM,StartDt,EndDate) as money)
  From @Table A
  Join (Select DateR1=RetVal,DateR2=DateAdd(MM,1,RetVal)  From [dbo].[udf-Create-Range-Date](@MinDate,@MaxDate,'MM',1)) B 
    on DateR1 Between StartDt and EndDate and DateR1<EndDate

返回

ID  DateR1      DateR2      Count
1   2016-03-01  2016-04-01  1.00
1   2016-04-01  2016-05-01  1.00
1   2016-05-01  2016-06-01  1.00
2   2016-02-01  2016-03-01  4.40
2   2016-03-01  2016-04-01  4.40
2   2016-04-01  2016-05-01  4.40
2   2016-05-01  2016-06-01  4.40
2   2016-06-01  2016-07-01  4.40

UDF

CREATE FUNCTION [dbo].[udf-Create-Range-Date] (@DateFrom datetime,@DateTo datetime,@DatePart varchar(10),@Incr int)

Returns 
@ReturnVal Table (RetVal datetime)

As
Begin
    With DateTable As (
        Select DateFrom = @DateFrom
        Union All
        Select Case @DatePart
               When 'YY' then DateAdd(YY, @Incr, df.dateFrom)
               When 'QQ' then DateAdd(QQ, @Incr, df.dateFrom)
               When 'MM' then DateAdd(MM, @Incr, df.dateFrom)
               When 'WK' then DateAdd(WK, @Incr, df.dateFrom)
               When 'DD' then DateAdd(DD, @Incr, df.dateFrom)
               When 'HH' then DateAdd(HH, @Incr, df.dateFrom)
               When 'MI' then DateAdd(MI, @Incr, df.dateFrom)
               When 'SS' then DateAdd(SS, @Incr, df.dateFrom)
               End
        From DateTable DF
        Where DF.DateFrom < @DateTo
    )

    Insert into @ReturnVal(RetVal) Select DateFrom From DateTable option (maxrecursion 32767)

    Return
End

-- Syntax Select * from [dbo].[udf-Create-Range-Date]('2016-10-01','2020-10-01','YY',1) 
-- Syntax Select * from [dbo].[udf-Create-Range-Date]('2016-10-01','2020-10-01','DD',1) 
-- Syntax Select * from [dbo].[udf-Create-Range-Date]('2016-10-01','2016-10-31','MI',15) 
-- Syntax Select * from [dbo].[udf-Create-Range-Date]('2016-10-01','2016-10-02','SS',1) 

答案 1 :(得分:0)

这是另一种方法而且有效。

declare @temp_table table (
        id int,
        start_date date,
        end_date date,
        count_no int
    )

    insert into @temp_table values (1,'3/1/2016','6/1/2016',3)

    select * from @temp_table

以上查询将返回您的原始结果。以下查询将返回您的预期结果。

;with temp(count) as (
    select 1
    union all
    select count + 1
    from temp
    where count < (select count_no from @temp_table)
)
select tt.id, 
    convert(char, dateadd(month,count -1 , tt.start_date), 101) as start_date,
    convert(char, dateadd(month,count,tt.start_date ) , 101) as end_date, 
    count(*) as count

from temp as t cross join @temp_table as tt
group by tt.id,tt.start_date,t.count

HTH