如何将这四个请求合二为一?

Posted

技术标签:

【中文标题】如何将这四个请求合二为一?【英文标题】:How to combine these four requests into one? 【发布时间】:2018-07-04 07:50:06 【问题描述】:

Select COUNT(Table1.statusID) as result1 from Table1 where Table1.emplDesignerID = 1135 and Table1.cancelled = 0
Select COUNT(Table1.statusID) as result2 from Table1 where Table1.emplDesignerID = 26 and Table1.cancelled = 0
Select COUNT(Table1.statusID) as result3 from Table1 where Table1.emplDesignerID = 1086 and Table1.cancelled = 0
Select COUNT(Table1.statusID) as result4 from Table1 where Table1.emplDesignerID = 1089 and Table1.cancelled = 0

我正在使用 MS SQL Server。

【问题讨论】:

您是在寻找 UNION 还是 IN 语句?? 你想要这些计数的总和吗? 不,我不想得到一笔款项 @Gennadyi 你用的是哪个数据库? 【参考方案1】:

SUMCASE 可以在这里有效地组合:

Select
    SUM(CASE WHEN Table1.emplDesignerID = 1135 THEN 1 ELSE 0 END) as result1,
    SUM(CASE WHEN Table1.emplDesignerID = 26 THEN 1 ELSE 0 END) as result2,
    SUM(CASE WHEN Table1.emplDesignerID = 1086 THEN 1 ELSE 0 END) as result3,
    SUM(CASE WHEN Table1.emplDesignerID = 1089 THEN 1 ELSE 0 END) as result4
from Table1
where Table1.cancelled = 0
and Table1.emplDesignerID in (1135,26,1086,1089)

根据使用情况,您可能会或可能不会觉得WHERE 子句中需要IN 过滤器。还有其他可能的转换(在SELECT 子句中有emplDesignerIDGROUP ing),这可能会使它更灵活。

【讨论】:

如果没有人告诉我他们认为什么使它没有用,我无法有效地编辑这个答案。 @Damien_The_Unbeliever 我认为最初的问题是从不同的表(4 个不同的表)中提取的。请稍作修改,以便我恢复投票。【参考方案2】:

您可以简单地使用子查询并将它们组合成一个更大的查询。

select
(
Select COUNT(Table1.statusID) as result1 from Table1 where Table1.emplDesignerID = 1135 and Table1.cancelled = 0
) as result1, 
(
Select COUNT(Table1.statusID) as result2 from Costs where Table1.emplDesignerID = 26 and Table1.cancelled = 0
) as result2,
(
Select COUNT(Table1.statusID) as result3 from Costs where Table1.emplDesignerID = 1086 and Table1.cancelled = 0
) as result3,
(
Select COUNT(Table1.statusID) as result4 from Costs where Table1.emplDesignerID = 1089 and Table1.cancelled = 0
) as result4;

如果您使用的是 Oracle,则必须添加 from dual

其他一些数据库如 MS SQL 不需要它。

更新 - SQL-92 合规性

Oracle 提供了双表,因此可以添加额外的from dual。 MS SQL 提供了在没有 from 语句的情况下进行查询的能力。

见SELECT from nothing?

或者让我们使用Values 功能:

select ( Select COUNT(Table1.statusID) as result1 from Table1 where Table1.emplDesignerID = 1135 and Table1.cancelled = 0 ) as result1, ( Select COUNT(Table1.statusID) as result2 from Costs where Table1.emplDesignerID = 26 and Table1.cancelled = 0 ) as result2, ( Select COUNT(Table1.statusID) as result3 from Costs where Table1.emplDesignerID = 1086 and Table1.cancelled = 0 ) as result3, ( Select COUNT(Table1.statusID) as result4 from Costs where Table1.emplDesignerID = 1089 and Table1.cancelled = 0 ) as result4 from (VALUES ('dummyValue'));

【讨论】:

我不确定这是不是有效的 SQL 取决于数据库。 那里没有 dbms。无论如何它都不是有效的 ANSI SQL。 几乎符合 ANSI SQL。添加括号:... from (VALUES ('dummyValue')); 也可以在这里查看@SQL 答案...:***.com/questions/3732422/select-from-nothing@jarlh【参考方案3】:

如果 count() 是您需要的唯一结果,您可以使用 UNION :

Select COUNT(Table1.statusID) as result1 from Table1 where Table1.emplDesignerID = 1135 and Table1.cancelled = 0
UNION 
Select COUNT(Table1.statusID) as result2 from Table1 where Table1.emplDesignerID = 26 and Table1.cancelled = 0
UNION 
Select COUNT(Table1.statusID) as result3 from Table1 where Table1.emplDesignerID = 1086 and Table1.cancelled = 0
UNION 
Select COUNT(Table1.statusID) as result4 from Table1 where Table1.emplDesignerID = 1089 and Table1.cancelled = 0

它很容易阅读,但不是更有效

【讨论】:

由于UNION 使用第一个查询中的列名生成一个结果集,这将产生一个在result1 列中包含4 行的结果,并且无法确定哪个结果与哪个计数相关. 由于 UNION 删除了重复项,因此可以返回 1 到 4 行。改为 UNION ALL。

以上是关于如何将这四个请求合二为一?的主要内容,如果未能解决你的问题,请参考以下文章

函数——函数接收四个参数分别是:姓名,性别,年龄,学历。用户通过输入这四个内容,然后将这四个内容传入到函数中, 此函数接收到这四个内容,将内容追加到一个student msg文件中

如何将这 2 个 SQL 命令合二为一?

如何将这两条 SQL 语句合并为一条?

内存分配失败:如何将四个结果集合并到一张表中

如何将这两个查询组合成一个查询?

如何创建共享文件夹?需要这四个方面