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

Langage SQL Discussion :

Probleme de group by


Sujet :

Langage SQL

  1. #1
    Membre émérite
    Avatar de Reskibil
    Profil pro
    Inscrit en
    Mai 2007
    Messages
    561
    Détails du profil
    Informations personnelles :
    Âge : 43
    Localisation : France, Paris (Île de France)

    Informations forums :
    Inscription : Mai 2007
    Messages : 561
    Par défaut Probleme de group by
    Bonjour,

    J'ai cette requête :

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    SELECT     dbo.[Dim Company].GKA, 
    [Country Code], 
    [Reporting Line], 
    CASE WHEN left([Proposal Type],3)='New' then 'New Proposal' WHEN left([Proposal Type],3)='Ren' then 'Renewal' else null end as [Type], 
    CASE WHEN LEFT([Proposal Status], 3) >= '020' THEN 'Issued' ELSE NULL END AS [Status], 
     YEAR([Issuing Date]) AS Year,
    FROM         dbo.[Fact Proposal] INNER JOIN
                          dbo.[Dim Company] ON dbo.[Fact Proposal].Company_Key = dbo.[Dim Company].Company_Key
    WHERE LEFT([Proposal Status], 3) >= '020'
    and LEFT([Proposal Type], 3)='New'
    group by dbo.[Dim Company].GKA, dbo.[Fact Proposal].[Country Code], dbo.[Fact Proposal].[Reporting Line]
    ,YEAR([Issuing Date]),left([Proposal Type],3),LEFT([Proposal Status], 3)
    Elle marche bien à part pour les [Proposal Type]. Ce champ peut avoir plusieurs valeurs (New Proposal-Existing Client, New Proposal-New Client, etc...) et je ne voudrais que [New Proposal] (le case au début quoi). Le probleme c'est qu'au niveau du group by, les lignes ne sont pas aggrégée à ce niveau. J'ai donc des doublons. N'étant pas très très doué en SQL, j'ai donc besoin d'un petit coup de main.

    Merci d'avance

  2. #2
    Modérateur
    Avatar de al1_24
    Homme Profil pro
    Retraité
    Inscrit en
    Mai 2002
    Messages
    9 134
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 64
    Localisation : France, Val de Marne (Île de France)

    Informations professionnelles :
    Activité : Retraité
    Secteur : High Tech - Éditeur de logiciels

    Informations forums :
    Inscription : Mai 2002
    Messages : 9 134
    Par défaut
    il suffit de rappeler dans le GROUP BY les termes de l'expression à regrouper :
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    ...
    group by dbo.[Dim Company].GKA, 
    dbo.[Fact Proposal].[Country Code], 
    dbo.[Fact Proposal].[Reporting Line], 
    YEAR([Issuing Date]),
    CASE WHEN left([Proposal Type],3)='New' then 'New Proposal' WHEN left([Proposal Type],3)='Ren' then 'Renewal' else null end as [Type], 
    CASE WHEN LEFT([Proposal Status], 3) >= '020' THEN 'Issued' ELSE NULL END AS [Status]
    Modérateur Langage SQL
    Règles du forum Langage SQL à lire par tous, N'hésitez pas à consulter les cours SQL
    N'oubliez pas le bouton et pensez aux balises
    [code]
    Si une réponse vous a aidé à résoudre votre problème, n'oubliez pas de voter pour elle en cliquant sur
    Aide-toi et le forum t'aidera : Un problème exposé sans mentionner les tentatives de résolution infructueuses peut laisser supposer que le posteur attend qu'on fasse son travail à sa place... et ne donne pas envie d'y répondre.

  3. #3
    Membre émérite
    Avatar de Reskibil
    Profil pro
    Inscrit en
    Mai 2007
    Messages
    561
    Détails du profil
    Informations personnelles :
    Âge : 43
    Localisation : France, Paris (Île de France)

    Informations forums :
    Inscription : Mai 2007
    Messages : 561
    Par défaut
    Merci pour la réponse et désolé pour mon retard mais j'étais parti sur tout autre chose.

    Bon en gros ca marche pas.

    Voila la requete complete :
    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
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    SELECT     dbo.[Dim Company].GKA, 
    [ZIG Number], 
    [Country Code], 
    [Reporting Line], 
    CASE WHEN left([Proposal Type],3)='New' then 'New Proposal' 
    WHEN left([Proposal Type],3)='Ren' then 'Renewal' else null end as [Type], 
    CASE WHEN LEFT([Proposal Status], 3) >= '020' THEN 'Issued' ELSE NULL END AS [Status], 
    YEAR([Issuing Date]) AS Year,
    Case when MONTH([Issuing Date])=1 then count(*) else 0 end as [Nb1], 
    Case when MONTH([Issuing Date])=2 then count(*) else 0 end as [Nb2],
    Case when MONTH([Issuing Date])=3 then count(*) else 0 end as [Nb3],
    Case when MONTH([Issuing Date])=4 then count(*) else 0 end as [Nb4],
    Case when MONTH([Issuing Date])=5 then count(*) else 0 end as [Nb5],
    Case when MONTH([Issuing Date])=6 then count(*) else 0 end as [Nb6],
    Case when MONTH([Issuing Date])=7 then count(*) else 0 end as [Nb7],
    Case when MONTH([Issuing Date])=8 then count(*) else 0 end as [Nb8],
    Case when MONTH([Issuing Date])=9 then count(*) else 0 end as [Nb9],
    Case when MONTH([Issuing Date])=10 then count(*) else 0 end as [Nb10],
    Case when MONTH([Issuing Date])=11 then count(*) else 0 end as [Nb11],
    Case when MONTH([Issuing Date])=12 then count(*) else 0 end as [Nb12],
    Case when MONTH([Issuing Date])=1 then sum(dbo.[Fact Proposal].[Proposal Amount (Euro)]) else 0 end as [Amount1], 
    Case when MONTH([Issuing Date])=2 then sum(dbo.[Fact Proposal].[Proposal Amount (Euro)]) else 0 end as [Amount2],
    Case when MONTH([Issuing Date])=3 then sum(dbo.[Fact Proposal].[Proposal Amount (Euro)]) else 0 end as [Amount3],
    Case when MONTH([Issuing Date])=4 then sum(dbo.[Fact Proposal].[Proposal Amount (Euro)]) else 0 end as [Amount4],
    Case when MONTH([Issuing Date])=5 then sum(dbo.[Fact Proposal].[Proposal Amount (Euro)]) else 0 end as [Amount5],
    Case when MONTH([Issuing Date])=6 then sum(dbo.[Fact Proposal].[Proposal Amount (Euro)]) else 0 end as [Amount6],
    Case when MONTH([Issuing Date])=7 then sum(dbo.[Fact Proposal].[Proposal Amount (Euro)]) else 0 end as [Amount7],
    Case when MONTH([Issuing Date])=8 then sum(dbo.[Fact Proposal].[Proposal Amount (Euro)]) else 0 end as [Amount8],
    Case when MONTH([Issuing Date])=9 then sum(dbo.[Fact Proposal].[Proposal Amount (Euro)]) else 0 end as [Amount9],
    Case when MONTH([Issuing Date])=10 then sum(dbo.[Fact Proposal].[Proposal Amount (Euro)]) else 0 end as [Amount10],
    Case when MONTH([Issuing Date])=11 then sum(dbo.[Fact Proposal].[Proposal Amount (Euro)]) else 0 end as [Amount11],
    Case when MONTH([Issuing Date])=12 then sum(dbo.[Fact Proposal].[Proposal Amount (Euro)]) else 0 end as [Amount12],
    Case when MONTH([Issuing Date])=1 then sum(dbo.[Fact Proposal].[Proposal Amount (Euro)]*[CAT N]/100) else 0 end as [AmountCAT1], 
    Case when MONTH([Issuing Date])=2 then sum(dbo.[Fact Proposal].[Proposal Amount (Euro)]*[CAT N]/100) else 0 end as [AmountCAT2],
    Case when MONTH([Issuing Date])=3 then sum(dbo.[Fact Proposal].[Proposal Amount (Euro)]*[CAT N]/100) else 0 end as [AmountCAT3],
    Case when MONTH([Issuing Date])=4 then sum(dbo.[Fact Proposal].[Proposal Amount (Euro)]*[CAT N]/100) else 0 end as [AmountCAT4],
    Case when MONTH([Issuing Date])=5 then sum(dbo.[Fact Proposal].[Proposal Amount (Euro)]*[CAT N]/100) else 0 end as [AmountCAT5],
    Case when MONTH([Issuing Date])=6 then sum(dbo.[Fact Proposal].[Proposal Amount (Euro)]*[CAT N]/100) else 0 end as [AmountCAT6],
    Case when MONTH([Issuing Date])=7 then sum(dbo.[Fact Proposal].[Proposal Amount (Euro)]*[CAT N]/100) else 0 end as [AmountCAT7],
    Case when MONTH([Issuing Date])=8 then sum(dbo.[Fact Proposal].[Proposal Amount (Euro)]*[CAT N]/100) else 0 end as [AmountCAT8],
    Case when MONTH([Issuing Date])=9 then sum(dbo.[Fact Proposal].[Proposal Amount (Euro)]*[CAT N]/100) else 0 end as [AmountCAT9],
    Case when MONTH([Issuing Date])=10 then sum(dbo.[Fact Proposal].[Proposal Amount (Euro)]*[CAT N]/100) else 0 end as [AmountCAT10],
    Case when MONTH([Issuing Date])=11 then sum(dbo.[Fact Proposal].[Proposal Amount (Euro)]*[CAT N]/100) else 0 end as [AmountCAT11],
    Case when MONTH([Issuing Date])=12 then sum(dbo.[Fact Proposal].[Proposal Amount (Euro)]*[CAT N]/100) else 0 end as [AmountCAT12]
    FROM         dbo.[Fact Proposal] INNER JOIN
                          dbo.[Dim Company] ON dbo.[Fact Proposal].Company_Key = dbo.[Dim Company].Company_Key
    WHERE [GKA] <>'' and [GKA]IS NOT NULL and LEFT([Proposal Status], 3) >= '020'
    group by dbo.[Dim Company].GKA, 
    dbo.[Dim Company].[ZIG Number], 
    dbo.[Fact Proposal].[Country Code], 
    dbo.[Fact Proposal].[Reporting Line],
    CASE WHEN left([Proposal Type],3)='New' then 'New Proposal' 
              WHEN left([Proposal Type],3)='Ren' then 'Renewal' else null end,
    CASE WHEN LEFT([Proposal Status], 3) >= '020' THEN 'Issued' ELSE NULL END, [Issuing Date]
    Le probleme semble venir du [Issuing Date] car meme avec ca les group, je continue d'avoir des lignes non groupées.

    En fait je pense qu'il me groupe les lignes par month([Issuing Date]) alors que je voudrais que ce soit fait par Year.

    J'arrive en 2 temps (2 vues quoi) mais ce serait mieux en une seule.

    Merci d'avance.

    Petite précision : C'est destiné à SQL Server 2005

Discussions similaires

  1. [CR XI] Probleme de groupe
    Par tatayoyo dans le forum SAP Crystal Reports
    Réponses: 9
    Dernier message: 22/03/2007, 14h18
  2. bonjour probleme de groupe
    Par CROSS dans le forum Oracle
    Réponses: 3
    Dernier message: 04/12/2006, 22h42
  3. probleme avec group by
    Par dirtyjs dans le forum Access
    Réponses: 1
    Dernier message: 01/09/2006, 03h40
  4. probleme de group by
    Par logone dans le forum Requêtes
    Réponses: 5
    Dernier message: 24/06/2006, 10h43
  5. Probleme de GROUP BY
    Par Golork dans le forum Access
    Réponses: 2
    Dernier message: 02/06/2006, 19h46

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