dbTalk Databases Forums  

3 Methods to do the same thing, which is best?

microsoft.public.sqlserver.programming microsoft.public.sqlserver.programming


Discuss 3 Methods to do the same thing, which is best? in the microsoft.public.sqlserver.programming forum.



Reply
 
Thread Tools Display Modes
  #1  
Old   
Michael C
 
Posts: n/a

Default 3 Methods to do the same thing, which is best? - 08-11-2009 , 01:16 AM






He Guys, here's 3 different methods to do the same thing, I'm curious to
know which method ppl think is the best. In this example I want to get the
employee who has the maximum salary in each department. If I simply use the
max function then I would only be able to get the maximum salary, not the
employee name, so a bit of a workaround is needed. Opinions?

Cheers,
Michael


CREATE TABLE #People (Name VARCHAR(20), Salary INT, Dept VARCHAR(20))
INSERT INTO #People VALUES ('John', 200000, 'Sales')
INSERT INTO #People VALUES ('Swee', 220000, 'Sales')
INSERT INTO #People VALUES ('Pete', 220000, 'HR')
INSERT INTO #People VALUES('Jenny', 240000, 'HR')


;WITH Y AS (
SELECT Name, Salary, MAX(Salary) OVER (PARTITION BY Dept) AS MaxSalary,
Dept
FROM #People
)
SELECT Name, Salary, Dept FROM Y
WHERE Salary = MaxSalary
ORDER BY Name



SELECT * FROM #People AS P1
WHERE NOT EXISTS (SELECT * FROM #People AS P2 WHERE P2.Salary > P1.Salary
AND P2.Dept = P1.Dept)
ORDER BY Name

;WITH Y AS (
SELECT Dept, MAX(Salary) AS MaxSalary FROM #People GROUP BY Dept
)
SELECT #People.* FROM #People
JOIN Y ON #People.Dept = Y.Dept AND #People.Salary = Y.MaxSalary
ORDER BY Name

DROP TABLE #People

Reply With Quote
  #2  
Old   
Uri Dimant
 
Posts: n/a

Default Re: 3 Methods to do the same thing, which is best? - 08-11-2009 , 01:50 AM






Michael

I think WHERE NOT EXISTS is a good one
Another is
SELECT * FROM #People

WHERE Salary=(SELECT MAX(Salary) FROM #People P WHERE

P.Dept=#People.Dept)





"Michael C" <mike (AT) nospam (DOT) com> wrote

Quote:
He Guys, here's 3 different methods to do the same thing, I'm curious to
know which method ppl think is the best. In this example I want to get the
employee who has the maximum salary in each department. If I simply use
the max function then I would only be able to get the maximum salary, not
the employee name, so a bit of a workaround is needed. Opinions?

Cheers,
Michael


CREATE TABLE #People (Name VARCHAR(20), Salary INT, Dept VARCHAR(20))
INSERT INTO #People VALUES ('John', 200000, 'Sales')
INSERT INTO #People VALUES ('Swee', 220000, 'Sales')
INSERT INTO #People VALUES ('Pete', 220000, 'HR')
INSERT INTO #People VALUES('Jenny', 240000, 'HR')


;WITH Y AS (
SELECT Name, Salary, MAX(Salary) OVER (PARTITION BY Dept) AS MaxSalary,
Dept
FROM #People
)
SELECT Name, Salary, Dept FROM Y
WHERE Salary = MaxSalary
ORDER BY Name



SELECT * FROM #People AS P1
WHERE NOT EXISTS (SELECT * FROM #People AS P2 WHERE P2.Salary > P1.Salary
AND P2.Dept = P1.Dept)
ORDER BY Name

;WITH Y AS (
SELECT Dept, MAX(Salary) AS MaxSalary FROM #People GROUP BY Dept
)
SELECT #People.* FROM #People
JOIN Y ON #People.Dept = Y.Dept AND #People.Salary = Y.MaxSalary
ORDER BY Name

DROP TABLE #People


Reply With Quote
  #3  
Old   
Plamen Ratchev
 
Posts: n/a

Default Re: 3 Methods to do the same thing, which is best? - 08-11-2009 , 07:16 AM



The following should be better than the 3 methods:

WITH Y AS (
SELECT Name, Dept, Salary, DENSE_RANK() OVER(PARTITION BY Dept ORDER BY Salary DESC) AS rk
FROM #People)
SELECT Name, Dept, Salary
FROM Y
WHERE rk = 1;

This can benefit from covering index or at least index on the partitioning and ordering columns.

--
Plamen Ratchev
http://www.SQLStudio.com

Reply With Quote
  #4  
Old   
Michael C
 
Posts: n/a

Default Re: 3 Methods to do the same thing, which is best? - 08-12-2009 , 01:01 AM



"Plamen Ratchev" <Plamen (AT) SQLStudio (DOT) com> wrote

Quote:
The following should be better than the 3 methods:

WITH Y AS (
SELECT Name, Dept, Salary, DENSE_RANK() OVER(PARTITION BY Dept ORDER BY
Salary DESC) AS rk
FROM #People)
SELECT Name, Dept, Salary
FROM Y
WHERE rk = 1;

This can benefit from covering index or at least index on the partitioning
and ordering columns.
Thanks Uri and Plamen, is there any reason you use DENSE_RANK over RANK?

Michael

Reply With Quote
  #5  
Old   
Plamen Ratchev
 
Posts: n/a

Default Re: 3 Methods to do the same thing, which is best? - 08-12-2009 , 07:46 AM



Not really, both RANK and DENSE_RANK will work fine in this case as only rank 1 is needed.

--
Plamen Ratchev
http://www.SQLStudio.com

Reply With Quote
Reply




Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off



Powered by vBulletin Version 3.5.3
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.