Visualizzazione dei risultati da 1 a 4 su 4
  1. #1

    [SQL Server 2005]Raggruppamento query

    Ciao a tutti,

    io ho queste 2 query

    select CODE, sum(real_con_cost) AS TOT_MI from EXPORT_COSTI_UTENTE WHERE CODE_PREFIX LIKE 'H/CL/DI%' AND TIPO_OP='interno'
    GROUP BY CODE

    select CODE, sum(real_con_cost) AS TOT_ME from EXPORT_COSTI_UTENTE WHERE CODE_PREFIX LIKE 'H/CL/DI%' AND TIPO_OP='esterno'
    GROUP BY CODE

    che mi producono 2 tabelle come queste
    CODE---TOT_MI
    ABC-----10

    CODE---TOT_ME
    DEF-----15

    vorrei, se possibile, riuscire a raggruppare le 2 query per avere poi una tabella sola tipo

    CODE---TOT_MI---TOT_ME
    ABC-----10--------0
    DEF------0--------15

    ci sto provando..ma con scarso successo .....qualcuno sa darmi una mano??

    thanks!

  2. #2
    Utente di HTML.it L'avatar di gibra
    Registrato dal
    Apr 2008
    residenza
    Italy
    Messaggi
    4,244
    Una cosetta veloce ed estemporanea la puoi fare così:

    codice:
    select CODE, sum(real_con_cost) AS TOT_MI [B], 0 As TOT_ME [/B] 
    from EXPORT_COSTI_UTENTE 
    WHERE CODE_PREFIX LIKE 'H/CL/DI%' AND TIPO_OP='interno'
    GROUP BY CODE
    
    UNION
    
    select CODE[B], 0 As TOT_MI [/B] , sum(real_con_cost) AS TOT_ME 
    from EXPORT_COSTI_UTENTE 
    WHERE CODE_PREFIX LIKE 'H/CL/DI%' AND TIPO_OP='esterno'
    GROUP BY CODE
    P.S. Ricorda di formattare


  3. #3
    Utente di HTML.it L'avatar di nicola75ss
    Registrato dal
    Nov 2004
    Messaggi
    12,922
    codice:
    select code,
    sum(case when tipo_op = 'interno' then real_con_cost else 0 end) as tot_mi,
    sum(case when tipo_op = 'esterno' then real_con_cost else 0 end) as tot_me
    from export_costi_utente
    where .... 
    group by code

  4. #4
    thanks!

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.