Solved

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

Posted on 2008-06-11
7
218 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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

 
LVL 51

Expert Comment

by:Mark Wills
Comment Utility
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
Comment Utility
(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
Comment Utility
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
Comment Utility
Thnaks - that works (as far as my testing has gone).
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Read about achieving the basic levels of HRIS security in the workplace.
Never store passwords in plain text or just their hash: it seems a no-brainier, but there are still plenty of people doing that. I present the why and how on this subject, offering my own real life solution that you can implement right away, bringin…
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…

771 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

9 Experts available now in Live!

Get 1:1 Help Now