Visualizzazione dei risultati da 1 a 10 su 10

Discussione: SQL problema

  1. #1

    SQL problema

    Buon giorno, ho questo problema.
    In una select devo eseguire non solo delle somme.
    Per il momento ho fatto cosi ma devo unirle e non so come fare:
    codice:
    //select 1
    SELECT 
    SUM(Quantita_unita_A) as Q_Unita_A,
    	from TB_A
    	where Prefisso_A IN ('1','2','3','4','5') and
    	Q_A = '204' and F_A = '1' and S_A = 'a'
    
    //select 2
    SELECT 
    SUM(Quantita_unita_B) as Q_Unita_B,
    	from TB_A
    	where Prefisso_A IN ('6','7') and
    	Q_A = '204' and F_A = '1' and S_A = 'a'
    
    //select 3
    SELECT 
    SUM(Quantita_unita_C) as Q_Unita_C,
    	from TB_A
    	where Q_A = '204' and F_A = '1' and S_A = 'a'
    Questa discriminante (Q_A = '204' and F_A = '1' and S_A = 'a') è uguale per tutti, l'unica che varia è nel Prefisso..

    In pratica dovrei unire le tre Select ed ottenere un solo record con le relative somme..

    Come faccio?
    Grazie mille

  2. #2
    se fai...

    //select 1
    SELECT
    SUM(Quantita_unita_A) as Q_Unita_A,
    from TB_A
    where Prefisso_A IN ('1','2','3','4','5') and
    Q_A = '204' and F_A = '1' and S_A = 'a'


    UNION


    //select 2
    SELECT
    SUM(Quantita_unita_B) as Q_Unita_B,
    from TB_A
    where Prefisso_A IN ('6','7') and
    Q_A = '204' and F_A = '1' and S_A = 'a'



    ti vengono due righe con i due totali

  3. #3
    quante UNION posso utilizzare?
    Perchè al momento devo unire 3 Select, ma potrebbero essere di più VVoVe:

    codice:
    SELECT 
    	SUM(Quantita_A) as Q_A
    	from TB_A
    	where Prefisso_A IN ('1','2','3','4','5') and
    	Q_A = '204' and F_A = '1' and S_ = 'a'
    			
    UNION
    			
    SELECT 
    	SUM(Quantita_B) as Q_B,
    	SUM(Quantita_C) as Q_C 
                 from TB_A
    	where 	Q_A = '204' and F_A = '1' and S_ = 'a'
    				
    UNION
    				
    SELECT 
    	SUM(Quantita_D) as Q_D
                 from TB_A
    	where Prefisso_D IN ('6','7') and
    	Q_A = '204' and F_A = '1' and S_ = 'a'
    Facendo cosi mi restituisce questo errore:
    The used SELECT statements have a different number of columns,
    Perchè ho aggiunto questa SUM(Quantita_C) as Q_C.
    Quindi come faccio a fare la SUM(Quantita_C) as Q_C, devo fare un'altra select?
    Cosi per tutte le Sum che verranno?Oppure le posso inserire all'interno di una sola select?

    grazie ancora...

  4. #4
    adesso ho fatto in qyesto modo, ho tolto il campo in più, ma purtroppo anche non restituendomi errori, non mi da risultati...faccio cosi:
    codice:
    $qTot = ("		SELECT 
    	SUM(Quantita_A) as Q_A
    	from TB_A
    	where Prefisso_A IN ('1','2','3','4','5') and
    	Q_A = '204' and F_A = '1' and S_ = 'a'
    			
    UNION
    			
    SELECT 
    	SUM(Quantita_B) as Q_B,
                 from TB_A
    	where 	Q_A = '204' and F_A = '1' and S_ = 'a'
    				
    UNION
    				
    SELECT 
    	SUM(Quantita_D) as Q_D
                 from TB_A
    	where Prefisso_D IN ('6','7') and
    	Q_A = '204' and F_A = '1' and S_ = 'a');
    
    
    $row = mysql_query($qTot);
    while ($Riga = mysql_fetch_array ($row)){
                 $nQ_A= $Riga['Q_A'];
    	$nQ_B= $Riga['Q_B'];
    	$nQ_D= $Riga['Q_D'];
    }
    echo $nQ_A . "
    ";
    echo $nQ_B . "
    ";
    echo $nQ_D . "
    ";
    Perchè?
    grazie ancora

  5. #5
    Allora facendo union avrai una colonna e 3 righe... quindi.. non mettere as Q_A, Q_B ecc...

    usa sempre Q_A

  6. #6
    grazie per la risposta, ma purtroppo non funziona, almeno per me....
    ho sostituito gli as Q_B,Q_C con Q_A cosi:

    codice:
    $qTot = ("		SELECT 
    	SUM(Quantita_A) as Q_A
    	from TB_A
    	where Prefisso_A IN ('1','2','3','4','5') and
    	Q_A = '204' and F_A = '1' and S_ = 'a'
    			
    UNION
    			
    SELECT 
    	SUM(Quantita_B) as Q_A,
                 from TB_A
    	where 	Q_A = '204' and F_A = '1' and S_ = 'a'
    				
    UNION
    				
    SELECT 
    	SUM(Quantita_D) as Q_A
                 from TB_A
    	where Prefisso_D IN ('6','7') and
    	Q_A = '204' and F_A = '1' and S_ = 'a');
    
    
    $row = mysql_query($qTot);
    while ($Riga = mysql_fetch_array ($row)){
                 $nQ_A= $Riga['Q_A'];
    }
    echo $nQ_A;
    Anche cosi non funziona....dove sbaglio?
    Grazie ancora

  7. #7
    dentro il ciclo while stampati dei valori significativi...

    while ($Riga = mysql_fetch_array ($row)){
    echo '
    valore ' .$Riga['Q_A'];


    $nQ_A= $Riga['Q_A'];
    }

  8. #8
    ho fatto cosi:
    codice:
    $Q_A = '';
    while ($Riga = mysql_fetch_array ($row)){
    	$Q_A .=  $Riga['Q_A'].",";
    }
    $Q_A = substr($Q_A, 0, -1);
    $Q_A = explode(",",$Q_A);
    echo "a: ".$Q_A[0]."</br>";
    echo "b: ".$Q_A[1]."</br>";
    echo "c: ".$Q_A[2]."</br>";
    Cosi funziona...però ho una curiosità!!!

    Utilizzando union, devo, per ogni SUM o risultato qualsiasi, utilizzare tante select quante ne occorrono, oppure posso ottimizzare in questo modo?
    Perchè in pratica devo fare 14 SUM, ce ne sono alcune che non utilizzano nessuna discrimintante di ricerca particolare, e quindi le vorrei inserire all'interno di una sola select, però mi restituisce questo errore:
    "The used SELECT statements have a different number of columns"

    codice:
    SELECT 
    	SUM(Quantita_A) as Q_A
    	from TB_A
    	where Prefisso_A IN ('1','2','3','4','5') and
    	Q_A = '204' and F_A = '1' and S_ = 'a'
    			
    UNION
    			
    SELECT 
    	SUM(Quantita_B) as Q_A,
                 SUM(Quantita_C) as Q_A
                 from TB_A
    	where 	Q_A = '204' and F_A = '1' and S_ = 'a'
    				
    UNION
    				
    SELECT 
    	SUM(Quantita_D) as Q_A
                 from TB_A
    	where Prefisso_D IN ('6','7') and
    	Q_A = '204' and F_A = '1' and S_ = 'a'
    Quindi?Come mi comporto?
    Grazie ancora

  9. #9
    Se una select ha 8 sum, devi avere 8 campi per tutte le altre....

    SELECT
    SUM(Quantita_A) as Q_A,
    0 AS Q_B from TB_A
    where Prefisso_A IN ('1','2','3','4','5') and
    Q_A = '204' and F_A = '1' and S_ = 'a'

    UNION

    SELECT
    SUM(Quantita_B) as Q_A,
    SUM(Quantita_C) as Q_B
    from TB_A
    where Q_A = '204' and F_A = '1' and S_ = 'a'

    UNION

    SELECT
    SUM(Quantita_D) as Q_A,
    0 AS Q_B from TB_A
    where Prefisso_D IN ('6','7') and
    Q_A = '204' and F_A = '1' and S_ = 'a'

  10. #10
    ho capito, quindi la soluzione, è di adottare tante select quante Sum devo ottenere...
    OK, ti ringrazio..per il momento è tutto!!
    Alla prox

Permessi di invio

  • Non puoi inserire discussioni
  • Non puoi inserire repliche
  • Non puoi inserire allegati
  • Non puoi modificare i tuoi messaggi
  •  
Powered by vBulletin® Version 4.2.1
Copyright © 2025 vBulletin Solutions, Inc. All rights reserved.