Solved

Sum Function Workaround?

Posted on 2013-10-24
5
246 Views
Last Modified: 2013-10-28
Hello:

I have a Data table that looks like this:
ID      PersonName      PersonClass
557      Dan      P
557      Dan      S
557      Dan      S
557      Dan      P
557      Dan      S
557      Dan      S
189      Mike      S
189      Mike      S
189      Mike      S

What I want to do is if there is a P in the PersonClass Column then Y if not N. My output will look like this

ID      PERSONNAME      PC
557      Dan      Y
189      Mike      N

Here is the script that I am using. It works, however, one of the tables has over 600,000 entries and the sum function really slows it down. Is there a way to a achieve my result without using sum or count?

SELECT ID, PERSONNAME, CASE WHEN PC >=1 THEN 'Y' ELSE 'N' END AS PC
FROM
(
SELECT ID, PERSONNAME, SUM(PC) AS PC
FROM
(
select ID, PERSONNAME, case when PERSONCLASS = 'p' THEN 1 ELSE 0 END AS PC FROM Test
)
table1
GROUP BY ID, PERSONNAME
)
TABLE2

Open in new window

0
Comment
Question by:RecipeDan
5 Comments
 
LVL 40

Expert Comment

by:lcohan
ID: 39598821
Did you tried simple CASE statement like:



select ID, PERSONNAME, case when PERSONCLASS = 'p' THEN 1 ELSE 0 END AS PC FROM Test
GROUP BY PERSONNAME
0
 
LVL 16

Expert Comment

by:DcpKing
ID: 39598823
select PersonClass, count(PersonClass) 
from table 
group by PersonClass

Open in new window

will give you how many P's and how many S's overall.

Then expand that to individuals
select PersonName, PersonClass, count(PersonClass) 
from table 
group by PersonName, PersonClass

Open in new window

to get the name, class, and count.

Even with a million records it shouldn't take _that_ long, but I doubt that there's a better way.
0
 
LVL 21

Accepted Solution

by:
Dale Burrell earned 500 total points
ID: 39598824
Its unlikely to be the 'sum' which is an issue... 600000 records isn't many in the great database scheme of things.

It could be the fact that you have 3 nested queries which might be preventing the optimiser from working. Or it could be the lack of a good index.

I recon this, simplified, query will produce the same result - don't know if it is faster though.

select id, PersonName, max(case when PERSONCLASS = 'p' THEN 'Y' ELSE 'n') END
from Test
group by id, PersonName

Open in new window

0
 
LVL 40

Expert Comment

by:lcohan
ID: 39598830
Sorry I missed the ID in the group by:

select ID, PERSONNAME, case when PERSONCLASS = 'p' THEN 1 ELSE 0 END AS PC FROM Test
GROUP BY ID, PERSONNAME
0
 
LVL 48

Expert Comment

by:PortletPaul
ID: 39599425
As P < S  I would just go for a simple MIN(PERSONCLASS) and then do a case expression on that - you do fewer case expressions this way.
|  ID | PERSONNAME | PC |
|-----|------------|----|
| 557 |        Dan |  Y |
| 189 |       Mike |  N |


SELECT
        id
      , PersonName
      , CASE WHEN MIN(PERSONCLASS) = 'P' THEN 'Y' ELSE 'N' END as PC
FROM YourTable
GROUP BY id
       , PersonName
;   
    
    CREATE TABLE YourTable
    	([ID] int, [PersonName] varchar(4), [PersonClass] varchar(1))
    ;
    	
    INSERT INTO YourTable
    	([ID], [PersonName], [PersonClass])
    VALUES
    	(557, 'Dan', 'P'),
    	(557, 'Dan', 'S'),
    	(557, 'Dan', 'S'),
    	(557, 'Dan', 'P'),
    	(557, 'Dan', 'S'),
    	(557, 'Dan', 'S'),
    	(189, 'Mike', 'S'),
    	(189, 'Mike', 'S'),
    	(189, 'Mike', 'S')
        ;
        
http://sqlfiddle.com/#!3/15ef6/4 

Open in new window

If you still have performance issues please provide an execution plan (as .sqlplan file)
0

Featured Post

Ransomware-A Revenue Bonanza for Service Providers

Ransomware – malware that gets on your customers’ computers, encrypts their data, and extorts a hefty ransom for the decryption keys – is a surging new threat.  The purpose of this eBook is to educate the reader about ransomware attacks.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
SQL Server 2014 STD not using available memory 5 49
SQL Server Sum Over Multiple Tables 20 30
SQL Add Parameter in Variable 4 21
How come this XML node is not read? 3 25
Load balancing is the method of dividing the total amount of work performed by one computer between two or more computers. Its aim is to get more work done in the same amount of time, ensuring that all the users get served faster.
Ever needed a SQL 2008 Database replicated/mirrored/log shipped on another server but you can't take the downtime inflicted by initial snapshot or disconnect while T-logs are restored or mirror applied? You can use SQL Server Initialize from Backup…
This video shows, step by step, how to configure Oracle Heterogeneous Services via the Generic Gateway Agent in order to make a connection from an Oracle session and access a remote SQL Server database table.
This video shows how to set up a shell script to accept a positional parameter when called, pass that to a SQL script, accept the output from the statement back and then manipulate it in the Shell.

679 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