Please start any new threads on our new
site at https://forums.sqlteam.com. We've got lots of great SQL Server
experts to answer whatever question you can come up with.
| Author |
Topic |
|
luizcruz
Starting Member
1 Post |
Posted - 2007-11-03 : 21:14:01
|
| I have these queriesSELECT sum(cantidad) AS suma FROM Movimiento WHERE ID=[puntp] AND id_sorteo=[sorteo] AND tipo="Entrada";SELECT sum(cantidad) AS resta FROM Movimiento WHERE ID=[puntp] AND id_sorteo=[sorteo] AND tipo="Salida";I want to join in one simple query and i want to display the result of suma-restaThnk you ! i hope u can help me, I'm going to explode my brain hehe |
|
|
tkizer
Almighty SQL Goddess
38200 Posts |
Posted - 2007-11-03 : 21:28:48
|
| Don't join them, use UNION instead.SELECT sum(cantidad) AS suma FROM Movimiento WHERE ID=[puntp] AND id_sorteo=[sorteo] AND tipo="Entrada"UNIONSELECT sum(cantidad) AS resta FROM Movimiento WHERE ID=[puntp] AND id_sorteo=[sorteo] AND tipo="Salida";Tara KizerMicrosoft MVP for Windows Server System - SQL Serverhttp://weblogs.sqlteam.com/tarad/ |
 |
|
|
arorarahul.0688
Posting Yak Master
125 Posts |
Posted - 2007-11-06 : 07:24:25
|
| doing union is seeming to be easiest wayi will let u know certainly if will get another solRahul Arora MCA 07 BatchNCCE Israna, PanipatHRY, INDIA######################IMPOSSIBLE = I+M+POSSIBLE |
 |
|
|
madhivanan
Premature Yak Congratulator
22864 Posts |
Posted - 2007-11-06 : 07:49:42
|
quote: Originally posted by luizcruz I have these queriesSELECT sum(cantidad) AS suma FROM Movimiento WHERE ID=[puntp] AND id_sorteo=[sorteo] AND tipo="Entrada";SELECT sum(cantidad) AS resta FROM Movimiento WHERE ID=[puntp] AND id_sorteo=[sorteo] AND tipo="Salida";I want to join in one simple query and i want to display the result of suma-restaThnk you ! i hope u can help me, I'm going to explode my brain hehe
Is this?Select suma-resta as total from(SELECT sum(cantidad) AS suma FROM Movimiento WHERE ID=[puntp] AND id_sorteo=[sorteo] AND tipo='Entrada' union allSELECT sum(cantidad) AS resta FROM Movimiento WHERE ID=[puntp] AND id_sorteo=[sorteo] AND tipo='Salida') as tMadhivananFailing to plan is Planning to fail |
 |
|
|
SwePeso
Patron Saint of Lost Yaks
30421 Posts |
Posted - 2007-11-06 : 08:01:29
|
[code]SELECT Suma, Resta, Suma - RestaFROM ( SELECT SUM(CASE WHEN Tipo = 'Entrada' THEN Cantidad ELSE 0 END) AS Suma, SUM(CASE WHEN Tipo = 'Salida' THEN Cantidad ELSE 0 END) AS Resta FROM Movimiento WHERE ID = [puntp] AND id_sorteo = [sorteo] ) AS d[/code] E 12°55'05.25"N 56°04'39.16" |
 |
|
|
|
|
|