IdentifiantMot de passe
Loading...
Mot de passe oublié ?Je m'inscris ! (gratuit)
Navigation

Inscrivez-vous gratuitement
pour pouvoir participer, suivre les réponses en temps réel, voter pour les messages, poser vos propres questions et recevoir la newsletter

MS SQL Server Discussion :

where sur 2 colonnes en même temps


Sujet :

MS SQL Server

  1. #1
    Membre du Club
    Profil pro
    Inscrit en
    Novembre 2004
    Messages
    94
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Novembre 2004
    Messages : 94
    Points : 67
    Points
    67
    Par défaut where sur 2 colonnes en même temps
    bonjour, dans une requète je cherche a faire un where
    sur deux colonnes en meme temps
    est-ce possible ?
    et si oui
    pourriez vous m'indiquer mon erreur merci!
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
     
    table a ( champ a, champ b, chmap c, champ d)
     
    select *
    from table a
    where b,c
    not in (
    select b,c from table a
    where d=conditions;
    )

  2. #2
    Membre averti
    Inscrit en
    Mars 2004
    Messages
    425
    Détails du profil
    Informations forums :
    Inscription : Mars 2004
    Messages : 425
    Points : 358
    Points
    358
    Par défaut
    Je te conseillerais d'utiliser NOT EXISTS
    OS:Win 2000 Pro, WIN XP
    SGBD: MS Sql Server, Oracle
    Environnement: VS.NET 2002, JBuilder
    Web: www.ndestudents.com

  3. #3
    Membre du Club
    Profil pro
    Inscrit en
    Novembre 2004
    Messages
    94
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Novembre 2004
    Messages : 94
    Points : 67
    Points
    67
    Par défaut
    je ne comprend pas mon erreur

    table a ( champ a, champ b, chmap c, champ d)

    select b,c
    from table a
    where
    not exist (
    select b,c from table a
    where d=conditions;
    )

    me renvoie rien
    cela signifie quoi ce not exist?

  4. #4
    Membre averti
    Inscrit en
    Mars 2004
    Messages
    425
    Détails du profil
    Informations forums :
    Inscription : Mars 2004
    Messages : 425
    Points : 358
    Points
    358
    Par défaut
    Est ce que tu peux nous envoyer la structure de tes tables ainsi que les données et le résultat que tu souhaites obtenir?
    OS:Win 2000 Pro, WIN XP
    SGBD: MS Sql Server, Oracle
    Environnement: VS.NET 2002, JBuilder
    Web: www.ndestudents.com

  5. #5
    Rédacteur

    Avatar de SQLpro
    Homme Profil pro
    Expert bases de données / SQL / MS SQL Server / Postgresql
    Inscrit en
    Mai 2002
    Messages
    21 839
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France, Var (Provence Alpes Côte d'Azur)

    Informations professionnelles :
    Activité : Expert bases de données / SQL / MS SQL Server / Postgresql
    Secteur : Conseil

    Informations forums :
    Inscription : Mai 2002
    Messages : 21 839
    Points : 52 932
    Points
    52 932
    Billets dans le blog
    5
    Par défaut
    Cette syntaxe :

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    select *
    from   tableA
    where (b,c) not in (select b, c 
                        from   tableA
                        where  d = conditions)
    Serait possible si MS SQL Server implémentais le "Row Value Constructeur" ce qui n'est pas le cas (a lire : http://sqlpro.developpez.com/cours/sqlaz/select/#L8)

    Cependant il est possible de transformer ceci avec le EXISTS :

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    select *
    from   tableA T1
    where  NOT EXISTS(select *
                      from   tableA T2
                      where  d = conditions
                        AND  T1.b = T2.b
                        AND  T1.c = T2.c)
    Mais il serait intelligent que tu donne les script de création de tes tables et un exemples. Lit ceci : http://www.developpez.net/forums/viewtopic.php?t=32668

    A +
    Frédéric Brouard - SQLpro - ARCHITECTE DE DONNÉES - expert SGBDR et langage SQL
    Le site sur les SGBD relationnels et le langage SQL: http://sqlpro.developpez.com/
    Blog SQL, SQL Server, SGBDR : http://blog.developpez.com/sqlpro
    Expert Microsoft SQL Server - M.V.P. (Most valuable Professional) MS Corp.
    Entreprise SQL SPOT : modélisation, conseils, audit, optimisation, formation...
    * * * * * Expertise SQL Server : http://mssqlserver.fr/ * * * * *

  6. #6
    Membre du Club
    Profil pro
    Inscrit en
    Novembre 2004
    Messages
    94
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Novembre 2004
    Messages : 94
    Points : 67
    Points
    67
    Par défaut
    dans cette table uniquement
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    CREATE TABLE [NIV_RELEVE] (
    	[ID] [int] IDENTITY (1, 1) NOT NULL ,
    	[Compartiment_NOM] [varchar] (20) COLLATE //nom d'une cuve
    	[Horodatage] [datetime] NOT NULL ,  // heure de relevé du niveau
    	[Valeur] [float] NOT NULL ,     // quantité
    	[TypeHuile_NOM] [varchar] (20) //    type d'hule
    	[StatutOPC] [int] NULL , // statut du releve

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    SELECT  sum (Valeur) as Valeur,TypeHuile_NOM 
    FROM TRACA_FLUIDE.dbo.NIV_RELEVE  
    where Horodatage >= '26/10/2005 15:11:00' 
    and Horodatage <= '27/10/2005 10:11:00' 
    group by TypeHuile_NOM, Horodatage
    ca me renvoie la somme et le type d'huile par période de temps
    mais maintenant je dois faire la m^me chose sauf que
    je ne veux pas calculer la somme pour un type d'huile si
    une des compartiements de ce type a un statutOPc différent de 192
    par exemple pour le ligne 3 (derniere colonne)

    donc pour la somme du type HYDO SAE 10W a la date 10/27/2005 10:00:02 je ne devrai spas calculer ces données



    exemple de données sur 2 période de temps

    331 BEO55.1 10/27/2005 10:00:02 AM 25.5055385794996 HYDO SAE 10W 192
    332 BEO55.2 10/27/2005 10:00:02 AM 17.0036923863331 HYDO SAE 10W 192
    333 BEO56.1 10/27/2005 10:00:02 AM 12.7527692897498 HYDO SAE 10W 0
    334 BEO56.2 10/27/2005 10:00:02 AM 29.7564616760829 TDTO SAE 30 192
    335 BEO57.1 10/27/2005 10:00:02 AM 12.7527692897498 HYDO SAE 10W 192
    336 BEO57.2 10/27/2005 10:00:02 AM 8.50184619316653 TDTO SAE 50 192
    337 BEO57.3 10/27/2005 10:00:02 AM 8.50184619316653 TDTO SAE 30 192
    338 BEO57.4 10/27/2005 10:00:02 AM 12.7527692897498 PANOLIN 192
    323 BEO55.1 10/27/2005 9:00:02 AM 24.5552570453314 HYDO SAE 10W 192
    324 BEO55.2 10/27/2005 9:00:02 AM 16.3701713635543 HYDO SAE 10W 192
    325 BEO56.1 10/27/2005 9:00:02 AM 12.2776285226657 HYDO SAE 10W 192
    326 BEO56.2 10/27/2005 9:00:02 AM 28.64779988622 TDTO SAE 30 192
    327 BEO57.1 10/27/2005 9:00:02 AM 12.2776285226657 HYDO SAE 10W 192
    328 BEO57.2 10/27/2005 9:00:02 AM 8.50184619316653 TDTO SAE 50 192
    329 BEO57.3 10/27/2005 9:00:02 AM 8.50184619316653 TDTO SAE 30 192
    330 BEO57.4 10/27/2005 9:00:02 AM 12.7527692897498 PANOLIN 192


    j'ai essayé ca mais ca ne passe pas....
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    select id ,Horodatage
    from TRACA_FLUIDE.dbo.NIV_RELEVE  
    where Horodatage >= '26/10/2005 15:11:00' 
    and Horodatage <= '27/10/2005 10:11:00' 
    and [ horodatage, TypeHuile_NOM ] in (	select Horodatage, TypeHuile_NOM
    	from TRACA_FLUIDE.dbo.NIV_RELEVE  
    	where Horodatage >= '26/10/2005 15:11:00' 
    	and Horodatage <= '27/10/2005 10:11:00' 
    	and StatutOPC <>'192' )
    order by Horodatage desc
    ou alors avec l'existe :
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
     
    SELECT A.Horodatage,A.TypeHuile_NOM , A.id, A.statutopc
    FROM TRACA_FLUIDE.dbo.NIV_RELEVE  A, TRACA_FLUIDE.dbo.NIV_RELEVE  B
    where A.Horodatage >= '26/10/2005 15:11:00' 
    and A.Horodatage <= '27/10/2005 10:11:00' 
    and B.Horodatage >= '26/10/2005 15:11:00' 
    and B.Horodatage <= '27/10/2005 10:11:00' 
    and not exist (
    	select * -- B.Horodatage,B.TypeHuile_NOM
    	FROM TRACA_FLUIDE.dbo.NIV_RELEVE  
    	where Horodatage >= '26/10/2005 15:11:00' 
    	and Horodatage <= '27/10/2005 10:11:00' 
    	and B.statutopc<>192
    	and  A.Horodatage =  B.Horodatage
    	and A.TypeHuile_NOM=B.TypeHuile_NOM
    )
    mais pour les deux j'ai un pb de syntaxe je ne sais pas comment m'y prendre merci pour ton aide!

    ps je suis sur sqlserveur

  7. #7
    Rédacteur

    Avatar de SQLpro
    Homme Profil pro
    Expert bases de données / SQL / MS SQL Server / Postgresql
    Inscrit en
    Mai 2002
    Messages
    21 839
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France, Var (Provence Alpes Côte d'Azur)

    Informations professionnelles :
    Activité : Expert bases de données / SQL / MS SQL Server / Postgresql
    Secteur : Conseil

    Informations forums :
    Inscription : Mai 2002
    Messages : 21 839
    Points : 52 932
    Points
    52 932
    Billets dans le blog
    5
    Par défaut
    EXISTS avec un S à la fin
    De plus tu fais un produit cartésien !


    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    SELECT A.Horodatage, A.TypeHuile_NOM, A.id, A.statutopc
    FROM   NIV_RELEVE A      
    where  A.Horodatage BETWEEN '26/10/2005 15:11:00' AND '27/10/2005 10:11:00'
      and  not exists (select * 
                       FROM  NIV_RELEVE B
                       where B.Horodatage BETWEEN '26/10/2005 15:11:00' AND '27/10/2005 10:11:00'
                         and B.statutopc<>192
                         and   A.Horodatage    = B.Horodatage
                         and   A.TypeHuile_NOM = B.TypeHuile_NOM)
    A +
    Frédéric Brouard - SQLpro - ARCHITECTE DE DONNÉES - expert SGBDR et langage SQL
    Le site sur les SGBD relationnels et le langage SQL: http://sqlpro.developpez.com/
    Blog SQL, SQL Server, SGBDR : http://blog.developpez.com/sqlpro
    Expert Microsoft SQL Server - M.V.P. (Most valuable Professional) MS Corp.
    Entreprise SQL SPOT : modélisation, conseils, audit, optimisation, formation...
    * * * * * Expertise SQL Server : http://mssqlserver.fr/ * * * * *

  8. #8
    Membre du Club
    Profil pro
    Inscrit en
    Novembre 2004
    Messages
    94
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Novembre 2004
    Messages : 94
    Points : 67
    Points
    67
    Par défaut
    C'est exactement ca!!
    merci beaucoup!!

+ Répondre à la discussion
Cette discussion est résolue.

Discussions similaires

  1. Réponses: 2
    Dernier message: 19/07/2006, 19h45
  2. [MySQL] Comment se connecter sur 2 BD en même temps ?
    Par cassy dans le forum PHP & Base de données
    Réponses: 9
    Dernier message: 26/05/2006, 12h57
  3. Requête sur deux tables en même temps
    Par will89 dans le forum Requêtes
    Réponses: 2
    Dernier message: 20/04/2006, 11h01
  4. Tri sur deux champs en même temps
    Par Azharis dans le forum Access
    Réponses: 8
    Dernier message: 11/01/2006, 13h10
  5. Comptez sur deux tables en même temps
    Par genova dans le forum Langage SQL
    Réponses: 12
    Dernier message: 13/09/2004, 18h58

Partager

Partager
  • Envoyer la discussion sur Viadeo
  • Envoyer la discussion sur Twitter
  • Envoyer la discussion sur Google
  • Envoyer la discussion sur Facebook
  • Envoyer la discussion sur Digg
  • Envoyer la discussion sur Delicious
  • Envoyer la discussion sur MySpace
  • Envoyer la discussion sur Yahoo