sql - SQL将查询合二为一
问题描述
我有几个插入查询:
insert into table2 (age, name)
select '0-19', 'abc'
where not exists (select 1 from table2 where age is null and name is null);
insert into table2 (age, name)
select '20-29', 'zxy'
where not exists (select 1 from table2 where age not in ( '0-19') and name is null);
insert into table2 (age, name)
select '30-39', 'egt'
where not exists (select 1 from table2 where age not in ( '0-19', '20-29') and name is null);
insert into table2 (age, name)
select '40-49', 'aaa'
where not exists (select 1 from table2 where age not in ( '0-19', '20-29', '30-39') and name is null);
insert into table2 (age, name)
select '50-59', 'rtg'
where not exists (select 1 from table2 where age not in ( '0-19', '20-29', '30-39', '40-49') and name is null);
insert into table2 (age, name)
select '60+', 'ghg'
where not exists (select 1 from table2 where age not in ( '0-19', '20-29', '30-39', '40-49', '50-59') and name is null);
如果满足相关条件,我想插入数据。你可以看到他们分开查询。将这些查询重写为一个查询。谢谢
我可以尝试与任何人进行测试,无论nvarchar
或integer
.
解决方案
不确定以下是否有帮助。
SELECT
CASE WHEN Age IN ("00-19")
THEN ("0-19")
ELSE ("NULL")
END AS Age,
CASE WHEN Age IN ("20-29")
THEN ("20-29")
ELSE ("NULL")
END AS Age,
CASE WHEN Age IN ("30-39")
THEN ("30-39")
ELSE ("NULL")
END AS Age,
CASE WHEN Age IN ("40-49")
THEN ("40-49")
ELSE ("NULL")
END AS Age,
CASE WHEN Age IN ("50-59")
THEN ("50-59")
ELSE ("NULL")
END AS Age,
CASE WHEN Age IN ("60-69")
THEN ("60-69")
ELSE ("NULL")
END AS Age
FROM table2
不知道上面有没有帮助。会鼓励你解释你正在尝试做什么,然后每个人都可以提供帮助。
推荐阅读
- java - JWPlayer 无效的许可证密钥错误代码 (100012)
- angular - ViewChild('inputState', { 读取:MatAutocompleteTrigger})
- javascript - 我的键盘事件没有触发,我不知道为什么
- core-data - Swift UI - EnvironmentObject 更改不会更新 UI
- java - 使用两个线程时如何在每次更改后打印值
- python - Python按行写入csv
- c# - 使用 `function().Wait()` 调用异步函数有效,但 `async function()` 崩溃
- java - Spring data jdbc - 具体的join,具体的映射
- powershell - Powershell - 登录/注销事件 - Get-WinEvent 与 Get-EventLog
- c# - double 或 float 的赋值和使用在 C# 中不起作用