views:

40

answers:

4
+1  Q: 

sql using aliases

I seem to be having some trouble grasping the concept of using AS to create an alias, and then using that alias later. Here is an example of a very simple query that I get an error when I run:

SELECT IP,  
       (SELECT server_ip as IP  
          FROM table1  
         WHERE start_time BETWEEN @startdate AND @enddate  
      Group By server_ip)X  
Group By IP 

I would expect this to just return the results from the second select statement in the server_ip column. However I get an error messages saying IP is not an invalid column name. Can someone explain to me how to do this properly?

+1  A: 
SELECT IP,
    (
    SELECT server_ip as IP
    FROM table1
    WHERE start_time BETWEEN @startdate AND @enddate
    Group By server_ip
    )X
/*FROM WHERE ? <--------------- */
Group By IP 

Your SELECT statement at the moment is

SELECT IP, X
GROUP BY IP

You need a FROM clause. X is a non correlated sub query at the moment. I suspect that you wanted to treat it as a derived table but even then the query makes no sense. There is no need to perform the same GROUP BY action twice.

Dependant upon your RDBMS you might also be able to use Common Table Expressions. Here's a somewhat contrived example of their use.

;WITH X AS
(
    SELECT server_ip as IP
    FROM table1
    WHERE start_time BETWEEN @startdate AND @enddate
),
Y AS
(
    SELECT IP
    FROM X
    Group By IP
)
SELECT IP
FROM Y
Martin Smith
thanks ;) appreciate the help
MilqueToasted
@MilqueToasted - Are you on SQL Server 2005+? If so check out common table expressions as well.
Martin Smith
Oracle 9i+ (and PostgreSQL 8.4+) supports WITH, along with DB2 and Firebird (dunno versions).
OMG Ponies
@OMG - Any idea which of those materialise the results?
Martin Smith
[Oracle 10g, according to AskTom](http://asktom.oracle.com/pls/asktom/f?p=100:11:0::::P11_QUESTION_ID:649529000346677493).
OMG Ponies
+1  A: 

You were missing the FROM clause:

  SELECT x.ip
    FROM (SELECT server_ip as IP  
            FROM table1  
           WHERE start_time BETWEEN @startdate AND @enddate  
        GROUP BY server_ip) AS x 
GROUP BY x.ip 

As is, you only need to use:

  SELECT server_ip as IP  
    FROM table1  
   WHERE start_time BETWEEN @startdate AND @enddate  
GROUP BY server_ip
OMG Ponies
oh boy do I feel silly..missing the FROM. I realize the second select is unnecessary but I was having trouble with aliases and more complex queries so I decided to make it as simple as possible and build up. Thanks for the help all :)
MilqueToasted
SELECT DISTINCT avoids the need to GROUP BY. We have fixed his query...someone else will have to help him with aliases.
kevpie
A: 

you have to use "AS" keyword befor your new name for assigning a alias to a temp table or column or etc.

Masoud
Depends on the database--`AS` can be optional. IE: Oracle, SQL Server...
OMG Ponies
A: 
select t.field,t.field2
from anytablename t
where...

that is setting up t as an alias for anytablename. Queries can also be used in this manner

select t.field,t.field2
from (select field1,field2 from anymytable where...) t
where...

I think OMG Ponies has what you want to do for this query, no need to repeat him. Remember the select statement you are selecting from (the subquery) goes in the from clause (not the select) and behaves (mostly) like a table.

M.E.