I have table in SQL Server containing sales data as follows.
seq_no Itemid Unit pcs value
------------------------------------
1 501 101 1 1001.48
1 502 102 2 1004.25
1 502 102 7 987.58
1 503 103 3 787.58
1 503 103 7 647.87
1 503 103 9 1478.58
1 504 104 2 202.25
1 504 104 3 365.87
1 504 104 7 102.25
1 504 104 6 322.22
1 505 105 1 2000.01
1 505 105 2 914.02
Now I want unit wise maximum value records. Means when I grouping on seq_no, itemid, unit and getting summing of pcs, and value, its giving result as follows.
Itemwise Summary
seq_no itemid unit pcs value
------------------------------------
1 501 101 1 1001.48
1 502 102 9 1991.83
1 503 103 19 2914.03
1 504 104 12 992.59
1 505 105 3 2914.03
Now you can see, I have two different records which has maximum value (2914.03) (eg. 3rd and 5th record). I need 3rd record because It has maximum value with maximum pcs. In my case I want following:
seq_no itemid unit pcs value
-------------------------------------
1 503 105 19 2914.03
How do I get this result, without harming performance, because this table has so many rows?