September 2, 2010 at 8:32 am
Hi, i was trying filtering a query with an alias, but it doesn't work
select a,b, (select count(*) as Tot from table2) as Tot
From table1
where (Tot > 0).
but the alias coulumn work if i using it in "order by".
can anybody help me to understand what's happens?
Tks in advance.
Cristian.-
September 2, 2010 at 9:01 am
Cristian Maidana-360609 (9/2/2010)
Hi, i was trying filtering a query with an alias, but it doesn't workselect a,b, (select count(*) as Tot from table2) as Tot
From table1
where (Tot > 0).
but the alias coulumn work if i using it in "order by".
can anybody help me to understand what's happens?
Brute force solution?
select * from
(
select a,b, (select count(*) as Tot from table2) as Tot
From table1
)
where (Tot > 0)
Having said that, are you aware the value of "Tot" will be always the same?
_____________________________________
Pablo (Paul) Berzukov
Author of Understanding Database Administration available at Amazon and other bookstores.
Disclaimer: Advice is provided to the best of my knowledge but no implicit or explicit warranties are provided. Since the advisor explicitly encourages testing any and all suggestions on a test non-production environment advisor should not held liable or responsible for any actions taken based on the given advice.September 2, 2010 at 9:33 am
Hi, thanks for your answer.
bellow the original Query, when i implemented your suggestion, i have the "Msg 102, Level 15, State 1, Line 12
Incorrect syntax near ')'." error message.
any idea what is the problem?
Tks in advance.
NOTE: TotProp retrieve differents values.
Query
SELECT * FROM
(
SELECT tEmpresa.idEmpresa,
(select COUNT(*) as [TotProp] FROM t_INMO_Poi_Inmobiliario
WHERE (idEmpresa = tEmpresa.idEmpresa) AND (activo = 1) group by
idempresa) as [TotProp], tEmpresa.RazonSocial, tEmpresa.NombreCalle, tEmpresa.NroCalle,
tEmpresa.DetalleDomicilio, tEmpresa.TelefonoCodCiudad,tEmpresa.Telefono, tEmpresa.Telefono2CodCiudad,
tEmpresa.Telefono2, tEmpresa.web, tEmpresa.email
FROM tEmpresa INNER JOIN
tUsuario ON tEmpresa.idUsuario = tUsuario.idUsuario
WHERE (tUsuario.habilitado = 1) AND (tEmpresa.idActividad = 16)
)
September 2, 2010 at 9:41 am
Cristian Maidana-360609 (9/2/2010)
Hi, i was trying filtering a query with an alias, but it doesn't workselect a,b, (select count(*) as Tot from table2) as Tot
From table1
where (Tot > 0).
but the alias coulumn work if i using it in "order by".
can anybody help me to understand what's happens?
Tks in advance.
Cristian.-
[Tot] is an output column. ORDER BY works on output columns - as you would expect.
The WHERE clause works on input columns to select the rows you want - again as you would expect.
Get the green balls out of the box then paint them red
vs
Paint the green balls red then get them out of the box.
For fast, accurate and documented assistance in answering your questions, please read this article.
Understanding and using APPLY, (I) and (II) Paul White
Hidden RBAR: Triangular Joins / The "Numbers" or "Tally" Table: What it is and how it replaces a loop Jeff Moden
September 2, 2010 at 9:46 am
Cristian Maidana-360609 (9/2/2010)
Hi, thanks for your answer.bellow the original Query, when i implemented your suggestion, i have the "Msg 102, Level 15, State 1, Line 12
Incorrect syntax near ')'." error message.
any idea what is the problem?
Tks in advance.
NOTE: TotProp retrieve differents values.
Query
SELECT * FROM
(
SELECT tEmpresa.idEmpresa,
(select COUNT(*) as [TotProp] FROM t_INMO_Poi_Inmobiliario
WHERE (idEmpresa = tEmpresa.idEmpresa) AND (activo = 1) group by
idempresa) as [TotProp], tEmpresa.RazonSocial, tEmpresa.NombreCalle, tEmpresa.NroCalle,
tEmpresa.DetalleDomicilio, tEmpresa.TelefonoCodCiudad,tEmpresa.Telefono, tEmpresa.Telefono2CodCiudad,
tEmpresa.Telefono2, tEmpresa.web, tEmpresa.email
FROM tEmpresa INNER JOIN
tUsuario ON tEmpresa.idUsuario = tUsuario.idUsuario
WHERE (tUsuario.habilitado = 1) AND (tEmpresa.idActividad = 16)
) d
You're missing the alias for the derived table (d)
For fast, accurate and documented assistance in answering your questions, please read this article.
Understanding and using APPLY, (I) and (II) Paul White
Hidden RBAR: Triangular Joins / The "Numbers" or "Tally" Table: What it is and how it replaces a loop Jeff Moden
September 2, 2010 at 10:04 am
EXCELENT! NOW IT'S WORKING!!!
Thanks to all!
Have a nice day!
Cristian..
Viewing 6 posts - 1 through 6 (of 6 total)
You must be logged in to reply to this topic. Login to reply