Link to home
Start Free TrialLog in
Avatar of Gaz124
Gaz124Flag for United Kingdom of Great Britain and Northern Ireland

asked on

SQL Where clause on last record of group

Hi I have the following sql query

select top 1000000 journal.callid
from journal, calllog
where journal.heatseq in (select max(heatseq) from journal where journaltype = 'Manager Escalation' group by heatseq)
and
calllog.callid = journal.callid
and calllog.callstatus = 'open'
group by journal.callid

Basically what I want is to get the maximum record of the group (heatseq of journal) then look at that record to see if it has the journal type of 'Manager Escalation'.

The above query works to bring back ANY journal which has a type of 'Manager Escalation' but when the highest record of the group changes to something other than 'Manager Escalation' the query still returns the result.

This is a sql call group for HEAT Callloggin so the first Select cause, select top 1000000 journal.callid
from journal, calllog

cannot change.

Thanks


select top 1000000 journal.callid
from journal, calllog
where journal.heatseq in (select max(heatseq) from journal where journaltype = 'Manager Escalation' group by heatseq)
and 
calllog.callid = journal.callid
and calllog.callstatus = 'open'
group by journal.callid

Open in new window

Avatar of Limbeck
Limbeck

isnt it
(select max(heatseq) from journal where journaltype = 'Manager Escalation' group by journaltype)

or
(select max(heatseq) from journal where journaltype = 'Manager Escalation')
Avatar of Gaz124

ASKER

(select max(heatseq) from journal where journaltype = 'Manager Escalation' group by journaltype)

or
(select max(heatseq) from journal where journaltype = 'Manager Escalation')

Return the top record overall not the top one for each group
ok i understand what you want now, how about something like

select top 1000000 a.callid
from journal a, calllog
where a.heatseq in (select max(heatseq) from journal b where journaltype = 'Manager Escalation' and a.heatseq=b.heatseq group by heatseq)
and
calllog.callid = a.callid
and calllog.callstatus = 'open'
group by a.callid
Avatar of Gaz124

ASKER

select top 1000000 a.callid
from journal a, calllog
where a.heatseq in (select max(heatseq) from journal b where journaltype = 'Manager Escalation' and a.heatseq=b.heatseq group by heatseq)
and
calllog.callid = a.callid
and calllog.callstatus = 'open'
group by a.callid


This still returns all records with 'Manager Escalation', not just the top 1 from each group which matches
hm ok, im guessing here but

select top 1000000 a.callid
from journal a, calllog
where a.heatseq in (select max(heatseq) from journal b where a.journaltype = b.journaltype group by journaltype )
and
calllog.callid = a.callid
and calllog.callstatus = 'open'
group by a.callid
Avatar of OnALearningCurve
How about:

select top 1000000 journal.callid
from journal, calllog
where journal.heatseq in (select max(heatseq) from journal where journaltype = 'Manager Escalation' group by callid)
and
calllog.callid = journal.callid
and calllog.callstatus = 'open'
group by journal.callid

HTH,

Mark.
Could you post some sample data (from tables journal and calllog) and how the query output should be, please?
Avatar of Gaz124

ASKER

Hi not at my laptop right now but I'll give it a go on the mobile.

Call id    Journal Type     heatseq
553024  Manager Escalation  1222253
553024     Call update      1222251
553024     Call logged      1222245
553015     Call update      1222239
553015  Manager Escalation  1222235
553015     Call Logged      1222234

So when I run my query I should just get 1 record returned;

553024 Manager Escalation 1222253

Because it is the last record for that callid when the journal type is Manager Escalation.

The Heatseq number system generate and incremented on each insert

Thanks
ASKER CERTIFIED SOLUTION
Avatar of dougaug
dougaug
Flag of Brazil image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Gaz124

ASKER

dougaug you are sql savant!

works perfect thank you
And of course it should be added that doing a TOP 1000000  without an ORDER BY is like saying give me 1000000 rows, I don't care which ones.  This may (or may not) be somehting you care about, but typically a TOP clause is always used with an ORDER BY clause.