0

I have the following table (must shorter version than the real one), and I want to all the rows with max _ values for each _ displayed. How should I do this?

Table Now

enter image description here

Table I want to have

enter image description here

thanks a lot in advance!!

Hitesh
  • 3,449
  • 8
  • 39
  • 57
elaine
  • 9
  • 1
  • Don't use images for sample data and what have you tried? This doesn't look particularly difficult if you use a window function like `RANK()`. – ZLK Oct 24 '17 at 04:21

3 Answers3

1

Using the dense_rank function and a derived table would be appropriate for this (please note I used underscores instead of spaces in the column names):

select group_type
       ,desk_number
       ,comments
from
(select *
       ,dense_rank() over(partition by group_type order by desk_number desc) dr
from mytable) t1
where t1.dr = 1

I made a rextester sample that you can try here

Let me know if you have any questions.

Simon
  • 1,201
  • 9
  • 18
0

try this :

WITH CTE
AS
(
  SELECT
    SeqNo = ROW_NUMBER() OVER(ORDER BY CAST(DeskNumber AS INT) DESC PARTITION BY GroupType),
    GroupType,
    DeskNumber,
    [Comment]
    FROM YourTable
)
SELECT
*
FROM CTE WHERE CTE.SeqNo = 1
Jayasurya Satheesh
  • 7,826
  • 3
  • 22
  • 39
0

How can I SELECT rows with MAX(Column value), DISTINCT by another column in SQL?

This answers your question quite well but I will convert it for your convenience <3

SELECT *
FROM table
INNER JOIN
    (SELECT comments, MAX([desk number]) AS MaxDesk
    FROM table
    GROUP BY comments) groupedtable
ON table.[desk number]= groupedtable.[desk number]
AND table.comments= groupedtable.MaxDesk
calamities
  • 78
  • 3
Jesse
  • 283
  • 1
  • 5
  • 14