【问题标题】:SQL combine query into oneSQL将查询合二为一
【发布时间】:2020-06-16 18:15:10
【问题描述】:

我有几个插入查询:

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,我都可以尝试与任何可以修改这些查询的人进行测试。

【问题讨论】:

  • 请解释您要做什么。这些查询对我来说真的没有意义。
  • 如果满足条件我想插入数据。你可以看到他们单独的查询。将这些查询重写为一个查询。
  • 抱歉,存储整数值(age 列的整数值)不是一个好主意。
  • 所有 nvarchar 不是整数
  • "all nvarchar not integer" 更糟糕的是,@qphan。 9 岁的人不 比 75 的人大,但是,'9' 大于 '75'(或者在你的情况下是 N'9' 和N'75' 分别)。然后我们会发现,在存储非 ANSI 字符时存储 nvarchar 也是一种快速存储。

标签: sql sql-server


【解决方案1】:

不确定以下是否有帮助。

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

我不知道以上是否有帮助。鼓励您解释您正在尝试做的事情,然后每个人都可以提供帮助。

【讨论】:

    【解决方案2】:

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

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

    【讨论】:

      猜你喜欢
      • 2014-06-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-11-18
      相关资源
      最近更新 更多