?
Solved

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

Posted on 2008-06-11
7
Medium Priority
?
237 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
  • 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
Industry Leaders: 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!

 
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

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Question has a verified solution.

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

In today's business world, data is more important than ever for informing marketing campaigns. Accessing and using data, however, may not come naturally to some creative marketing professionals. Here are four tips for adapting to wield data for insi…
Creating a Cordova application which allow user to save to/load from his Dropbox account the application database.
In this video, Percona Solutions Engineer Barrett Chambers discusses some of the basic syntax differences between MySQL and MongoDB. To learn more check out our webinar on MongoDB administration for MySQL DBA: https://www.percona.com/resources/we…
With just a little bit of  SQL and VBA, many doors open to cool things like synchronize a list box to display data relevant to other information on a form.  If you have never written code or looked at an SQL statement before, no problem! ...  give i…
Suggested Courses

840 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