Sql Duplicate Column

Questions about the MySQL, PostgreSQL, and most other databases, as well as using it with PHP can be asked here.

Moderator: General Moderators

Post Reply
MAGDOOMAK
Forum Newbie
Posts: 1
Joined: Wed Dec 13, 2006 8:34 am

Sql Duplicate Column

Post by MAGDOOMAK »

My STOCK table is like this:

Catcode Item_No SoldOn Returned_Date Invoice_No

P P00797 9/19/2006 21/12/2005 COL01255
P P00798 8/20/2005 21/12/2005 COL01250
P P00799 6/10/2005 04/10/2006 COL01717
P P00799 5/13/2005 04/05/2006 COL01622
P P00800 2/09/2005 07/11/2005 COL01217

I would like retrieve distinct item_no (P00799) from the above table, so, I used the query as:

select Distinct A.Catcode,A.Item_No,A.SoldOn from Stock A where A.SoldOn in (select max(SoldOn) from Stock group by item_no)

which returns all the rows from the above table. (P00799 is coming twice)
(How can I modify the query to satisfy my condition)

Thanks in advance
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

DISTINCT works across all fields in the final record unfortunately, so you will likely need a GROUP BY.
Post Reply