【问题标题】:SQL how to create multiple row from a single rowSQL如何从单行创建多行
【发布时间】:2012-04-24 22:11:26
【问题描述】:

您好,我是 SQL Server 2008 的新手

我想根据另一个列将单行扩展到多行,

例如

date          value
7-2011         5

结果:

2011-07-01     
2011-08-01
2011-09-01
2011-10-01
2012-11-01

日期应该是当前和下个月的第一天重复5次

【问题讨论】:

    标签: sql-server-2008


    【解决方案1】:

    尝试:

    DECLARE @YourTable table (YourDate datetime, value int)
    
    insert into @YourTable VALUES ('2011-7-12',5)
    
    ;WITH AllNumbers AS
    (
        SELECT 0 AS Number
        UNION ALL
        SELECT Number+1
            FROM AllNumbers
            WHERE Number<4
    )
    SELECT 
        dateadd(month,number,DATEADD(month,DATEDIFF(month,0,YourDate),0))
        FROM @YourTable        y
        INNER JOIN AllNumbers  a ON 1=1
    

    输出:

    -----------------------
    2011-07-01 00:00:00.000
    2011-08-01 00:00:00.000
    2011-09-01 00:00:00.000
    2011-10-01 00:00:00.000
    2011-11-01 00:00:00.000
    
    (5 row(s) affected)
    

    它适用于表格中的多行,请参见此处:

    DECLARE @YourTable table (YourDate datetime, ValueOf int)
    
    insert into @YourTable VALUES ('2011-7-12',5)
    insert into @YourTable VALUES ('2012-4-24',6)
    
    ;WITH AllNumbers AS
    (
        SELECT 0 AS Number
        UNION ALL
        SELECT Number+1
            FROM AllNumbers
            WHERE Number<4
    )
    SELECT 
        y.ValueOf
            ,dateadd(month,number,DATEADD(month,DATEDIFF(month,0,y.YourDate),0))
        FROM @YourTable        y
        INNER JOIN AllNumbers  a ON 1=1
        ORDER BY 1,2
    

    输出:

    ValueOf     
    ----------- -----------------------
    5           2011-07-01 00:00:00.000
    5           2011-08-01 00:00:00.000
    5           2011-09-01 00:00:00.000
    5           2011-10-01 00:00:00.000
    5           2011-11-01 00:00:00.000
    6           2012-04-01 00:00:00.000
    6           2012-05-01 00:00:00.000
    6           2012-06-01 00:00:00.000
    6           2012-07-01 00:00:00.000
    6           2012-08-01 00:00:00.000
    
    (10 row(s) affected)
    

    另外,我没有可用的 SQL Server 2008,所以我使用了 datetime,如果你有 2008,你可以使用 DATE 数据类型并且你不必设置 datetime,所以使用这一行:

    dateadd(month,number,y.YourDate)
    

    【讨论】:

    • 谢谢,但是我有一个包含不同月份和值的表,我想按值而不是单个值进行拆分
    • 您好,谢谢它的工作,但我如何从表中动态选择日期和值而不是手动设置它们?
    • 我不确定您要的是什么?你可以随时添加WHERE ValueOf=6 或类似的东西。
    【解决方案2】:
    create  function addMonths(@date date, @limit int)
    returns @date_table TABLE(
    myDate date
    )
    AS
    begin
      declare @cont int
      set @cont = 1
      while (@cont <= @limit) begin
        insert into @date_table values(DATEADD(MONTH,@cont,@date))
        set @cont=@cont+1
      end
    
      return 
    end
    

    用法:

    select * from addMonths(GETDATE(),5)
    

    编辑:

    create table mydates(
    myDate date,
    inc_months int)
    
    
    insert into mydates values ('01/01/2005',3)
    insert into mydates values ('01/01/2006',5)
    
    
    select AM.mydate 
    from mydates MD cross apply addMonths(MD.mydate,MD.inc_months) AM
    

    结果:

    2005-02-01
    2005-03-01
    2005-04-01
    2006-02-01
    2006-03-01
    2006-04-01
    2006-05-01
    2006-06-01
    

    【讨论】:

    • 你传入@date,但在函数中使用GETDATE()
    • 谢谢伙计们,但重点是我不想传递@date 和值,我必须从另一个表中选择它们并在我的新表中进行扩展,例如 1000 行和值这意味着我将不得不传递参数 1000?而不是从现有表中选择它们?
    • @Diego 谢谢你是明星!!如何将功能添加到我的正常查询中?我从来没有和他们合作过
    • 好吧,取决于您的查询。如果您按照我的示例,只需将 mydates 表替换为您的表,并将 mydate 和 inc_months 列替换为您的列。
    • 我已经谢谢了,还有一个问题,虽然我有一个这种格式的日期:5-2011 当我使用 addMonth 函数运行我的查询时,我收到这个错误:转换日期和/或时转换失败字符串的时间。如何将其转换为日期?
    猜你喜欢
    • 1970-01-01
    • 2023-02-02
    • 1970-01-01
    • 2019-04-06
    • 1970-01-01
    • 1970-01-01
    • 2013-01-07
    • 1970-01-01
    • 2016-03-27
    相关资源
    最近更新 更多