Select 来自 table 的最大值并在另一个中递增
Select max value from a table and increment it in another
我正在为一个 sql 查询而苦苦挣扎,如果有任何帮助,我将不胜感激。
我有两个 table,它们都有一个排序列。第一个看起来像这样:
person_id
image_name
sort_number
739
chest.png
1
739
legs.png
2
第二个table像这样
person_id
advert
sort_number
739
house.png
1
739
car.png
2
我希望能够 select 来自 table1 的最大 sort_number 并在 table2 [=30] 中制作第一个 sort_number =](house.png)变为3,而sort_number为car.png)变为4.
本质上,我想要实现的是 selects 来自 table2 并插入 table1 的插入语句,但我需要 sort_number 没有重复,所以从 table2 开始的 sort_number 应该是 table1+1 的最大值...等等。如果 table1 没有人,我只是插入而不更改 sort_number 值。
如果有人可以帮助我,我将不胜感激。
这是一种方法:
With grouped as
(Select person_id, max(sort_num) as maxsort
From table1
Group by person_id)
Select t2.person_id, t2.advert, t2.sort_num + coalesce(g.maxsort,0) as newsortnum
From table t2
Left join grouped g on t2.person_id = g.person_id
这将获取第一个 table 中每个键的排序编号的最大值,然后尝试将第二个 table 加入此分组数据集。如果匹配,则将第二个 table 的值添加到最大值,否则保留第二个 table 的值。
您可以尝试使用 UNION ALL
和 ROW_NUMBER
:
WITH CTE AS
(
SELECT
person_id,
image_name,
sort_number,
1 sort_table
FROM dbo.Table1 t1
UNION ALL
SELECT
person_id,
advert,
sort_number,
2
FROM dbo.Table2 t2
)
SELECT
person_id,
image_name,
ROW_NUMBER() OVER(PARTITION BY person_id ORDER BY sort_table, sort_number) sort_number
FROM CTE
;
insert into table2(person_id, advert, sort_number)
select table1.person_id, table1.image_name, table1.sort_number + table2.sort_number
from table1
join table2
on 1 = 1
left join table2 newer
on table2.sort_number < newer.sort_number
left join table1 mismatch
on table2.person_id = mistmatch.person_id and table2.advert = mismatch.image_name
where newer.person_id is null and mismatch.person_id is null
第一次加入:我们需要配对表 1 和表 2
第二次连接:我们确保对中的 table2 记录是最新的
第三次连接:我们确保不插入已经存在的内容。
我正在为一个 sql 查询而苦苦挣扎,如果有任何帮助,我将不胜感激。
我有两个 table,它们都有一个排序列。第一个看起来像这样:
person_id | image_name | sort_number |
---|---|---|
739 | chest.png | 1 |
739 | legs.png | 2 |
第二个table像这样
person_id | advert | sort_number |
---|---|---|
739 | house.png | 1 |
739 | car.png | 2 |
我希望能够 select 来自 table1 的最大 sort_number 并在 table2 [=30] 中制作第一个 sort_number =](house.png)变为3,而sort_number为car.png)变为4.
本质上,我想要实现的是 selects 来自 table2 并插入 table1 的插入语句,但我需要 sort_number 没有重复,所以从 table2 开始的 sort_number 应该是 table1+1 的最大值...等等。如果 table1 没有人,我只是插入而不更改 sort_number 值。
如果有人可以帮助我,我将不胜感激。
这是一种方法:
With grouped as
(Select person_id, max(sort_num) as maxsort
From table1
Group by person_id)
Select t2.person_id, t2.advert, t2.sort_num + coalesce(g.maxsort,0) as newsortnum
From table t2
Left join grouped g on t2.person_id = g.person_id
这将获取第一个 table 中每个键的排序编号的最大值,然后尝试将第二个 table 加入此分组数据集。如果匹配,则将第二个 table 的值添加到最大值,否则保留第二个 table 的值。
您可以尝试使用 UNION ALL
和 ROW_NUMBER
:
WITH CTE AS
(
SELECT
person_id,
image_name,
sort_number,
1 sort_table
FROM dbo.Table1 t1
UNION ALL
SELECT
person_id,
advert,
sort_number,
2
FROM dbo.Table2 t2
)
SELECT
person_id,
image_name,
ROW_NUMBER() OVER(PARTITION BY person_id ORDER BY sort_table, sort_number) sort_number
FROM CTE
;
insert into table2(person_id, advert, sort_number)
select table1.person_id, table1.image_name, table1.sort_number + table2.sort_number
from table1
join table2
on 1 = 1
left join table2 newer
on table2.sort_number < newer.sort_number
left join table1 mismatch
on table2.person_id = mistmatch.person_id and table2.advert = mismatch.image_name
where newer.person_id is null and mismatch.person_id is null
第一次加入:我们需要配对表 1 和表 2
第二次连接:我们确保对中的 table2 记录是最新的
第三次连接:我们确保不插入已经存在的内容。