Solved

SQL Where clause on last record of group

Posted on 2011-09-05
11
870 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
Optimizing Cloud Backup for Low Bandwidth

With cloud storage prices going down a growing number of SMBs start to use it for backup storage. Unfortunately, business data volume rarely fits the average Internet speed. This article provides an overview of main Internet speed challenges and reveals backup best practices.

 

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
 
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

Best Practices: Disaster Recovery Testing

Besides backup, any IT division should have a disaster recovery plan. You will find a few tips below relating to the development of such a plan and to what issues one should pay special attention in the course of backup planning.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

This article explains how to reset the password of the sa account on a Microsoft SQL Server.  The steps in this article work in SQL 2005, 2008, 2008 R2, 2012, 2014 and 2016.
The Delta outage: 650 cancelled flights, more than 1200 delayed flights, thousands of frustrated customers, tens of millions of dollars in damages – plus untold reputational damage to one of the world’s most trusted airlines. All due to a catastroph…
Familiarize people with the process of utilizing SQL Server functions from within Microsoft Access. Microsoft Access is a very powerful client/server development tool. One of the SQL Server objects that you can interact with from within Microsoft Ac…
Viewers will learn how the fundamental information of how to create a table.

785 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