?
Solved

sql CASE statment with COUNT function

Posted on 2009-06-29
8
Medium Priority
?
1,138 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
Get free NFR key for Veeam Availability Suite 9.5

Veeam is happy to provide a free NFR license (1 year, 2 sockets) to all certified IT Pros. The license allows for the non-production use of Veeam Availability Suite v9.5 in your home lab, without any feature limitations. It works for both VMware and Hyper-V environments

 
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

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Why is this different from all of the other step by step guides?  Because I make a living as a DBA and not as a writer and I lived through this experience. Defining the name: When I talk to people they say different names on this subject stuff l…
JSON is being used more and more, besides XML, and you surely wanted to parse the data out into SQL instead of doing it in some Javascript. The below function in SQL Server can do the job for you, returning a quick table with the parsed data.
Via a live example, show how to set up a backup for SQL Server using a Maintenance Plan and how to schedule the job into SQL Server Agent.
Via a live example, show how to shrink a transaction log file down to a reasonable size.
Suggested Courses

801 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