插入多行不同的列值

时间:2018-01-16 14:58:41

标签: sql sql-server insert

这是我现有的表格

enter image description here

在此表中,每个用户根据其状态拥有各自的数据。每个用户肯定都有状态1。 现在,每个用户都有3个状态存储。 通过插入新行用户复制其状态 1数据,尝试让每个用户拥有3个状态,这样:

  • 用户Ali目前只有状态1及其数据,因此需要插入新的 行状态为2的Ali,并再次沿状态1复制数据 插入具有状态3的新行Ali,并从中复制数据 状态1。
  • 用户John目前只有状态1和2,因此需要插入新的 对状态为3的John进行排序,并沿状态1中的数据进行复制。
  • 与其他用户
  • 继续相同的模式

预期结果:

enter image description here

4 个答案:

答案 0 :(得分:1)

我会使用CROSS JOINNOT EXISTS

with data as 
(
  select name, 
        column1,
        column2
  from your_table
  where status = 1
), cross_join_data as
(
  select d1.name, t.status, d1.column1, d1.column2
  from data d1
  cross join
  (
     select 1 status
     union
     select 2 status
     union
     select 3 status
  ) t
  where not exists (
     select 1
     from your_table d2
     where d2.name = d1.name and
           d2.status = t.status
  )
)
select *
from your_table
union all
select *
from cross_join_data 

dbfiddle demo

答案 1 :(得分:0)

这应该有效

with cte as (
    select
         [Name], coalesce(max(iif([Status]=1, [Column1], null)), max(iif([Status]=2, [Column1], null)), max(iif([Status]=3, [Column1], null))) col1
         , coalesce(max(iif([Status]=1, [Column2], null)), max(iif([Status]=2, [Column2], null)), max(iif([Status]=3, [Column2], null))) col2
    from
        MyTable
    group by [Name]
)

--insert into MyTable
select
    cte.[Name], nums.n, cte.col1, cte.col2
from
    cte
    cross join (values (1),(2),(3)) nums(n)
    left join MyTable on cte.[Name]=MyTable.[Name] and n=MyTable.[Status]
where
    MyTable.[Status] is null

答案 2 :(得分:0)

如果数据不可为空,则此方法有效

declare @table table (name varchar(10), status int, data int);
insert into @table values 
  ('a', 1, 2)
, ('a', 2, 5)
, ('a', 3, 7)
, ('b', 1, 5) 
, ('b', 2, 6)
, ('c', 1, 3) 

select stats.status as statusStats
     , tn.name as nameTN 
     , t.status as statusData, t.name, t.data 
     , ISNULL(t.data, t1.data) as 'fillInData'
from (values (1),(2),(3)) as stats(status)
cross join (select distinct name from @table) tn 
left join @table t 
  on t.status = stats.status 
 and t.name = tn.name 
join @table t1
  on t1.name = tn.name 
 and t1.status = 1 
 order by tn.name, stats.status

答案 3 :(得分:-1)

以下是我要做的事情:

CREATE TABLE #existingtable (Name VARCHAR(50), Status INT, Column1 VARCHAR (10), Column2 VARCHAR(10));

INSERT INTO #existingtable (Name,Status,Column1,Column2) Values('Ali',1,'100','90');
INSERT INTO #existingtable (Name,Status,Column1,Column2) Values('John',1,'20','200');
INSERT INTO #existingtable (Name,Status,Column1,Column2) Values('John',2,'80','90');
INSERT INTO #existingtable (Name,Status,Column1,Column2) Values('Ming',1,'54','345');
INSERT INTO #existingtable (Name,Status,Column1,Column2) Values('Mei',1,'421','123');
INSERT INTO #existingtable (Name,Status,Column1,Column2) Values('Mei',3,'24','344');

SELECT * FROM #existingtable;

WITH CTE (Name,Column1,Column2)
AS 
(   
    SELECT DISTINCT NAME,COLUMN1,COLUMN2
    FROM #existingtable
)
, CTE2 (NAME,Status,Column1,Column2)
AS 
( 
    SELECT NAME,1 AS STATUS,COLUMN1,COLUMN2
    FROM CTE
    UNION
    SELECT NAME,2 AS STATUS,COLUMN1,COLUMN2
    FROM CTE
    UNION
    SELECT NAME,3 AS STATUS,COLUMN1,COLUMN2
    FROM CTE
)

INSERT INTO #existingtable (Name,Status,Column1,Column2)
SELECT C.Name,C.Status,C.Column1,C.Column2 
FROM CTE2 AS C
LEFT JOIN #existingtable AS E
ON C.NAME = E.Name
AND C.Status = E.Status
WHERE E.Status IS NULL

SELECT * FROM #existingtable
ORDER BY Name, status

这有两个编辑。初始编辑为CTE添加了where子句 第二次编辑添加了OP

添加的值