TSQL:将缺少的记录插入表中

时间:2016-01-18 06:13:09

标签: tsql

我被困在这个T-SQL查询中。 我有下表

 Age   SectioName  Cost 
 ---------------------
 1      Section1    100
 2      Section1    200
 1      Section2    500
 3      Section2    100
 4      Section2    200

让我们说每个部分我最多可以有5岁。在上表中有一些遗失的年龄。如何为每个部分插入缺失的年龄。 (可能没有使用光标)。缺少年龄

的成本为零

因此,在插入后,表格看起来应该是

 Age SectioName  Cost 
 ---------------------
  1    Section1    100
  2    Section1    200
  3    Section1    0
  4    Section1    0
  5    Section1    0
  1    Section2    500  
  2    Section2    0
  3    Section2    100
  4    Section2    200
  5    Section2    0

EDIT1 我的问题应该更清楚了。最大年龄是动态值。它可能是5,6,10或其他值,但它总是小于25。

3 个答案:

答案 0 :(得分:2)

我想我明白了

;WITH tally AS
    (
            SELECT 1 AS r
            UNION ALL
            SELECT r + 1 AS r
            FROM  tally
            WHERE r < 5 -- this value could be dynamic now
    )
select n.r, t.SectionName, 0 as Cost
from (select distinct SectionName from TempFormsSectionValues) t
cross join
(select ta.r FROM tally ta) n
where not exists
  (select * from TempFormsSectionValues where YearsAgo = n.r and SectionName = t.SectionName)
order by t.SectionName, n.r

答案 1 :(得分:1)

您可以使用此查询选择缺失值:

select n.num, t.SectioName, 0 as Cost
from (select distinct SectioName from table1) t
cross join
(select 1 as num union select 2 union select 3 union select 4 union select 5) n
where not exists
  (select * from table1 where table1.age = n.num and table1.SectioName = t.SectioName)

它创建了截面和数字1到5的笛卡尔积,然后选择那些尚不存在的那些。然后,您可以将此查询用作表格insert into的来源。

SQL Fiddle(已添加order by以更轻松地检查结果,但无需插入)。

答案 2 :(得分:0)

使用以下查询生成缺失的行

SELECT t1.Age,t1.Section,ISNULL(t2.Cost,0) as Cost
FROM
(
SELECT 1 as Age,'Section1' as Section,0 as Cost
UNION
SELECT 2,'Section1',0
UNION
SELECT 3,'Section1',0
UNION
SELECT 4,'Section1',0
UNION
SELECT 5,'Section1',0
UNION
SELECT 1,'Section2',0
UNION
SELECT 2,'Section2',0
UNION
SELECT 3,'Section2',0
UNION
SELECT 4,'Section2',0
UNION
SELECT 5,'Section2',0
  ) as t1

 LEFT JOIN test t2
 ON t1.Age=t2.Age AND t1.Section=t2.Section
 ORDER BY Section,Age

SQL Fiddle

您可以使用 EXCEPT 运算符将以上结果集用于插入缺失的行,以排除表中已存在的行 -

INSERT INTO test
SELECT t1.Age,t1.Section,ISNULL(t2.Cost,0) as Cost
FROM
(
SELECT 1 as Age,'Section1' as Section,0 as Cost
UNION
SELECT 2,'Section1',0
UNION
SELECT 3,'Section1',0
UNION
SELECT 4,'Section1',0
UNION
SELECT 5,'Section1',0
UNION
SELECT 1,'Section2',0
UNION
SELECT 2,'Section2',0
UNION
SELECT 3,'Section2',0
UNION
SELECT 4,'Section2',0
UNION
SELECT 5,'Section2',0
  ) as t1

 LEFT JOIN test t2
 ON t1.Age=t2.Age AND t1.Section=t2.Section

EXCEPT 

SELECT Age,Section,Cost
FROM test

SELECT * FROM test
ORDER BY Section,Age

http://www.sqlfiddle.com/#!3/d9035/11