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

C# Discussion :

Résultat d'un calcul dans une colonne ajoutée d'un listview via requête SQL


Sujet :

C#

  1. #1
    Membre à l'essai
    Homme Profil pro
    Bachelier informatique de gestion
    Inscrit en
    Juillet 2012
    Messages
    40
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : Belgique

    Informations professionnelles :
    Activité : Bachelier informatique de gestion
    Secteur : High Tech - Produits et services télécom et Internet

    Informations forums :
    Inscription : Juillet 2012
    Messages : 40
    Points : 20
    Points
    20
    Par défaut Résultat d'un calcul dans une colonne ajoutée d'un listview via requête SQL
    Bonjour à tous,

    J'arrive à récupérer le résultat de mon calcul dans une boucle mais ensuite je sèche complètement pour afficher ce résultat dans ma colonne pourcentage

    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
     
            public StatPatient()
            {
                InitializeComponent();
                Afficher();
                test();
            }
     
            private void Afficher()
            {
                GestionListe.RemplirListe(Program.BD, listStatP, "SELECT patient.id, COUNT(*) AS Nombre_de_patients, provenance.nom_provenance AS Provenance FROM patient LEFT JOIN provenance ON patient.ref_provenance = provenance.id GROUP BY provenance.id");
                listStatP.Columns.Add("Pourcentage", 80);
            }
     
            private void test() 
            {
                double nbrPT = Program.BD.GetValueDouble("SELECT COUNT(*) FROM patient");
                double nbrEnregistrements = Program.BD.GetValueDouble("SELECT COUNT(*) FROM (SELECT COUNT(*) FROM patient LEFT JOIN provenance ON patient.ref_provenance = provenance.id GROUP BY provenance.id)AS COUNTS");
                double nombre = 0;
                double calcul = 0;
                double position = nbrEnregistrements - 1;
                for (double i = 0; i <= position; i++) 
                {
                    nombre = Program.BD.GetValueDouble("SELECT COUNT(*) FROM patient LEFT JOIN provenance ON patient.ref_provenance = provenance.id GROUP BY provenance.id LIMIT "+i+",1");               
                    calcul = (nombre / nbrPT) * 100;
                    textBox1.Text += calcul.ToString() + "%\r\t";
                }
     
                //string[] texte = textBox1.Text.Split('\t');
                //string pourcentage = texte[0] + "-" + texte[1] + "-" + texte[2];
                //MessageBox.Show('"'+ pourcentage +'"');
            }
    Merci d'avance

  2. #2
    Membre éprouvé
    Avatar de dkmix
    Profil pro
    Inscrit en
    Septembre 2007
    Messages
    619
    Détails du profil
    Informations personnelles :
    Localisation : Jamaïque

    Informations forums :
    Inscription : Septembre 2007
    Messages : 619
    Points : 924
    Points
    924
    Par défaut
    Bonjour,
    pourquoi ne pas le faire en SQL ? et ainsi récupérer les champs dont tu as besoin (y compris le champs calculé)

  3. #3
    Membre à l'essai
    Homme Profil pro
    Bachelier informatique de gestion
    Inscrit en
    Juillet 2012
    Messages
    40
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : Belgique

    Informations professionnelles :
    Activité : Bachelier informatique de gestion
    Secteur : High Tech - Produits et services télécom et Internet

    Informations forums :
    Inscription : Juillet 2012
    Messages : 40
    Points : 20
    Points
    20
    Par défaut
    J'ai essayé ta solution mais il y a encore un soucis ça m'affiche le même pourcentage à chaque ligne :s
    Je n'arrive pas à arranger le problème

    Voici le code

    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
     
            private void Afficher() 
            {
                int nbrPT = Program.BD.GetValueInt("SELECT COUNT(*) FROM patient");
                int nbrEnregistrements = Program.BD.GetValueInt("SELECT COUNT(*) FROM (SELECT COUNT(*) FROM patient LEFT JOIN provenance ON patient.ref_provenance = provenance.id GROUP BY provenance.id)AS COUNTS");
                int nombre = 0;
                double calcul = 0;
                int position = nbrEnregistrements - 1;
                for (int i = 0; i <= position; i++)
                {
                    nombre = Program.BD.GetValueInt("SELECT COUNT(*) FROM patient LEFT JOIN provenance ON patient.ref_provenance = provenance.id GROUP BY provenance.id LIMIT "+i+",1");
                    calcul = (Convert.ToDouble(nombre) / Convert.ToDouble(nbrPT)) * 100;
                    GestionListe.RemplirListe(Program.BD, listStatP, "SELECT patient.id, COUNT(*) AS Nombre_de_patients, provenance.nom_provenance AS Provenance, " + calcul + " AS Pourcentage FROM patient LEFT JOIN provenance ON patient.ref_provenance = provenance.id GROUP BY provenance.id");          
                }
            }
    Merci d'avance

  4. #4
    Membre éprouvé
    Avatar de dkmix
    Profil pro
    Inscrit en
    Septembre 2007
    Messages
    619
    Détails du profil
    Informations personnelles :
    Localisation : Jamaïque

    Informations forums :
    Inscription : Septembre 2007
    Messages : 619
    Points : 924
    Points
    924
    Par défaut
    Pouvez vous mettre la structure des tables SQL (nom de la table + libellé des champs utilisés + clefs étrangères) ainsi que le résultat attendu (nom des colonnes) ?

  5. #5
    Membre à l'essai
    Homme Profil pro
    Bachelier informatique de gestion
    Inscrit en
    Juillet 2012
    Messages
    40
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : Belgique

    Informations professionnelles :
    Activité : Bachelier informatique de gestion
    Secteur : High Tech - Produits et services télécom et Internet

    Informations forums :
    Inscription : Juillet 2012
    Messages : 40
    Points : 20
    Points
    20
    Par défaut
    Bonjour,

    En pièce jointe le résultat attendu, dans la textbox les résultats que je voudrais avoir dans ma colonne pourcentage.

    La structure de ma table patient contient les champs suivants :
    id
    ref_provenance

    table provenance contient juste :
    id
    nom_provenance

    Le problème vient de la dernière requête :

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
                for (int i = 0; i <= position; i++)
                {
                    nombre = Program.BD.GetValueInt("SELECT COUNT(*) FROM patient LEFT JOIN provenance ON patient.ref_provenance = provenance.id GROUP BY provenance.id LIMIT "+i+",1");
                    calcul = (Convert.ToDouble(nombre) / Convert.ToDouble(nbrPT)) * 100;
                    textBox1.Text += calcul.ToString("00.00") + "%\r\n";
                    pourcent = calcul.ToString("00.00");
                    GestionListe.RemplirListe(Program.BD, listStatP, "SELECT patient.id, COUNT(*) AS Nombre_de_patients, provenance.nom_provenance AS Provenance, " + pourcent + " AS Pourcentage FROM patient LEFT JOIN provenance ON patient.ref_provenance = provenance.id GROUP BY provenance.id");          
                }
    Merci d'avance
    Images attachées Images attachées  

  6. #6
    Membre éprouvé
    Avatar de dkmix
    Profil pro
    Inscrit en
    Septembre 2007
    Messages
    619
    Détails du profil
    Informations personnelles :
    Localisation : Jamaïque

    Informations forums :
    Inscription : Septembre 2007
    Messages : 619
    Points : 924
    Points
    924
    Par défaut
    Et avec ceci ?
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
     
    SELECT COUNT(*) AS [Nb par provenance],
     nom_provenance ,
     (CAST(COUNT(*) AS DECIMAL(9,0))/(SELECT COUNT(*) FROM patient))*100 AS pourcentage
    FROM patient p
    JOIN  provenance pr ON p.ref_provenance = pr.id
    GROUP BY nom_provenance

  7. #7
    Membre à l'essai
    Homme Profil pro
    Bachelier informatique de gestion
    Inscrit en
    Juillet 2012
    Messages
    40
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : Belgique

    Informations professionnelles :
    Activité : Bachelier informatique de gestion
    Secteur : High Tech - Produits et services télécom et Internet

    Informations forums :
    Inscription : Juillet 2012
    Messages : 40
    Points : 20
    Points
    20
    Par défaut
    Merci pour ton code je l'ai modifié un peu

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
     
    SELECT patient.id, COUNT(*) AS Nombre_de_patients, provenance.nom_provenance AS Provenance, (CAST(COUNT(*) AS DECIMAL(5,2))/(SELECT COUNT(*) FROM patient))*100 AS Pourcentage FROM patient LEFT JOIN provenance ON patient.ref_provenance = provenance.id GROUP BY provenance.id
    Sais tu comment pourrais je rajouter le signe % après le chiffre et pour les decimal j'essaie d'avoir 2 chiffres après la virgule mais ca ne marche pas

    Merci encore

  8. #8
    Membre éprouvé
    Avatar de dkmix
    Profil pro
    Inscrit en
    Septembre 2007
    Messages
    619
    Détails du profil
    Informations personnelles :
    Localisation : Jamaïque

    Informations forums :
    Inscription : Septembre 2007
    Messages : 619
    Points : 924
    Points
    924
    Par défaut
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
     
    CAST( MonPourcentage AS VARCHAR) +'%'

  9. #9
    Membre à l'essai
    Homme Profil pro
    Bachelier informatique de gestion
    Inscrit en
    Juillet 2012
    Messages
    40
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : Belgique

    Informations professionnelles :
    Activité : Bachelier informatique de gestion
    Secteur : High Tech - Produits et services télécom et Internet

    Informations forums :
    Inscription : Juillet 2012
    Messages : 40
    Points : 20
    Points
    20
    Par défaut
    Je n'y arrive pas je fais une erreur de syntaxe quelque part :s
    J'ai essayé un cast du cast mais ça ne fonctionne pas

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
     
    SELECT patient.id, COUNT(*) AS Nombre_de_patients, provenance.nom_provenance AS Provenance, 
    (CAST(COUNT(*) AS DECIMAL(5,2))/(SELECT COUNT(*) FROM patient)*100) +'%' AS Pourcentage FROM patient LEFT JOIN provenance ON patient.ref_provenance = provenance.id GROUP BY provenance.id

  10. #10
    Membre éprouvé
    Avatar de dkmix
    Profil pro
    Inscrit en
    Septembre 2007
    Messages
    619
    Détails du profil
    Informations personnelles :
    Localisation : Jamaïque

    Informations forums :
    Inscription : Septembre 2007
    Messages : 619
    Points : 924
    Points
    924
    Par défaut
    Ce que vous avez écrit ne veut rien dire....

    Avant d'aller plus loin, il faut comprendre :
    1- GROUP BY
    2- CAST

    Bonne lecture

  11. #11
    Membre à l'essai
    Homme Profil pro
    Bachelier informatique de gestion
    Inscrit en
    Juillet 2012
    Messages
    40
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : Belgique

    Informations professionnelles :
    Activité : Bachelier informatique de gestion
    Secteur : High Tech - Produits et services télécom et Internet

    Informations forums :
    Inscription : Juillet 2012
    Messages : 40
    Points : 20
    Points
    20
    Par défaut
    Merci mais je ne trouve toujours pas malgré de nombreux essais :s

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
     
    SELECT patient.id, COUNT(*) AS Nombre_de_patients, provenance.nom_provenance AS Provenance, (CAST(COUNT(*) AS DECIMAL)/(SELECT COUNT(*) FROM patient)*100) + '%' AS Pourcentage FROM patient LEFT JOIN provenance ON patient.ref_provenance = provenance.id GROUP BY provenance.nom_provenance
    Quelqu'un aurais une idée pour juste rajouter le signe % et éventuellement avoir juste 1 chiffre après la virgule?

    J'en ai absolument besoin pour remettre mon mémoire

    Merci d'avance

  12. #12
    Membre éprouvé
    Avatar de dkmix
    Profil pro
    Inscrit en
    Septembre 2007
    Messages
    619
    Détails du profil
    Informations personnelles :
    Localisation : Jamaïque

    Informations forums :
    Inscription : Septembre 2007
    Messages : 619
    Points : 924
    Points
    924
    Par défaut
    Avez-vous compris l'instruction "GROUP BY" ?

    Vous écrivez :
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    SELECT patient.id,..... GROUP BY provenance.nom_provenance
    Ce n'est pas possible en SQL, si patient.id se trouve dans les champs du SELECT, il doit également se trouver dans la clause GROUP BY.
    Vous ne pouvez pas à la fois ressortir des informations sur 1 patient et sur un groupe de patient.
    On parle de deux choses différentes : 1 patient != 1 groupe de patient.
    Le GROUP BY vous permet d'obtenir des informations pour un groupe de patient.
    Un groupe de patient n'a pas de "patient.id", mais si on veut une somme de patient.id

    Par exemple :
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
     
    SELECT COUNT(*) AS [Nb par provenance],
     nom_provenance ,
    CAST(
    	round(
    		(CAST(COUNT(*) AS float)/(SELECT COUNT(*) FROM patient))*100,
    		2)
    AS VARCHAR
    )+ '%'
    AS pourcentage
    FROM patient p
    JOIN  provenance pr ON p.ref_provenance = pr.id
    GROUP BY nom_provenance
    Vous donne ceci :
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
     
    Nb par provenance	nom_provenance	pourcentage
    4	                      provenance 1	66.67%
    1	                      provenance 2	16.67%
    1	                      provenance 3	16.67%

  13. #13
    Membre à l'essai
    Homme Profil pro
    Bachelier informatique de gestion
    Inscrit en
    Juillet 2012
    Messages
    40
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : Belgique

    Informations professionnelles :
    Activité : Bachelier informatique de gestion
    Secteur : High Tech - Produits et services télécom et Internet

    Informations forums :
    Inscription : Juillet 2012
    Messages : 40
    Points : 20
    Points
    20
    Par défaut
    Oui ça va ça marche, j'ai effectué des modifications dans la requête.
    J'ai du garder le patient.id car sinon ça ne m'affiche rien dans ma listview

    Voici le code
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
     
            public StatPatient()
            {
                InitializeComponent();
                Afficher();
            }
     
            private void Afficher() 
            {
                GestionListe.RemplirListe(Program.BD, listStatP, "SELECT patient.id, COUNT(*) AS Nombre_de_patients, provenance.nom_provenance AS Provenance, CONCAT(CAST(ROUND((CAST(COUNT(*) AS DECIMAL)/(SELECT COUNT(*) FROM patient))*100, 2) AS CHAR), '%') AS Pourcentage FROM patient LEFT JOIN provenance ON patient.ref_provenance = provenance.id GROUP BY provenance.nom_provenance");           
            }
    Encore un grand merci

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

Discussions similaires

  1. [WD15] calcul dans une colonne table
    Par kirikou84 dans le forum WinDev
    Réponses: 13
    Dernier message: 23/08/2012, 18h35
  2. Update calcul dans une colonne petite probleme
    Par mihaispr dans le forum MS SQL Server
    Réponses: 2
    Dernier message: 06/12/2009, 10h24
  3. [XL-97] Ecrire résultat calcul dans une colonne déterminée
    Par Bocage dans le forum Macros et VBA Excel
    Réponses: 2
    Dernier message: 23/04/2009, 18h24
  4. Réponses: 3
    Dernier message: 14/06/2007, 13h35
  5. [VB.NET] Calculs dans une colonne d'un datagridview
    Par boulete dans le forum Windows Forms
    Réponses: 3
    Dernier message: 31/03/2006, 15h11

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