Is there a efficient way to write this query?
SELECT CASE
WHEN Population BETWEEN 0 AND 100 THEN '0-100' WHEN Population BETWEEN 101 AND 1000 THEN '101-1000' ELSE 'Greater than 1000' END AS Population_Range,
COUNT(CASE WHEN Population BETWEEN 0 AND 100 THEN '0-100' WHEN Population BETWEEN 101 AND 1000 THEN '101-1000' ELSE 'Greater than 1000' END) AS [No. Of Countries]
FROM Country
GROUP BY
CASE WHEN Population BETWEEN 0 AND 100 THEN '0-100' WHEN Population BETWEEN 101 AND 1000 THEN '101-1000' ELSE 'Greater than 1000' ENDThe CASE statement embedded in you COUNT function is unnecessary:
SELECT CASE
WHEN Population BETWEEN 0 AND 100 THEN '0-100'
WHEN Population BETWEEN 101 AND 1000 THEN '101-1000'
ELSE 'Greater than 1000' END AS Population_Range,
COUNT(*) AS [No. Of Countries]
FROM Country
GROUP BY CASE
WHEN Population BETWEEN 0 AND 100 THEN '0-100'
WHEN Population BETWEEN 101 AND 1000 THEN '101-1000'
ELSE 'Greater than 1000' END
Showing posts with label case. Show all posts
Showing posts with label case. Show all posts
Wednesday, March 21, 2012
Rewrite a query efficiently
Labels:
0-100,
case,
database,
efficient,
efficiently,
microsoft,
mysql,
oracle,
population,
query,
queryselect,
rewrite,
server,
sql,
write
Wednesday, March 7, 2012
Returning two values from Case statement
I want to write a query that looks something like this. I get an error
running this:
select id_1 =
case [ID]
WHEN 1 then 'test' , 'test2' -- error
end
from myTable where id = 1
That way, I don't have to run the same query twice. I'll then insert those
two values in another table.
Thanks.use two case functions, one for each column
select id_1 = case [ID]
WHEN 1 then 'test'
end
,select id_2 = case [ID]
WHEN 1 then 'test2'
end
from myTable where id = 1
"VMI" <VMI@.discussions.microsoft.com> wrote in message
news:9226AEED-DE32-4555-803F-356992ED22D8@.microsoft.com...
> I want to write a query that looks something like this. I get an error
> running this:
> select id_1 =
> case [ID]
> WHEN 1 then 'test' , 'test2' -- error
> end
> from myTable where id = 1
> That way, I don't have to run the same query twice. I'll then insert those
> two values in another table.
> Thanks.|||Can you give the actual requirement?
For your query, the case statement is redundant.
"VMI" wrote:
> I want to write a query that looks something like this. I get an error
> running this:
> select id_1 =
> case [ID]
> WHEN 1 then 'test' , 'test2' -- error
> end
> from myTable where id = 1
> That way, I don't have to run the same query twice. I'll then insert those
> two values in another table.
> Thanks.
running this:
select id_1 =
case [ID]
WHEN 1 then 'test' , 'test2' -- error
end
from myTable where id = 1
That way, I don't have to run the same query twice. I'll then insert those
two values in another table.
Thanks.use two case functions, one for each column
select id_1 = case [ID]
WHEN 1 then 'test'
end
,select id_2 = case [ID]
WHEN 1 then 'test2'
end
from myTable where id = 1
"VMI" <VMI@.discussions.microsoft.com> wrote in message
news:9226AEED-DE32-4555-803F-356992ED22D8@.microsoft.com...
> I want to write a query that looks something like this. I get an error
> running this:
> select id_1 =
> case [ID]
> WHEN 1 then 'test' , 'test2' -- error
> end
> from myTable where id = 1
> That way, I don't have to run the same query twice. I'll then insert those
> two values in another table.
> Thanks.|||Can you give the actual requirement?
For your query, the case statement is redundant.
"VMI" wrote:
> I want to write a query that looks something like this. I get an error
> running this:
> select id_1 =
> case [ID]
> WHEN 1 then 'test' , 'test2' -- error
> end
> from myTable where id = 1
> That way, I don't have to run the same query twice. I'll then insert those
> two values in another table.
> Thanks.
Subscribe to:
Posts (Atom)