473,394 Members | 1,737 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,394 software developers and data experts.

just to do a select

hello, i cant find how to make this select :

here is what i have : 2 tables
Incident(incident_id,incident_name)
action(action_id,incident_id,action_name,dept_id)

what i want?
i would like to find all those incident which have all their action
with dept_id=3.

how can we do this?
Dec 26 '07 #1
3 1268
noumian (n.******@gmail.com) writes:
hello, i cant find how to make this select :

here is what i have : 2 tables
Incident(incident_id,incident_name)
action(action_id,incident_id,action_name,dept_id)

what i want?
i would like to find all those incident which have all their action
with dept_id=3.

how can we do this?
Two ways to skin the cat:

SELECT i.incident_id, i.incident_name
FROM incidents i
WHERE EXISTS (SELECT *
FROM actions a
WHERE a.incident_id = i.incident_id)
AND NOT EXISTS (SELECT *
FROM actions a
WHERE a.incident_id = i.incident_id
AND a.dept_id = 3)

SELECT i.incident_id, i.incident_name
FROM incidents i
JOIN (SELECT incident_id
FROM actions
GROUP incident_id
HAVING COUNT(*) =
SUM(CASE WHEN dept_id = 3 THEN 1 ELSE 0 END)) AS a
ON a.incident_id = i.incident_id


--
Erland Sommarskog, SQL Server MVP, es****@sommarskog.se

Books Online for SQL Server 2005 at
http://www.microsoft.com/technet/pro...ads/books.mspx
Books Online for SQL Server 2000 at
http://www.microsoft.com/sql/prodinf...ons/books.mspx
Dec 26 '07 #2
On Dec 26, 10:23*am, noumian <n.nou...@gmail.comwrote:
hello, i cant find how to make this select :

here is what i have : 2 tables
Incident(incident_id,incident_name)
action(action_id,incident_id,action_name,dept_id)

what i want?
i would like to find all those incident which have all their action
with dept_id=3.

how can we do this?
SELECT *, Incident.incident_name
FROM action LEFT OUTER JOIN
Incident ON action.incident_id =
Incident.incident_id
WHERE (action.dept_id = 3)
Dec 26 '07 #3
On Wed, 26 Dec 2007 04:27:05 -0800 (PST), "ha********@gmail.com"
<ha********@gmail.comwrote:
>On Dec 26, 10:23*am, noumian <n.nou...@gmail.comwrote:
>hello, i cant find how to make this select :

here is what i have : 2 tables
Incident(incident_id,incident_name)
action(action_id,incident_id,action_name,dept_i d)

what i want?
i would like to find all those incident which have all their action
with dept_id=3.

how can we do this?

SELECT *, Incident.incident_name
FROM action LEFT OUTER JOIN
Incident ON action.incident_id =
Incident.incident_id
WHERE (action.dept_id = 3)
That selects where some of the action took place in department 3.

Try

select incident.incident_id, incident_name
from incident inner join action on incident.incident_id =
action.incident_id
group by incident.incident_id, incident_name
where max(dept_id) = 3 and min(dept_id) =3

This presumes dept_id is always filled in.
Jun 27 '08 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: mr_burns | last post by:
hi, how do i do a SELECT statment that will just query a table to find the row with the maximum of a declared field. for example, if i have a table of customers and i would like to get the value...
10
by: Stu | last post by:
I have the following code which I am having difficulty getting to work. I think it may be a problem with the $got query that is being run as if I set that to a set value then my site seems to run...
1
by: Steven Burn | last post by:
I know I could probably write something along the lines of the below to do this but, am hoping there's an easier way. Basically what I am wanting to do is, select an entry in 1 table, then move...
0
by: schaf | last post by:
Hi ! I'm writing a new xsl:function, which uses two other functions. But by the call of the first function, it would be abort just after the call. Not even the xsl:param would be set. I don't...
7
by: x muzuo | last post by:
Hi guys, I have got a prob of javascript form validation which just doesnt work with my ASP code. Can any one help me out please. Here is the code: {////<<head> <title>IIBO Submit Page</title>...
10
by: MLH | last post by:
Suppose the following... Dim A as Date A=#7/24/2005# I wish to compare value of A against 2 other values: 1) 8/1/2005 2) 9/1/2005 Which is better and why... First:
1
by: Bennett Haselton | last post by:
Suppose I add a new row to a table in a dataset, and then I use an OleDbDataAdapter to add that new row to a SQL Server database using OleDbDataAdapter.Update(), as in the following code: ...
9
by: Leszek | last post by:
Hi. How can I get just one row from selected column and put it into html dropdown list I tried like this: function pobierz_wszystko($tabela,$kolumna) { $zapytanie="SELECT $kolumna FROM...
66
by: mensanator | last post by:
Probably just me. I've only been using Access and SQL Server for 12 years, so I'm sure my opinions don't count for anything. I was, nevertheless, looking forward to Sqlite3. And now that gmpy...
2
by: RHNShK | last post by:
SQL> ED Wrote file afiedt.buf 1 select ROUND(avgprice, 10) avgprice, SeQ_no 2 from (select b.*, 3 sum(decode(INVXH_code, 'BUY', invxh_TOTAL_QTY, -invxh_TOTAL_QTY))...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.