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.

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 Help with query guys..

Author  Topic 

luizcruz
Starting Member

1 Post

Posted - 2007-11-03 : 21:14:01
I have these queries

SELECT 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-resta

Thnk 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"
UNION
SELECT sum(cantidad) AS resta FROM Movimiento WHERE ID=[puntp] AND id_sorteo=[sorteo] AND tipo="Salida";

Tara Kizer
Microsoft MVP for Windows Server System - SQL Server
http://weblogs.sqlteam.com/tarad/
Go to Top of Page

arorarahul.0688
Posting Yak Master

125 Posts

Posted - 2007-11-06 : 07:24:25
doing union is seeming to be easiest way

i will let u know certainly if will get another sol

Rahul Arora
MCA 07 Batch
NCCE Israna, Panipat
HRY, INDIA

######################
IMPOSSIBLE = I+M+POSSIBLE
Go to Top of Page

madhivanan
Premature Yak Congratulator

22864 Posts

Posted - 2007-11-06 : 07:49:42
quote:
Originally posted by luizcruz

I have these queries

SELECT 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-resta

Thnk 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 all
SELECT sum(cantidad) AS resta FROM Movimiento WHERE ID=[puntp] AND id_sorteo=[sorteo] AND tipo='Salida'
) as t

Madhivanan

Failing to plan is Planning to fail
Go to Top of Page

SwePeso
Patron Saint of Lost Yaks

30421 Posts

Posted - 2007-11-06 : 08:01:29
[code]SELECT Suma,
Resta,
Suma - Resta
FROM (
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"
Go to Top of Page
   

- Advertisement -