Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/sql/84.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 基于另一个表中的INT在SELECT中创建行_Sql_Sql Server_Tsql_Sql Server 2008_Recursive Query - Fatal编程技术网

Sql 基于另一个表中的INT在SELECT中创建行

Sql 基于另一个表中的INT在SELECT中创建行,sql,sql-server,tsql,sql-server-2008,recursive-query,Sql,Sql Server,Tsql,Sql Server 2008,Recursive Query,假设我有一个名为“contracts”的表,其中有一列“NumYears” 例如 我有另一个表“contract_reviews”,它可以通过ClientId加入到合同中 每年要求用户审查存档的信息,完成后在“合同审查”中插入新行 SELECT ClientId, Reviewed, YearFor, OtherColumn FROM Contract_Reviews ClientId Reviewed YearFor OtherColumn -----

假设我有一个名为“contracts”的表,其中有一列“NumYears”

例如

我有另一个表“contract_reviews”,它可以通过ClientId加入到合同中

每年要求用户审查存档的信息,完成后在“合同审查”中插入新行

SELECT
    ClientId,
    Reviewed,
    YearFor,
    OtherColumn
FROM Contract_Reviews

ClientId  Reviewed    YearFor OtherColumn
--------- ----------- ------- -----------
123456789 1           2018    '£100'
123456789 1           2019    '£100'
客户“123456789”可能在第二年,因此“合同审查”将只保留2条记录。到“努迈耶节”结束时,我们预计会有5个

基本上,我需要从“contract_reviews”中选择所有行,并为缺失的“contract_reviews”生成新的空行,直到“NumYears”计数

ClientId  Reviewed    YearFor OtherColumn
--------- ----------- ------- -----------
123456789 1           2018    '£100'
123456789 1           2019    '£100'
123456789 0           NULL    NULL
123456789 0           NULL    NULL
123456789 0           NULL    NULL

这个问题提供了一些帮助,但我仍然没有找到解决方案。

一个选项是使用递归查询:

with cte as (
    select 
        c.clientId, 
        c.numYears - (
            select count(*) from contract_reviews cr where cr.clientId = c.clientId
        ) numYears
    from contracts c
    union all
    select clientId, numYears - 1 from cte where numYears > 1
)
insert into contract_reviews(clientId) 
select clientId from cte
递归cte的锚点计算每个客户的审阅表中缺少多少行,然后递归部分生成这些行。最后,外部查询执行插入操作

-执行查询后,审核表的内容为:

ClientId | Reviewed | YearFor | OtherColumn :-------- | :------- | :------ | :---------- 123456789 | 1 | 2018 | £100 123456789 | 1 | 2019 | £100 123456789 | null | null | null 123456789 | null | null | null 123456789 | null | null | null 987654321 | null | null | null 987654321 | null | null | null 987654321 | null | null | null
一个选项是使用递归查询:

with cte as (
    select 
        c.clientId, 
        c.numYears - (
            select count(*) from contract_reviews cr where cr.clientId = c.clientId
        ) numYears
    from contracts c
    union all
    select clientId, numYears - 1 from cte where numYears > 1
)
insert into contract_reviews(clientId) 
select clientId from cte
递归cte的锚点计算每个客户的审阅表中缺少多少行,然后递归部分生成这些行。最后,外部查询执行插入操作

-执行查询后,审核表的内容为:

ClientId | Reviewed | YearFor | OtherColumn :-------- | :------- | :------ | :---------- 123456789 | 1 | 2018 | £100 123456789 | 1 | 2019 | £100 123456789 | null | null | null 123456789 | null | null | null 123456789 | null | null | null 987654321 | null | null | null 987654321 | null | null | null 987654321 | null | null | null
如果给定合同的评审表中还没有行呢?好问题!我没有考虑过,谢谢。我认为出于报告的目的,它应该返回X numyers的空行,因为如果我们没有失败的记录,那么在创建合同时应该插入第一条记录。如果给定合同的评审表中还没有行呢?好问题!我没有考虑过,谢谢。我认为出于报告的目的,它应该返回X个空行,因为如果没有记录,我们就失败了,因为在创建合同时应该插入第一条记录。