satyam sinha
satyam sinha

Reputation: 11

How to write query 3 statements in a single statement

How can I use a single select query to execute the below 3 query in sql?

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

Upvotes: 1

Views: 152

Answers (1)

Gordon Linoff
Gordon Linoff

Reputation: 1271231

You basically just want aggregation functions and conditional aggregation:

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');

Note that this is pretty much standard SQL (except for the column names), so it will work in both MySQL and SQL Server.

Upvotes: 6

Related Questions