Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/sql-server/27.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Sql server 需要帮助将列透视到SQL Server中包含计数的行吗_Sql Server_Pivot - Fatal编程技术网

Sql server 需要帮助将列透视到SQL Server中包含计数的行吗

Sql server 需要帮助将列透视到SQL Server中包含计数的行吗,sql-server,pivot,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 感谢

这就是我试图在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

感谢您的帮助。

我使用自定义项创建动态日期范围,请参见下文。我在示例数据中添加了另一行来说明多对多

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
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) 

这是另一种方法,它是有效的

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

请发布您的尝试,以便我们可以帮助调试。没有运气意味着什么?你有错误吗?我想你需要一个日历表来生成那些没有出现在原始表中的日期。不是一个真正的透视图。你需要一个日历表,并加入一个介于开始dt和结束dt之间的->搜索stackoverflow,有很多答案。嗨,Tab,谢谢你的评论,我不知道如何实现这一点,请给出一些建议,这样我可以尝试一下。谢谢你们,你们能给我举个例子吗。嗨,John,谢谢。当我创建函数时,我遇到这个错误,级别16,状态1,过程udf create Range Date,第12行,无法绑定多部分标识符df.dateFrom。Msg 4104,级别16,状态1,过程udf创建范围日期,第13行无法绑定多部分标识符df.dateFrom。Msg 4104,级别16,状态1,程序udf创建范围日期,第14行,这是您的排序规则。。区分大小写。搜索并替换所有df。用DF。
;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