Wednesday, March 7, 2012

Query on SQL Server 2005

Hello.
I've a problem Whit a SQL Server 2005 query.
I've two tables Carico and Scarico that are:
- Id
- IdProdotto
- Data
- Quantita.
I want to know the giacency. Then my query is:
SELECT Sum(Tot)
FROM (SELECT Sum(Carico.Quantita) as Tot FROM Carico WHERE (IdProdotto = 2)
UNION
SELECT (Sum(Scarico.Quantita)*(-1)) as Tot From Scarico WHERE (IdProdotto =
2));
But for SQL Server there is a problem:
Msg 102, Level 15, State 1, Line 4
Incorrect syntax near ')'.
Help me please!!
Bye
IvanCristian wrote:
> Hello.
> I've a problem Whit a SQL Server 2005 query.
> I've two tables Carico and Scarico that are:
> - Id
> - IdProdotto
> - Data
> - Quantita.
> I want to know the giacency. Then my query is:
> SELECT Sum(Tot)
> FROM (SELECT Sum(Carico.Quantita) as Tot FROM Carico WHERE (IdProdotto = 2
)
> UNION
> SELECT (Sum(Scarico.Quantita)*(-1)) as Tot From Scarico WHERE (IdProdotto
=
> 2));
> But for SQL Server there is a problem:
> Msg 102, Level 15, State 1, Line 4
> Incorrect syntax near ')'.
> Help me please!!
> Bye
> Ivan
An alias is required for the derived table. ("AS T"):
SELECT SUM(Tot)
FROM (
SELECT SUM(Carico.Quantita) AS Tot
FROM Carico
WHERE (IdProdotto = 2)
UNION
SELECT (SUM(Scarico.Quantita)*(-1)) AS Tot
FROM Scarico
WHERE (IdProdotto = 2)) AS T ;
David Portas
SQL Server MVP
--|||OK!
Thank's for your help.
Is the good solution!!!
Bye
"David Portas" <REMOVE_BEFORE_REPLYING_dportas@.acm.org> ha scritto nel
messaggio news:1133431092.145093.62670@.g43g2000cwa.googlegroups.com...
> Cristian wrote:
> An alias is required for the derived table. ("AS T"):
> SELECT SUM(Tot)
> FROM (
> SELECT SUM(Carico.Quantita) AS Tot
> FROM Carico
> WHERE (IdProdotto = 2)
> UNION
> SELECT (SUM(Scarico.Quantita)*(-1)) AS Tot
> FROM Scarico
> WHERE (IdProdotto = 2)) AS T ;
> --
> David Portas
> SQL Server MVP
> --
>

No comments:

Post a Comment