Sql 如何在一条语句中编写查询3语句
如何使用单个select查询在sql中执行以下3个查询Sql 如何在一条语句中编写查询3语句,sql,sql-server,Sql,Sql Server,如何使用单个select查询在sql中执行以下3个查询 select COUNT(*) from Product with(nolock) where ProductNumber in ('704021872', '704021871', '704021870') select COUNT(*) from Product with(nolock) where ProductNumber in ('704021872', '704021871', '704021870') and column#
select COUNT(*)
from Product with(nolock)
where ProductNumber in ('704021872',
'704021871',
'704021870')
select COUNT(*)
from Product with(nolock)
where ProductNumber in ('704021872',
'704021871',
'704021870')
and column#3 is not null
select min(column#2), MAX(column#3) from Product
where ProductNumber in ('704021872',
'704021871',
'704021870')
and column#3 is not null
您基本上只需要聚合函数和条件聚合:
select COUNT(*),
COUNT(column#3),
min(case when column#3 is not null then column#2 end), MAX(column#3)
from Product with(nolock)
where ProductNumber in ('704021872', '704021871', '704021870');
请注意,除了列名之外,这几乎是标准的SQL,因此它可以在MySQL和SQL Server中工作。您基本上只需要聚合函数和条件聚合:
select COUNT(*),
COUNT(column#3),
min(case when column#3 is not null then column#2 end), MAX(column#3)
from Product with(nolock)
where ProductNumber in ('704021872', '704021871', '704021870');
请注意,除了列名之外,这几乎是标准的SQL,因此它在MySQL和SQL Server中都可以使用。请使用{}工具栏按钮或每行缩进4个空格来格式化代码片段。MySQL和SQL Server之间存在差异,您使用的代码片段看起来像SQL Server,尽管如此。请使用{}工具栏按钮或每行缩进4个空格来格式化代码段。MySQL和SQL Server之间有区别,您使用的代码段看起来像SQL Server。