gemmo
gemmo

Reputation: 1456

Sub Query and then use AS with join

I think someone wrote an overly complicated query. I don't quite understand what the point of 'AS' at the end. Essentially I can see two subqueries in this script. To cut it down I've just included one column.

SELECT [tableau_fact_group_committee_pl_agg].[dim_div_mgmt_key] AS [dim_div_mgmt_key]
FROM

(
SELECT fact_group_pl_agg.[dim_div_mgmt_key]

FROM fact_group_pl_agg 

join dbo.dim_DIV_MGMT_v d   on fact_group_pl_agg.dim_div_mgmt_key = d.dim_div_mgmt_key

WHERE EXIST

(SUB QUERY 2: SELECT FROM WHERE) 
)
AS 

[tableau_fact_group_committee_pl_agg]
INNER JOIN table 1 on col1 = col2
INNER JOIN table 2 on col3 = col4

Can anyone please tell me what "AS [tableau_fact_group_committee_pl_agg] INNER JOIN" does to the query? It's simple to follow the subqueries, as it is just making the data set smaller each time. But I don't follow anything beginning from "AS". Thank you.

Upvotes: 0

Views: 37

Answers (2)

grelak
grelak

Reputation: 1

in tsql there must be a unique name for each table expression (table, subquery, table-valued function, etc) in a query. the AS keyword allows you to assign a name (called an alias) to the table expression, fulfilling the naming requirement for subqueries and table-valued functions or giving a shorter or more meaningful name to a table. AS is optional and implied if you do something along the lines of select t.* from mytable t join (select top 1 id from dbo.mytable2) t2 on t2.id=t.id in this case both mytable and the subquery table expression are aliased without using AS

Upvotes: 0

shree.pat18
shree.pat18

Reputation: 21757

AS is used to introduce an alias i.e. another name, be it for a column or the results of a query. In this case, the result set of the query (SELECT fact_group_pl_agg.[dim_div_mgmt_key] ...) is given the alias [tableau_fact_group_committee_pl_agg].

After that, the INNER JOIN clauses simply join the result set of the first query to the specified tables with the specified conditions.

Upvotes: 1

Related Questions