-3

I want an SQL statement to get the row with a minimum value to the "C4".

Consider the table:

Table

I need something like this

Result

I need to group N2 and N3 because the value of C2 and C3 is the same, I can select any value of C4 to group, in this case we can take 10 or 20, or in C1 with same N4 and N5. Can take 12 or 14.

Not need the maximum to grout, I need one value with C2 and C3 are the same. C1 is a Id this can be 1,2,3.. etc

Thanks for any help!

Wzap PepexD
  • 17
  • 1
  • 9

2 Answers2

1
SELECT * FROM yourtable
WHERE C4 = (SELECT MIN(C4) FROM yourtable)
dadde
  • 649
  • 1
  • 11
  • 24
1

Can't exactly match your question with the expected answer as shown.

I suppose you're trying to do aggregation with grouping on C2 / C3 columns here. You may try this.

SELECT * 
FROM [Table_Name] t
    JOIN
        (
            SELECT 
                C2, C3, MIN(C4) as MIN_C4
            FROM 
                [Table_Name]
            GROUP BY C2, C3
        ) Aggreated_Table ON Aggreated_Table.C2 = t.C2 AND Aggreated_Table.C3 = t.C3 AND Aggreated_Table.MIN_C4 = t.C4

Alternatively, using ROW_NUMBER()

SELECT C1, C2, C3, C4
FROM
(
    SELECT *, ROW_NUMBER() OVER (PARTITION BY C2, C3 ORDER BY C4) AS rk FROM [TABLE_NAME]
) Ranked_Table
WHERE rk = 1
ydoow
  • 2,969
  • 4
  • 24
  • 40