Solved

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

Posted on 2008-06-11
7
231 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
Major Incident Management Communications

Major incidents and IT service outages cost companies millions. Often the solution to minimizing damage is automated communication. Find out more in our Major Incident Management Communications infographic.

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

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

These days, all we hear about hacktivists took down so and so websites and retrieved thousands of user’s data. One of the techniques to get unauthorized access to database is by performing SQL injection. This article is quite lengthy which gives bas…
When it comes to protecting Oracle Database servers and systems, there are a ton of myths out there. Here are the most common.
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…
This is a high-level webinar that covers the history of enterprise open source database use. It addresses both the advantages companies see in using open source database technologies, as well as the fears and reservations they might have. In this…

687 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