根据有效日期和有效日期创建新行

时间:2019-04-10 08:09:34

标签: tsql

我有一个表,该表具有帐号,月末有效期和月末有效期至各列。 我需要的是一个具有帐号的表,该表具有该帐户启用时的所有月底日期,包括有效的月底。 The Current Table looks like this

New table will need to be like this

我尝试使用日历表和CTE表类型查询,但没有成功。

任何帮助都会很棒。

3 个答案:

答案 0 :(得分:0)

基于msdn线程how to get month end date between two dates.上的sql

//This is your fetched html string
let stringHTML = "<div> <h2>Some data</h2><div class='myDiv'> <h1> Test </h1> </div> </div>";
//Create temp element holder
let tempElement = document.createElement("div");
//Add fetched HTML to tempElement
tempElement.innerHTML = stringHTML;

//Now you can query it as it was a normal DOM element
let elementsWithClass = tempElement.getElementsByClassName("myDiv")

document.getElementById('root').appendChild(elementsWithClass[0]);

编辑:或者没有光标

<div id='root'> 

</div>

答案 1 :(得分:0)

这应该有效。

    ;WITH Span AS (

        SELECT 
            AccountNumber,
            ValidFrom AS Valid

        FROM dbo.Input
        UNION ALL
        SELECT 
            AccountNumber,
            DATEADD(DAY, 1, Span.Valid) AS Valid
        FROM Span
            WHERE DATEADD(DAY, 1, Span.Valid) <= (SELECT ValidTo FROM dbo.Input WHERE AccountNumber = Span.AccountNumber)
    )
    SELECT * FROM Span
    ORDER BY Span.AccountNumber, Span.Valid
    OPTION (MAXRECURSION 0);

答案 2 :(得分:0)

可以使用 在语句中使用多个逗号分隔的CTE

查询

with t0 (i) AS (select 0 union all select 0 union all select 0 union all select 0 union all select 0 union all select 0),
     t1 (i) AS (select 0 from t0 a inner join t0 b on a.i = b.i), 
     n (i) AS (select row_number()over(order by i)  from t1),
     Account_details (Account_number,valid_from,valid_to,mth,Live_date)As(

     select  Account_number,valid_from,valid_to,   datediff(month,valid_from,valid_to ) mth, valid_from"Live_date" 
     from tbl1
     union all
     select  Account_number,valid_from,valid_to,   datediff(month,valid_from,valid_to ) mth, EOMONTH (dateadd(month,n.i,valid_from)) "Live_date" 
     from tbl1
     inner join n on 1=1 and n.i between 1 and datediff(month,valid_from,valid_to ) 
     )
select * 
from Account_details
where Account_details.Account_number =1
order by  Account_details.Account_number

输出

enter image description here

CTE表t0,t1和n将生成数字。这是生成没有任何数据的行的最佳方法。

然后使用CTE表Account_details从表中提取数据。