Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

sql CASE statment with COUNT function

Posted on 2009-06-29
8
Medium Priority
?
1,140 Views
Last Modified: 2012-05-07
Hi,

I want to know how to write the query to run efficiently.
In english:
if the count is
0, return just the count (zero)
else, return the formatted string

I believe I'm calling the count multiple times and I want to see if there's a better way to do this.

Thanks
Arun
SELECT 
      CASE Count(ItemID)
		WHEN 0 THEN '0'
		ELSE '<a href="link here/" + Count(ItemID) + ">here</a>"'
FROM ItemMaster

Open in new window

0
Comment
Question by:nmarun
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2
  • 2
  • +1
8 Comments
 
LVL 60

Accepted Solution

by:
chapmandew earned 600 total points
ID: 24737890
Its fine, but here is a different way to do it

SELECT
CASE WHEN counter = 0 then '0' else '<a href="link here/" + cast(counter as vachar(5)) + ">here</a>"'
FROM
(
SELECT
COUNTER = Count(ItemID)
FROM ItemMaster
) a
0
 
LVL 17

Assisted Solution

by:pssandhu
pssandhu earned 200 total points
ID: 24737902
SELECT CASE WHEN Count(ItemID) = 0 THEN '0'  
                             ELSE '<a href="link here/" + Count(ItemID) + ">here</a>"'
                 END  as ColName
FROM ItemMaster
0
 
LVL 27

Author Comment

by:nmarun
ID: 24737925
chapmandew, are you saying that doing the count(itemId) multiple times will not have a big impact on the query's performance?

pssandhu, thanks for completing my query (i forgot the END part), but you are using the Count(ItemID) multiple times. This is what I want to avoid.
0
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
LVL 60

Expert Comment

by:Kevin Cross
ID: 24737944
The above are correct, just remember to add the close/open ' between the literal link text and count.
SELECT
CASE WHEN counter = 0 then '0' else '<a href="link here/"' + cast(counter as vachar(5)) + '">here</a>"'
FROM
(
SELECT 
COUNTER = Count(ItemID)
FROM ItemMaster
) a

Open in new window

0
 
LVL 60

Assisted Solution

by:chapmandew
chapmandew earned 600 total points
ID: 24737958
No, its not going to affect it much.
0
 
LVL 60

Expert Comment

by:Kevin Cross
ID: 24737959
And yes, you will need the END for the case statement.
0
 
LVL 17

Expert Comment

by:pssandhu
ID: 24737965
Yea, I do not think it going to impact the query performance a lot. However, if you are delaing with lots of tables and records and you are waiting for the query to finish in more than a minute then post your whole query and we look into optimising it.
P.
0
 
LVL 27

Author Closing Comment

by:nmarun
ID: 31598010
Thanks guys
0

Featured Post

Learn Veeam advantages over legacy backup

Every day, more and more legacy backup customers switch to Veeam. Technologies designed for the client-server era cannot restore any IT service running in the hybrid cloud within seconds. Learn top Veeam advantages over legacy backup and get Veeam for the price of your renewal

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

For both online and offline retail, the cross-channel business is the most recent pattern in the B2C trade space.
One of the most important things in an application is the query performance. This article intends to give you good tips to improve the performance of your queries.
Using examples as well as descriptions, and references to Books Online, show the different Recovery Models available in SQL Server and explain, as well as show how full, differential and transaction log backups are performed
This videos aims to give the viewer a basic demonstration of how a user can query current session information by using the SYS_CONTEXT function

604 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question