Click here to Skip to main content
15,885,309 members
Please Sign up or sign in to vote.
5.00/5 (1 vote)
See more:
I have following result


compID OrderId  rate
__________________________________________

  15	123      1000
  12	123      2344
  13	123      2456
  14	123      2666

  119	234      3245
  120	234      3678
  118	234      4566
  23	234      6789

  22	345      900
  27	345      980
  26	345      1567
  25	345      1600


now I need compid having minimum rate group by order id

for eg.

for order id 123 company id is 15
for order id 234 company id is 119
for order id 345 company id is 22
Posted
Updated 17-Aug-14 23:37pm
v2

Hi,
I think this will work for you.

SQL
select  orderid,compid from TableName
where rate in(select MIN(rate) from TableName group by OrderId)
 
Share this answer
 
v2
Comments
Gihan Liyanage 18-Aug-14 6:35am    
I didn't see your answer before post my one. Any way great, this also working.
I have Tested for you ...

SQL
SELECT compID, OrderId, rate
FROM (
    SELECT *, ROW_NUMBER() OVER (PARTITION BY OrderId ORDER BY rate) AS rn
    FROM TestTable
) AS T1
WHERE rn = 1


Please mark as answered if you are ok with the answer.
 
Share this answer
 
Comments
jagdish123061 26-Feb-15 0:28am    
thank u soooo much @Gihan Liyange
SQL
select  compID ,OrderId , MIN(rate) from TableName

GROUP BY compID ,OrderId
 
Share this answer
 
Comments
jagdish123061 18-Aug-14 6:08am    
not working it giving me all company id

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900