?
Solved

SQL Where clause on last record of group

Posted on 2011-09-05
11
Medium Priority
?
873 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Microsoft Certification Exam 74-409

Veeam® is happy to provide the Microsoft community with a study guide prepared by MVP and MCT, Orin Thomas. This guide will take you through each of the exam objectives, helping you to prepare for and pass the examination.

 

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

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

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

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…
In part one, we reviewed the prerequisites required for installing SQL Server vNext. In this part we will explore how to install Microsoft's SQL Server on Ubuntu 16.04.
This video shows, step by step, how to configure Oracle Heterogeneous Services via the Generic Gateway Agent in order to make a connection from an Oracle session and access a remote SQL Server database table.
Via a live example, show how to extract information from SQL Server on Database, Connection and Server properties
Suggested Courses

762 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