I'd like to create named tables that are transformations of existing tables. I can then join them easily with each other later in my query. I know you can name tables using with [Table Name] as (...)
, but I then immediately need to select from this table, and I can't refer to it later. How do I make a callable table?
Here's a skeleton of what I'd like
With Table1 as (select...)
With Table2 as (select...)
select * from Table1 join Table2 using Table1.ID = Table2.ID
Here's what I have currently, which doesn't recognise Chart_Titles_Table
the second time I try to from
it.
with Control_Limit_Table as (
select CHRT_CONTROLLIMITS.CHRTID,CLSTARTDATE,LOWERCONTROLLIMIT,UPPERCONTROLLIMIT,CENTRALLINE from CHRT_CONTROLLIMITS
inner join (
select CHRTID, Max(CLSTARTDATE) as RecentCL from CHRT_CONTROLLIMITS group by CHRTID
) Newest on Newest.CHRTID = CHRT_CONTROLLIMITS.CHRTID and Newest.RecentCL = CLSTARTDATE
)
select * from Control_Limit_Table
;
with Chart_Titles_Table as (
select CHRTID, TITLE, ENGOWNER from CHRT
)
select * from Chart_Titles_Table join
Control_Limit_Table on Control_Limit_Table.chrtid = Chart_Titles_Table.CHRTID