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 IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Select query (with DISTINCT) yields multiple results 1

Status
Not open for further replies.

karlomutschler

Programmer
Jun 5, 2001
75
DE
Hi,
The query below should display the contract number and the last invoice date with the corresponding amount.
However, the result lists all the dates and corresponding amounts.

SELECT DISTINCT con.contract_id
,MAX(cot.revenue_date)
,cot.revenue_amount
FROM contract con
INNER JOIN contract_turnover cot ON con.contract_id = cot.contract_id
GROUP BY con.contract_id, cot.revenue_amount ;

Result:
contract_id revenue_date revenue_amount
1000055 2000-05-01 5,01
1000055 2000-06-01 14,18
1000055 2000-07-01 13,07
1000055 2000-08-01 9,40
1000055 2000-09-01 16,06
1000055 2000-10-01 12,10

Desired result:
1000055 2000-10-01 12,10

TIA
Kind regards.
Karlo
 
Hi Karlo,

The query as written is working correctly, because it is returning lots of DISTINCT MAX dates. Don't forget, DISTINCT works across the entire query, not just the column it is put against.

What you actually require is :

SELECT con.contract_id,
MAX(cot.revenue_date),
cot.revenue_amount
FROM contract con
INNER JOIN contract_turnover cot ON con.contract_id = cot.contract_id
GROUP BY con.contract_id, cot.revenue_amount ;

Tim
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top