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
 select from results returned by a select statement

Author  Topic 

mavericky
Posting Yak Master

117 Posts

Posted - 2011-08-12 : 11:09:48
Hi,
I get a set of results returned by this query:
SELECT DISTINCT P.Name, W.Name, A.LoadKW, AP.UtilizationFactor*A.LoadKW AS HEValues, AP.MaxAttributeValue AS HE FROM Program P
INNER JOIN WholesaleProduct W
ON P.WholesaleProductID = W.ID
INNER JOIN AssetType AT
ON AT.ID = P.AssetTypeID
INNER JOIN AssetProfile AP
ON AT.ID = AP.AssetTypeID
INNER JOIN Asset A
ON A.AssetTypeID = AT.ID ORDER BY P.Name

Now, from the returned results, I want to select HEValues. I tried putting this query in a bracket and then did a select on it, but i get an error saying "Incorrect syntax near ')'"

Thanks in anticipation,
Mavericky

jimf
Master Smack Fu Yak Hacker

2875 Posts

Posted - 2011-08-12 : 11:27:05
You need to alias it
SELECT t1.<stuff>
from
(
SELECT DISTINCT P.Name, W.Name, A.LoadKW, AP.UtilizationFactor*A.LoadKW AS HEValues, AP.MaxAttributeValue AS HE FROM Program P
INNER JOIN WholesaleProduct W
ON P.WholesaleProductID = W.ID
INNER JOIN AssetType AT
ON AT.ID = P.AssetTypeID
INNER JOIN AssetProfile AP
ON AT.ID = AP.AssetTypeID
INNER JOIN Asset A
ON A.AssetTypeID = AT.ID
-- Notice the disappearance of the order by
) T1


ORDER BY t1.Name

Jim

Everyday I learn something that somebody else already knew
Go to Top of Page
   

- Advertisement -