【问题标题】:Is it possible to combine multiple SQL statements together?是否可以将多个 SQL 语句组合在一起?
【发布时间】:2023-03-16 00:50:01
【问题描述】:

我正在测试表中的每一列以查看它是否具有空值。 目前,我正在使用:

select count (column) when column is not null

我为每列列出了 20 个这样的列表。我可以将它们全部组合起来运行一次,然后告诉我每列的记录数。任何有 0 的列都是空列

【问题讨论】:

  • 很可能是的,但请发布您目前拥有的代码示例以及您希望它们最终的样子(显示您迄今为止尝试过的内容)。
  • 你真的想知道它是否可能吗?或者你想解决你的问题?是的,有可能。但是,如果您需要解决方案方面的帮助,请发布示例数据、预期结果(全部为格式化文本或 DDL/DML 语句)以及您迄今为止的尝试。
  • 这似乎与您之前的问题重复?如果您没有得到答案,请改进您现有的问题 - 不要再问另一个问题。
  • 我也不认为 SQL Server 与 Amazon Redshift 是一回事。所以请更正你的标签。
  • "任何有 0 的列都是空列" 您是在搜索 null 值,还是在搜索 null 和 0?

标签: sql sql-server validation amazon-redshift qa


【解决方案1】:

where 子句是多余的。您可以将所有内容添加到单个查询中:

select count(column1),
       count(column2),
       . . .
       count(column20)      
from t

【讨论】:

  • 当然!我给出了一个多行批处理查询,但这更简单、更快。
【解决方案2】:

您可以使用filter 为每一列创建一个count。虽然 filter 是 SQL 标准,但 SQL Server 不支持它。你可以用一个案例来模拟它。

select
  count(case when a is null then 1 end) a,
  count(case when b is null then 1 end) b,
  count(case when c is null then 1 end) c
from foo;

Try it

【讨论】:

    【解决方案3】:

    SQL Server 允许批量查询。例如,这将在 Management Studio 中工作:

    select count(field1) from sample_table;
    select count(field2) from sample_table;
    select count(field3) from sample_table;
    

    您的结果可能类似于

    (No column name)
    -----
    1093
    
    (No column name)
    -----
    470
    
    (No column name)
    -----
    1099
    

    如果您愿意,您也可以在客户端应用程序中进行批量查询,但目前尚不清楚您的问题的意图。

    【讨论】:

      猜你喜欢
      • 2022-01-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-01-07
      • 2013-07-06
      • 1970-01-01
      相关资源
      最近更新 更多