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!

Keeping a ROW intact when using MAX

Status
Not open for further replies.

wickyd

Technical User
Feb 14, 2002
57
ZA
Hi folx

I want to use:
Code:
SELECT NOCOLOUR, GRVQTY, MAX(CAST(GRVDOCNO AS INTEGER)) AS LATESTGRV INTO RESULT3 FROM RESULT2
GROUP BY NOCOLOUR
ORDER BY NOCOLOUR ASCENDING;
but this is not entirely correct as it causes the following problem:

The test data is the following:
Code:
NOCOLOUR   GRVQTY   GRVDOCNO
A            10         1020
B            20         1012
B            50         1034
B            40         1065
The result with my SQL is:
Code:
NOCOLOUR   GRVQTY  LATESTGRV
A            10         1020
B            20         1065 <-- GRVQTY is nonsense
I want the result to be:
Code:
NOCOLOUR   GRVQTY  LATESTGRV
A            10         1020
B            40         1065 <-- the row must remain intact

Thank you.
 
The result you get is very odd, because your SQL statement is illegal. GRVQTY must either be grouped by, or used in a set function (like MAX).

An ANSI SQL compliant solution to your problem:

SELECT * FROM result2
WHERE (nocolour,GRVDOCNO) in
(select NOCOLOUR,max(GRVDOCNO) from result2
group by NOCOLOUR)

ANSI SQL-99, using the non-core feature F641, "Row and table constructors".

Or, if your database server doesn't support row constructors:

select * from result2 r
where GRVDOCNO = (select max(GRVDOCNO) from result2 r2
where r.NOCOLOUR = r2.NOCOLOUR)

Core SQL-99 compliant.
 
I am using a report writer called QRDesign which is based on the DBISAM engine.

Unfortunately, neither of your solutions work.

I modified my previous SQL statement, which now results in a different test data table, whereby GRVDOCNO is now descending, using:
Code:
SELECT GRVDOCNO, SUM(QTY)GRVQTY, NOCOLOUR INTO RESULT2 FROM RESULT1
GROUP BY GRVDOCNO, NOCOLOUR
ORDER BY NOCOLOUR ASCENDING, GRVDOCNO DESCENDING;

I used the following SQL statement to get to the correct result:
Code:
SELECT * INTO RESULT3 FROM RESULT2
GROUP BY NOCOLOUR;

JarlH, thank you for the help.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top