INSERT INTO dbo.SummerCourse(AgeMax, AgeMin, CashDiscount, EndCourse, InitCourse, Link,
Name, Photo, SchoolId, ThematicId, SalesforceId)
SELECT 16, 6, '2019-07-02 00:00:00.0000000', '2019-07-27 00:00:00.0000000',
'2018-07-02 00:00:00.0000000', 'ww.link.com', S.Nombre_Convocatoria,
'img-little.jpg', S.IdCentro, 4, '01s4E00001222AQQAY'
FROM dbo.AuxTab S
WHERE S.Nombre_Convocatoria NOT IN (SELECT Name FROM dbo.SummerCourse);
我想从aux db插入,只是第一次出现s.Nombre_Convocatoria,现在第一次脚本添加17行(每个名称多次),第二次0但是应该只添加7第一次排,第二次排0。 谢谢
实施例 SummerCourse有列名和更多,auxtab有列(nombre convocatoria)以及更多我想只添加第一次名称在auxtab中 "佩佩" "路易斯" "路易斯" "路易斯" "添" "添" 所以SummerCourse应该只有Pepe,Luis,Tim行
答案 0 :(得分:1)
使用ROW_NUMBER()
每个名称只需1个。
;WITH NameRanking AS
(
SELECT
S.*,
NameRanking = ROW_NUMBER() OVER (PARTITION BY S.Nombre_Convocatoria ORDER BY (SELECT NULL)) -- Your ordering criteria here
FROM
dbo.AuxTab AS S
)
INSERT INTO dbo.SummerCourse(AgeMax, AgeMin, CashDiscount, EndCourse, InitCourse, Link,
Name, Photo, SchoolId, ThematicId, SalesforceId)
SELECT 16, 6, '2019-07-02 00:00:00.0000000', '2019-07-27 00:00:00.0000000',
'2018-07-02 00:00:00.0000000', 'ww.link.com', S.Nombre_Convocatoria,
'img-little.jpg', S.IdCentro, 4, '01s4E00001222AQQAY'
FROM NameRanking S
WHERE
S.Nombre_Convocatoria NOT IN (SELECT Name FROM dbo.SummerCourse) AND
S.NameRanking = 1
如果您需要为每个名称插入特定行,请修改ORDER BY
中的OVER()
以确定哪一行先行。
答案 1 :(得分:0)
考虑到S.IdCentro是int并且需要取最小值。
INSERT INTO dbo.SummerCourse(AgeMax, AgeMin, CashDiscount, EndCourse, InitCourse, Link,Name, Photo, SchoolId, ThematicId, SalesforceId)
SELECT 16, 6, '2019-07-02 00:00:00.0000000', '2019-07-27 00:00:00.0000000','2018-07-02 00:00:00.0000000', 'ww.link.com', S.Nombre_Convocatoria,'img-little.jpg', MIN(S.IdCentro), 4, '01s4E00001222AQQAY'
FROM dbo.AuxTab S
Group By S.Nombre_Convocatoria
Having S.Nombre_Convocatoria
NOT IN (SELECT Name FROM dbo.SummerCourse);