繁体   English   中英

SQL 合二为一

[英]SQL combine query into one

我有几个插入查询:

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);

如果满足相关条件,我想插入数据。 你可以看到他们分开查询。 将这些查询重写为一个查询。 谢谢

无论nvarcharinteger ,我都可以尝试与任何人一起测试可以返工这些查询。

不确定以下是否有帮助。

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

不知道上面有没有帮助。 会鼓励你解释你正在尝试做什么,然后每个人都可以提供帮助。

更新的答案:创建一个表变量来存储您的范围,然后将临时变量连接到要插入缺失值的表中,例如:

-- your Table2
Declare @table2 TABLE (age varchar(10), name varchar(10))

-- a temp variable table to join in order to exclude the duplicates
Declare @data TABLE (age varchar(10), name varchar(10))

    -- add all the values you want to verify existence of
    INSERT INTO @data SELECT '0-19', 'abc'
    INSERT INTO @data SELECT '20-29', 'zxy'
    INSERT INTO @data SELECT '30-39', 'egt'
    INSERT INTO @data SELECT '40-49', 'aaa'
    INSERT INTO @data SELECT '50-59', 'rtg'
    INSERT INTO @data SELECT '60+', 'ghg'

    --  before the insert (all the missing values)
    SELECT d.age, d.[name] 
        FROM @data d 
            LEFT OUTER JOIN @table2 t ON d.age = t.age 
        WHERE t.age IS NULL

    -- do the insert
    INSERT INTO @Table2 
    SELECT d.age, d.[name] 
        FROM @data d 
            LEFT OUTER JOIN @table2 t ON d.age = t.age 
        WHERE t.age IS NULL

    -- this should now be empty
    SELECT d.age, d.[name] 
        FROM @data d 
            LEFT OUTER JOIN @table2 t ON d.age = t.age 
        WHERE t.age IS NULL

版本 1:你能告诉我们存储在表中的数据吗? 对存储为字符串的年龄进行范围搜索可能会像您认为的那样工作。

例如: '0-19'

数据在您的表中存储为“5”还是“0-19”? 如果它存储为“5”,那么正如其他人已经提到的那样,将该数据类型转换为数字数据类型(小 int 或 int)是一个好主意。

这是一个示例,您是否曾经对存储为字符串(varchar)但代表数字的值进行排序? 例如,如果我们有这组值,我们期望排序返回为:

 1,2,3,100,200,300

然而我们得到:

 1,100,2,200,3,300

为了正确排序字符串,您必须向右填充最长的长度。

在这种情况下 3 个字符。

 001 002, 003, 100, 200, 300.

在一个字符串中,每个字符都被评估。

如果您查看 ASCII 表( https://www.ascii-code.com/ ),您就会知道原因。 NULL 领先于数字。

1 = 49 0 0
100 = 49 48 48
2 = 50 0 0
200 = 50 48 48

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM