如何在单个语句中编写查询3语句

时间:2015-01-09 03:47:37

标签: sql sql-server

如何使用单个选择查询在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#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

1 个答案:

答案 0 :(得分:6)

您基本上只需要聚合函数和条件聚合:

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中使用。