Solved

SQL Where clause on last record of group

Posted on 2011-09-05
11
867 Views
Last Modified: 2012-05-12
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

0
Comment
Question by:Gaz124
  • 4
  • 3
  • 2
  • +2
11 Comments
 
LVL 19

Expert Comment

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

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

Author Comment

by:Gaz124
ID: 36483240
(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
0
 
LVL 19

Expert Comment

by:Limbeck
ID: 36483245
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
0
 

Author Comment

by:Gaz124
ID: 36483371
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
0
 
LVL 19

Expert Comment

by:Limbeck
ID: 36483410
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
0
PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

 
LVL 10

Expert Comment

by:OnALearningCurve
ID: 36483539
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.
0
 
LVL 11

Expert Comment

by:dougaug
ID: 36485687
Could you post some sample data (from tables journal and calllog) and how the query output should be, please?
0
 

Author Comment

by:Gaz124
ID: 36485759
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
0
 
LVL 11

Accepted Solution

by:
dougaug earned 500 total points
ID: 36485796
Try this:

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

Author Closing Comment

by:Gaz124
ID: 36487159
dougaug you are sql savant!

works perfect thank you
0
 
LVL 75

Expert Comment

by:Anthony Perkins
ID: 36499806
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.
0

Featured Post

Zoho SalesIQ

Hassle-free live chat software re-imagined for business growth. 2 users, always free.

Join & Write a Comment

When you hear the word proxy, you may become apprehensive. This article will help you to understand Proxy and when it is useful. Let's talk Proxy for SQL Server. (Not in terms of Internet access.) Typically, you'll run into this type of problem w…
JSON is being used more and more, besides XML, and you surely wanted to parse the data out into SQL instead of doing it in some Javascript. The below function in SQL Server can do the job for you, returning a quick table with the parsed data.
Via a live example combined with referencing Books Online, show some of the information that can be extracted from the Catalog Views in SQL Server.
Viewers will learn how to use the SELECT statement in SQL to return specific rows and columns, with various degrees of sorting and limits in place.

743 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now