Click here to monitor SSC
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


SQL query


SQL query

Author
Message
coolman.peto
coolman.peto
Forum Newbie
Forum Newbie (1 reputation)Forum Newbie (1 reputation)Forum Newbie (1 reputation)Forum Newbie (1 reputation)Forum Newbie (1 reputation)Forum Newbie (1 reputation)Forum Newbie (1 reputation)Forum Newbie (1 reputation)

Group: General Forum Members
Points: 1 Visits: 5
Hello,
I need advice on how to do SQL queries that I returned the following:
I have 2 tables: customer and the department
-----------
SELECT a.id, a.first_name, a.last_name, MIN (b.income) b.department
/* --b.department can not be in GOUP BY clause clause, but I need to know which department has the
--smallest income, ie which department is responsible MIN (b.income) * /
FROM CUSTOMERS a
INNER JOIN department b
ON a.id = b.id
GROUP BY a.id, a.first_name, a.last_name;

How can I do it?
Luis Cazares
Luis Cazares
SSCrazy Eights
SSCrazy Eights (8.5K reputation)SSCrazy Eights (8.5K reputation)SSCrazy Eights (8.5K reputation)SSCrazy Eights (8.5K reputation)SSCrazy Eights (8.5K reputation)SSCrazy Eights (8.5K reputation)SSCrazy Eights (8.5K reputation)SSCrazy Eights (8.5K reputation)

Group: General Forum Members
Points: 8487 Visits: 18089
The common way is to use ROW_NUMBER() within a CTE or subquery.

WITH CTE AS(
SELECT a.id,
a.first_name,
a.last_name,
b.income,
b.department,
ROW_NUMBER() OVER( PARTITION BY a.id ORDER BY b.income) rn
FROM CUSTOMERS a
INNER JOIN department b ON a.id = b.id
)
SELECT id,
first_name,
last_name,
income,
department
FROM CTE
WHERE rn = 1;


If you want ties, you could use RANK().

Feel free to ask any questions that you need to fully understand the code. ;-)


Luis C.
General Disclaimer:
Are you seriously taking the advice and code from someone from the internet without testing it? Do you at least understand it? Or can it easily kill your server?


How to post data/code on a forum to get the best help: Option 1 / Option 2
Mike Good
Mike Good
SSC-Addicted
SSC-Addicted (405 reputation)SSC-Addicted (405 reputation)SSC-Addicted (405 reputation)SSC-Addicted (405 reputation)SSC-Addicted (405 reputation)SSC-Addicted (405 reputation)SSC-Addicted (405 reputation)SSC-Addicted (405 reputation)

Group: General Forum Members
Points: 405 Visits: 1020
I like Luis' answer. A different approach, likely with a different query plan which may be more or less efficient depending on your data/indexes, etc is this:

    SELECT a.id, 
a.first_name,
a.last_name,
b.income,
b.department
FROM CUSTOMERS a
CROSS APPLY (SELECT TOP 1 b.income, b.department FROM department b WHERE b.id = a.id ORDER BY b.income DESC) AS b




Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum

































































































































































SQLServerCentral


Search