help with this group of

Hello

I need your help to make a group in a query (for ORACLE 11 g) for some columns and a particular column, get the latest (or value of that column) it's my table example:

CODE MOBILE AMOUNT COUNTRY RECORD_DATE
10009999999910U.S..18 JUNE 14
1000999999995ItalyJUNE 13, 14
20009999999920France16 JUNE 14
2000888888883Germany17 JUNE 14
20008888888811France14 JUNE 14
10008888888832CanadaJUNE 10, 14

So my query result should make a 'group', CODE, MOBILE, and the AMOUNT of MONEY and give the latter country registered in this form:

CODE MOBILE AMOUNT COUNTRY
10009999999915U.S..
20009999999920France
10008888888832Canada
20008888888814Germany

Is just an example, I can make the query and group by statement, but I can't show just the last country (though I think that in the recent record of column RECORD_DATE).

Can you help me with this pls? I don't know much about oracle 11 g

Thank you much in advance.

Best regards

Mauro

Hi, Mauro,

So, you don't want to MAX (record_date); you want to the country which is on the same line as the MAX (record_date); Isn't it?

This looks like a job for the fucntion LAST aggregation.

Since I have not phew copy your table, I'll use scott.emp (which is probably on your system) to illustrate.

SELECT deptno, job

The SUM (sal) AS total_sal

MIN (ename) DUNGEON (DENSE_RANK LAST ORDER BY hiredate)

AS most_recent

FROM scott.emp

GROUP BY deptno, job

ORDER BY deptno, job

;

Output:

DEPTNO JOB TOTAL_SAL MOST_RECENT

---------- --------- ---------- -----------

THE 10 1300 MILLER CLERK

10 MANAGER 2450 CLARK

10. PRESIDENT KING 5000

20 ANALYST SCOTT 6000

20 CLERK 1900 ADAMS

20 MANAGER 2975 JONES

JAMES CLERK 30 950

30 MANAGER 2850 BLAKE

30 SELLER 5600 MARTIN

What happens if one is be a tie for the most recent line, i.e. If the MAX (record_date) in a group we produce 2 or more lines, with different countries?

I hope that answers your question.

If not, post a small example of data (CREATE TABLE and only relevant columns, INSERT statements) for all of the tables involved and the results desired from these data.

Point where the above query is to produce erroneous results, and explain, using specific examples, how you get the right results from data provided in these places.

If you change the query at all, post your modified version.

Always say what version of Oracle you are using (for example, 11.2.0.2.0).

See the FAQ forum: https://community.oracle.com/message/9362002#9362002

Tags: Database

Similar Questions

Maybe you are looking for