PHWinfo banniere

Titres
PORTAIL ANNUAIRE ARTICLES COMPARATEUR HÉBERGEURS DEVIS FORUMS RÉDUCTEUR D'URL
Précédent   PHWinfo > Forums Hébergement > Forum Hébergement serveur > comp.db.ms-sqlserver > just to do a select
S'inscrire FAQ Membres Recherche Messages du jour Marquer les forums comme lus
just to do a select

Réponse
 
LinkBack Outils de la discussion
Vieux 26/12/2007, 08h23   #1
noumian
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut 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?
  Réponse avec citation
Vieux 26/12/2007, 09h21   #2
Erland Sommarskog
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: just to do a select

noumian (n.noumia@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, esquel@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
  Réponse avec citation
Vieux 26/12/2007, 12h27   #3
harry9katz@gmail.com
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: just to do a select

On Dec 26, 10:23am, noumian <n.nou...@gmail.com> wrote:
> 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)
  Réponse avec citation
Vieux 19/05/2008, 08h56   #4
Iain Sharp
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: just to do a select

On Wed, 26 Dec 2007 04:27:05 -0800 (PST), "harry9katz@gmail.com"
<harry9katz@gmail.com> wrote:

>On Dec 26, 10:23am, noumian <n.nou...@gmail.com> wrote:
>> 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)


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.
  Réponse avec citation
Réponse


Outils de la discussion

Règles de messages
Vous ne pouvez pas créer de nouvelles discussions
Vous ne pouvez pas envoyer des réponses
Vous ne pouvez pas envoyer des pièces jointes
Vous ne pouvez pas modifier vos messages

Les balises BB sont activées : oui
Les smileys sont activés : oui
La balise [IMG] est activée : oui
Le code HTML peut être employé : non
Trackbacks are oui
Pingbacks are oui
Refbacks are oui


Fuseau horaire GMT +1. Il est actuellement 11h36.


Édité par : vBulletin® version 3.7.3
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.2.0 RC5 Tous droits réservés.
Version française #16 par l'association vBulletin francophone
PHWinfo est un site Éducation Sans Frontières ©2000-2008
Ad Management by RedTyger
©Tous droits réservés par les parties respectives
Page generated in 0,12387 seconds with 12 queries