Solved

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

Posted on 2008-06-11
7
221 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
VMware Disaster Recovery and Data Protection

In this expert guide, you’ll learn about the components of a Modern Data Center. You will use cases for the value-added capabilities of Veeam®, including combining backup and replication for VMware disaster recovery and using replication for data center migration.

 
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

Backup Your Microsoft Windows Server®

Backup all your Microsoft Windows Server – on-premises, in remote locations, in private and hybrid clouds. Your entire Windows Server will be backed up in one easy step with patented, block-level disk imaging. We achieve RTOs (recovery time objectives) as low as 15 seconds.

Question has a verified solution.

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

Suggested Solutions

I'm trying, I really am. But I've seen so many wrong approaches involving date(time) boundaries I despair about my inability to explain it. I've seen quite a few recently that define a non-leap year as 364 days, or 366 days and the list goes on. …
Load balancing is the method of dividing the total amount of work performed by one computer between two or more computers. Its aim is to get more work done in the same amount of time, ensuring that all the users get served faster.
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…
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…

895 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

Need Help in Real-Time?

Connect with top rated Experts

14 Experts available now in Live!

Get 1:1 Help Now