2009-09-15 8 views
0

J'ai une table dans laquelle j'ai besoin de faire quelques calculs simples sur un champ. J'ai tenté des instructions SELECTS, IF/THEN ou CASE imbriquées dans SQL sans résultat.

EXEMPLE TABLEAU:
wYear, yMonth, compte, utilisation, Serv_code
2008, 9, 12345, 1000, Banana
2008, 9, 12345, 5000, Banana
2008, 9, 12345, 4000 , Apple
2007, 5, 54321, 1500, Banana


Je dois Grouper par wYear, yMonth, compte et additionnez "Utilisation" Où Serv_code = 'Banana' ...

SELECT wYear, yMonth, Account, SUM (Utilisation) en tant que "Totaux"
DE myTable
OÙ Serv_code = 'Banana'
GROUP BY wYear, yMonth, Compte

mais où je frappe le mur est - Si "Serv_code" ne correspond Apple '(où Serv_code = 'Apple') , J'ai besoin de soustraire l'utilisation 'Apple' de la somme (utilisation).

EXEMPLE RÉSULTATS DE L'UTILISATION DU TABLEAU CI-DESSUS:
2008, 9, 12345, 2000
2007, 5, 54321, 1500

Addition ou soustraction conditionnelle SQL

Je suis un hack en matière de SQL et la réponse est clair comme le jour, mais il a été un de ces jours.

+0

répondre à des questions comme celui-ci est plus facile si vous incluez créer table et insérer les instructions pour les données de test. –

Répondre

1

Vous pouvez utiliser une déclaration de cas pour obtenir une valeur positive ou négative en fonction de la valeur dans Serv_code:

select wYear, yMonth, Account, sum(Usage * case Serv_code when 'Apple' then -1 else 1 end) as "Totals" 
from myTable 
where Serv_code in ('Banana', 'Apple') 
group by wYear, yMonth, Account 
+0

Très bonne idée. –

1

J'espère que c'est ce que vous voulez:

SELECT 
    banana.wYear, 
    banana.yMonth, 
    banana.Account, 
    totals_banana - totals_apple AS totals 
FROM 
(
    SELECT wYear, yMonth, Account, SUM(Usage) as "totals_banana" 
    FROM myTable 
    WHERE Serv_code = 'Banana' 
    GROUP BY wYear, yMonth, Account 
) banana 
INNER JOIN 
(
    SELECT wYear, yMonth, Account, SUM(Usage) as "totals_apple" 
    FROM myTable 
    WHERE Serv_code = 'Apple' 
    GROUP BY wYear, yMonth, Account 
) apple 
ON 
    banana.wYear = apple.wYear 
    AND banana.yMonth = apple.yMonth 
    AND banana.Account = apple.Account 
1
SELECT wYear, yMonth, Account, SUM(case when Serv_code = 'Banana' then 
Usage when Serv_Code = 'Apple' then -Usage else 0 end) as "Totals" 
FROM myTable 

GROUP BY wYear, yMonth, Account 

test Version imprimable

Declare @myTable table (wYear int, ymonth int, Account varchar(20), 
Usage int, Serv_Code varchar(20)) 

Insert into @mytable values (2008, 9, '12345', 1000, 'Banana') 
Insert into @mytable values (2008, 9, '12345', 5000, 'Banana') 
Insert into @mytable values (2008, 9, '12345', 4000, 'Apple') 
Insert into @mytable values (2007, 5, '54321', 1500, 'Banana') 

SELECT wYear, yMonth, Account, SUM(case when Serv_code = 'Banana' then 
Usage when Serv_Code = 'Apple' then -Usage else 0 end) as "Totals" 
FROM @myTable 

GROUP BY wYear, yMonth, Account 


wYear yMonth Account Totals 
2007 5 54321 1500 
2008 9 12345 2000 
0

Je doute son efficacité, mais cela devrait faire l'affaire

SELECT 
    wYear, yMonth, Account, 
    (
     (SELECT SUM(Usage) FROM myTable AS m 
     WHERE m.wYear=wYear AND 
       m.yMonth=yMonth AND 
       m.Account=Account AND 
       m.Serv_code='Banana') - 
     (SELECT SUM(Usage) FROM myTable AS m 
     WHERE m.wYear=wYear AND 
       m.yMonth=yMonth AND 
       m.Account=Account AND 
       m.Serv_code='Apple') 
    ) as "Totals" 
FROM myTable 
GROUP BY wYear, yMonth, Account 
0

Peut-être que cela pourrait fonctionner -

SELECT wYear, yMonth, compte, SUM (cas lorsque l'utilisation = 'Apple' puis utilisation * -1 autre fin d'utilisation) comme « totaux » dE myTable OU PAR GROUPE wYear, yMonth, « Banana » = Serv_code Compte

Questions connexes