Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

DISTINCT Problem

Status
Not open for further replies.

BlueBeep

Programmer
Aug 13, 2002
23
US
Here's my query. It's for an access db - I'm trying to return both columns, the product id and productdate, order it by productdate, and have no duplicate records based on productid.

Is this possible? I'm was using distinct before, without luck. I removed it, but nothing still. Here's the query I'm trying to use without luck. Thanks.

SELECT ProductID, ProductDate
FROM Products
WHERE ProductDate >= #1/1/03# AND ProductDate <= #6/1/03#
GROUP BY ProductID, ProductDate
ORDER BY ProductDate;

I'm still getting some duplicate ProductIDs. (Duplicate ProductsIDs when the ProductDate is different). Not as much as before. Any thoughts? I gladly appreciate it. Thanks!
 
I tried this in MySQL without luck either.
 
Well read up on SQL The
Code:
DISTINCT
clause only eliminates duplicate rows from the result, not duplicate columns. If you want unique product id's order by date, but don't need the date try this.

Code:
SELECT DISTINCT ProductID, ProductDate 
FROM Products 
WHERE ProductDate >= #1/1/03# AND ProductDate <= #6/1/03#
ORDER BY ProductDate;

If you need the date and need unique product ids then you need to decide what date do you want. Do you want the greatest,
Code:
MAX
, or the least,
Code:
MIN
. Then you can do this.

Code:
SELECT ProductID, MAX(ProductDate) 
FROM Products 
WHERE ProductDate >= #1/1/03# AND ProductDate <= #6/1/03# 
GROUP BY ProductID
ORDER BY ProductDate;

You only need a
Code:
GROUP BY
when you are using an aggregate function:
Code:
MAX
,
Code:
MIN
,
Code:
SUM
,
Code:
AVG
,
Code:
COUNT
, etc.
 
Sorry... I submitted the previous post on accident. Here is the correct syntax for the first query I posted.

Code:
SELECT DISTINCT ProductID
FROM Products 
WHERE ProductDate >= #1/1/03# AND ProductDate <= #6/1/03#
ORDER BY ProductDate;

Good Luck,

abombss
 
abombss, some databases will not allow you to sort a result set by a column not included in the result set

your &quot;correct syntax&quot; might actually run in mysql but it doesn't make sense

;)

also, your comment &quot;You only need a GROUP BY when you are using an aggregate function: MAX, MIN, SUM, AVG, COUNT, etc.&quot; is not quite right, or at least needs some qualification

if you use an aggregate function, then you must use GROUP BY on any non-aggregates in the SELECT list, but if there are no non-aggregates in the SELECT list, then the entire result set is treated as one large group

for example, [tt]select count(*) from addresses[/tt]

further, if you do have GROUP BY, you do not necessarily need an aggregate function

for example, [tt]select prov from addresses group by prov[/tt]

this last example works the same way and gives the same result as [tt]select distinct prov from addresses[/tt]

rudy
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top