开发者

Oracle Select Highest date per record

开发者 https://www.devze.com 2023-03-10 19:25 出处:网络
I\'m a little bit stumped as to how to do this.I want to select records from a table \"agency\" joined to a table \"notes\" on an id column that the two tables share.

I'm a little bit stumped as to how to do this. I want to select records from a table "agency" joined to a table "notes" on an id column that the two tables share.

Table structure:

create table notes (
    notes_id varchar2(5),
    agency_gp_id varchar2(5),
    call_date date,
    call_note varchar2(4000)
);

create table agency(
    agency_id varchar2(5),
    agency_name varchar2(5),
    street varchar2(75),
    city varchar2(50)
);

alter table notes add constraint "fk_group_notes_agency_id" foreign key(agency_gp_id)
    references agency(agency_id) enable;

-Each table has auto-numbering, "before-insert" triggers so the id numbers stay in synch (along with other stuff in the case of adding a note to a newly created agency) - everything I need it to do (the databse), it does.

-Each record from the agency table has a distinct name/address combo (with different branches in different cities) and each record from the notes table has a date entry corresponding to each agency.

-Each agency can have multiple notes (multiple note details from subsequent visits)

What I am attempting to do is select each (distinct agency,street,city) that has not had a note added to it within the past four months.

This is the query I came up with:

SELECT count(a.agency_name) as number_of_visits,
    a.agency_name,
    (a.street||', '||a.city) as "Location", 
    n.call_date,
    ROUND(TRUNC(sysdate - call_date)) AS days_since_visit
FROM notes n, agency a
WHERE (sysdate - n.call_date) > 120
    AND n.agency_gp_id = a.agency_id
    --AND a.city = 'M开发者_StackOverflow社区ünchen' --not necessary, used for limiting number of results
GROUP BY n.call_date,a.agency_name,a.street, a.city
ORDER BY a.agency_name ASC, n.call_date desc;

It kind of works...I can see what I want but I also see what I DO NOT want (e.g. the multiple notes on each agency). The only thing I want to see is the last entry (most recent, according to the WHERE clause) of each agency. The picture I want to create is: For whichever agency that has not been annotated within 120 days of the last note, display the address and name and the last note date. (Instead of showing the number of days since EACH visit, I want to show the number of days that have past since the LAST visit - per distinct agency,street,city).

This is for an app that will help a sales executive schedule her sales calls and is run twice a week. I have been unable to figure this out. Also, bear in mind that the actual tables used are much more descriptive - what I have used here are only the parts I need to describe the question.

I would appreciate any suggestions on how to solve this problem.

Thanks!


If I understand your problem correctly, changing call_date to MAX(call_date) (and removing it from the GROUP BY statement) should get you what you want int terms of data, but would also pull in false positives, namely any agency that had notes older than 120 days, regardless of the most recent note. If we filter those agencies out in a NOT EXISTS subquery, that should get you where you need to go.

SELECT count(a.agency_name) as number_of_visits,
    a.agency_name,
    (a.street||', '||a.city) as "Location", 
    MAX(n.call_date),
    ROUND(TRUNC(sysdate - MAX(call_date))) AS days_since_visit
FROM notes n, agency a
WHERE (sysdate - n.call_date) > 120
    AND n.agency_gp_id = a.agency_id
    AND NOT EXISTS (SELECT 1 FROM notes n2 
                    WHERE n2.agency_gp_id = a.agency_id 
                    AND (sysdate - n2.call_date) <= 120)
    --AND a.city = 'München' --not necessary, used for limiting number of results
GROUP BY a.agency_name,a.street, a.city
ORDER BY a.agency_name ASC, MAX(n.call_date) desc;
0

精彩评论

暂无评论...
验证码 换一张
取 消