?
Solved

How do i write this complex SQl statement using values within results group as condition?

Posted on 2008-06-11
7
Medium Priority
?
232 Views
Last Modified: 2010-04-21
The simplified view of this issue: I have 2 tables (ALERT, ALERT_GROUP) which can be joined on alert_id

AlLERT has 2 fields (alert_id and name)
ALERT_GROUP has 3 fields (group_id, alert_id and priority)

Given an alert_id ($alid) I wish to return a list of other alert_ids that belong to the groups of which $alid is a member BUT only if they have a priority greater than or equal to $alid within each group.

Writing a query to do this without checking the priorities within groups is realtively trivial but requires external processing and a second SQL call to get the desired result.  I am trying to avoid having to do any of the statement outside of the DB if possible.

0
Comment
Question by:ja1clark
[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
  • 3
  • 3
7 Comments
 
LVL 15

Expert Comment

by:dirknibleck
ID: 21763418
You could just use 2 nested selects:

SELECT * FROM ALERT, ALERT_GROUP where ALERT.alert_id = ALERT_GROUP.alert_id AND ALERT.alert_id in (SELECT * alert_id FROM ALERT_GROUP WHERE alert_id = $alid) AND ALERT_GROUP.priority >= (SELECT priority FROM ALERT_GROUP WHERE alert_id = $alid)
0
 
LVL 51

Expert Comment

by:Mark Wills
ID: 21765824
Hi, you pretty much "say" what yht SQL should look like, except the "BUT only if they have a priority..." probably means something else, so will have to clarify that requirement before a final solution... In the meantime, try this:

select *
from alert
where alert_id in (select alert_id from alert_group where  alert_id = $alid and priority >= ???)

can also do join version depending on volume of alert_id's expected...
0
 
LVL 1

Author Comment

by:ja1clark
ID: 21766380
alerts can belong to multiple groups and have different priorities in each group.
The problem is getting group alert ids of  >= priority than $alid in that group
 
To get all required alert_ids is the easy part

select alert_id FROM alert_group WHERE group_id IN
(SELECT group_id from alert_group where alert_id=$alid)

However we only want the alert_ids within each group that are >= to the priority of $alid in that group
=> For Each Group we need to get priority of $alid and then select only alert_ids in that group with a greater priority

If this is still not clear I will post example tables tomorrow AM (PST)
0
Tutorials alone can't teach real engineering

So we built better training tools.

-Hands-on Labs
-Instructor Mentoring
-Scenario-Based Tests
-Dedicated Cloud Servers

All at your fingertips. What are you waiting for?

 
LVL 51

Expert Comment

by:Mark Wills
ID: 21766502
No, that is clear. The priority comes from the priority in alert_group that has alert_id = @alid.

select alert_id FROM alert_group
WHERE group_id IN (SELECT group_id from alert_group where alert_id=$alid)
and Priority >= (SELECT priority from alert_group where alert_id=$alid)
0
 
LVL 1

Author Comment

by:ja1clark
ID: 21770682
(SELECT priority from alert_group where alert_id=$alid) will select the first match of $alid and what we need is the priority of the alert_id that matches $alid  AND matches the specific instance of group_id
e.g.
gid     alert_id      priority
 1          44             1
 1          45             2
 2           7              1
 2         44              2

Using $alid 44 in our query should return 44, 45

reasoning: 44 belongs to group 1 and 2
  Gp1: ( id 44 has priority 1 in group 1) there are alerts 44, 45 with priority  >= 1, return both
  Gp2:  (id 44 has priority 2 in group 2 ) there are alerts 7,44 but only 44 has priority >= 1 do NOT return 7

0
 
LVL 51

Accepted Solution

by:
Mark Wills earned 500 total points
ID: 21770953
Ahhh, thats a bit different to what I had imagined. Alerts can belong to multiple groups !

But, that's OK, just need to add the original group id in the priority check (to get the above scenario)...

SELECT G.group_id,G.alert_id, A.alert_name
FROM alert_group G
inner join alert A on G.alert_id = A.alert_id
WHERE G.group_id IN (SELECT group_id from alert_group where alert_id= $alid)
and G.Priority >= (SELECT priority from alert_group ag where ag.group_id = g.group_id and ag.alert_id= $alid)



0
 
LVL 1

Author Closing Comment

by:ja1clark
ID: 31466305
Thnaks - that works (as far as my testing has gone).
0

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

In this blog post, we’ll look at how using thread_statistics can cause high memory usage.
Backups and Disaster RecoveryIn this post, we’ll look at strategies for backups and disaster recovery.
Video by: Steve
Using examples as well as descriptions, step through each of the common simple join types, explaining differences in syntax, differences in expected outputs and showing how the queries run along with the actual outputs based upon a simple set of dem…
In this video, Percona Director of Solution Engineering Jon Tobin discusses the function and features of Percona Server for MongoDB. How Percona can help Percona can help you determine if Percona Server for MongoDB is the right solution for …

752 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