0

I have a table of data that looks like follows:

+--------+----------+------------+
| Client | Item No. | Serial No. |
+--------+----------+------------+
| A      |        1 |          1 |
| A      |        1 |          2 |
| B      |        1 |          2 |
| B      |        2 |          1 |
| C      |        2 |          3 |
| C      |        2 |          2 |
| C      |        3 |          1 |
| C      |        3 |          2 |
| D      |        2 |          3 |
| D      |        2 |          1 |
| D      |        3 |          2 |
| D      |        3 |          3 |
| D      |        2 |          2 |
| D      |        3 |          1 |
+--------+----------+------------+

What I'm looking to do is find for each client the highest Item No. followed by highest Serial No. (in that order). So for the above the output would be:

+--------+----------+------------+
| Client | Item No. | Serial No. |
+--------+----------+------------+
| A      |        1 |          2 |
| B      |        2 |          1 |
| C      |        3 |          2 |
| D      |        3 |          3 |
+--------+----------+------------+

I'm thinking this will require a nested MAX() statement, to first get the MAX(Item No.) for each client and for those with multiple then get the MAX(Serial No.) What's an efficient way to write this query?

2
  • which DBMS are you using? MySQL?
    – Deep
    Nov 24, 2014 at 7:28
  • I'm using MS SQL 2008
    – David_S53
    Nov 25, 2014 at 0:40

3 Answers 3

1

Maybe this helps:

SELECT t.client,
       t.item,
       MAX(t.serial) AS serial
  FROM(SELECT client,
              MAX(item) AS item
         FROM your_table
        GROUP
           BY client
      ) a
  JOIN your_table t
    ON a.client = t.client AND a.item = t.item
 GROUP
    BY t.client, t.item 

If your DBMS supports window functions, another way would be:

SELECT client, item, serial
  FROM(SELECT client, item, serial,
              ROW_NUMBER() OVER (PARTITION BY client ORDER BY item DESC, serial DESC) rn
         FROM your_table
      )
 WHERE rn = 1
2
  • Great solution, I was wondering if there was something available like the window function you mention. In terms of performance does the Window function work better?
    – David_S53
    Nov 25, 2014 at 1:02
  • The Windowing version will be much better unless the query optimizer understands the long version and does the same thing. There should be an EXPLAIN feature to compare query plans. Nov 25, 2014 at 5:10
0

Postgres has the awesome shortcut

SELECT DISTINCT ON (Client) Client, Item, SerialNumber
FROM some_table
ORDER BY Item DESC, SerialNumber DESC -- DESC gets highest

Because of the DISTINCT this grabs only one record per Client, and the ORDER BY makes sure it is the ones you want.

I have no idea if any other DBMSs will do this so easily.

1
  • Yes i was hoping something like this would work but not in MS SQL. Closest way to get the same is per the OVER clause as shown in DirkNM's response. Same idea, returns just the row number you specify, in this case 1.
    – David_S53
    Nov 25, 2014 at 2:02
0

here is solution:

WITH CTE AS (
SELECT Client, MAX(Item_No) AS  Item_No
FROM YourTable 
GROUP BY Client)

SELECT a.Client,b.Item_No,MAX(Serial_No) AS Serial_No 
FROM CTE a
JOIN YourTable b ON a.Client = b.Client AND a.Item_No = b.Item_No
GROUP BY a.Client,b.Item_No

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.