Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

SQL Where clause on last record of group

Posted on 2011-09-05
11
Medium Priority
?
881 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
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.

 

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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Why is this different from all of the other step by step guides?  Because I make a living as a DBA and not as a writer and I lived through this experience. Defining the name: When I talk to people they say different names on this subject stuff l…
When trying to connect from SSMS v17.x to a SQL Server Integration Services 2016 instance or previous version, you get the error “Connecting to the Integration Services service on the computer failed with the following error: 'The specified service …
Via a live example, show how to setup several different housekeeping processes for a SQL Server.
Viewers will learn how to use the INSERT statement to insert data into their tables. It will also introduce the NULL statement, to show them what happens when no value is giving for any given column.

636 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