Click here to Skip to main content
15,896,063 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Dear Friends,

I need to retrieve Minimum Id Rows using sql query.. I tried this query:

SQL
SELECT    MIN(Id), item_code, item_name, item_price, StockInHand, actv_code, actv_name from Stocks 
group by item_code, item_name, item_price, StockInHand, actv_code, actv_name



can't retrieve Minimum Id Rows, but the same time, I removed item_price then i tried it comes. but i must need item_price.
Posted
Updated 1-Nov-15 20:42pm
v2
Comments
Tomas Takac 2-Nov-15 3:01am    
Are you looking for min id per item_code + some details? Then you should do it in two steps: first get the id, then join to the table to get the details.

1 solution

If you want a single row to return then you can do it alternatively using TOP clause.
SQL
SELECT TOP 1 Id, item_code, item_name, item_price, StockInHand, actv_code, actv_name 
FROM Stocks 
ORDER BY Id


If you want minimum id with same item_code then you can try something like-
SQL
SELECT S.Id, S.item_code, item_name, item_price, StockInHand, actv_code, actv_name 
FROM Stocks S
INNER JOIN 
(
  SELECT MIN(Id), item_code   
  FROM Stocks 
  GROUP BY item_code
) AS T
ON S.Id=T.Id


If you want something else than this, please let me know.

Hope, it helps :)
 
Share this answer
 
v2
Comments
Vivek.anand34 2-Nov-15 3:56am    
This is also same problem.. all Rows returns..
Suvendu Shekhar Giri 2-Nov-15 4:24am    
Can you share some sample data from you result?
Vivek.anand34 2-Nov-15 4:06am    
Then Top clause is an error.. when i put top(100) then only executed...
Suvendu Shekhar Giri 2-Nov-15 4:23am    
..sorry. I missed the "1" in "TOP 1"
Vivek.anand34 2-Nov-15 5:01am    
top 1 is only one row return.. but i need every Items minimum 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