I have a query
With Price as
(
select DISTINCT ID, A,B,C,D from AB_TABLE
WHERE TYPE = 'FULL'
)
I need to able to join this with another query
With New_Price as
(
select DISTINCT ID, A,B,C from AB_TABLE
WHERE TYPE = 'HALF'
)
How could I achieve this? I need this query for creating a view, so cant use SELECT with the WITH clause. My apologies if this is confusing. But the query inside Price and New_Price is actually complicated and I want to join both of them to show records from both of them, probably by applying a UNION. Can someone please help here.
WITH
handles multiple CTEs`:
With Price as (
select DISTINCT ID, A,B,C,D
from AB_TABLE
WHERE TYPE = 'FULL'
),
New_Price as (
select DISTINCT ID, A,B,C
from AB_TABLE
WHERE TYPE = 'HALF'
)
select . . .
from . . .
I think you are looking for .
;With Price as (
select DISTINCT ID, A,B,C,D
from AB_TABLE
WHERE TYPE = 'FULL'
),
New_Price as (
select DISTINCT ID, A,B,C
from AB_TABLE
WHERE TYPE = 'HALF'
)
select ID, A,B,C from Price
union ALL
select ID, A,B,C from New_Price
or
select DISTINCT ID, A,B,C,D
from AB_TABLE
WHERE TYPE = 'FULL' or TYPE = 'HALF'
SQL-Server VIEW
can use multiple CTE
.
You can follow this
CREATE VIEW
AS
WITH
CTE1 AS (SELECT * FROM T),
CTE2 AS (SELECT * FROM T),
CTE3 AS (SELECT * FROM T)
SELECT * CTE
Here is a simple
CREATE VIEW test
AS
WITH price
AS (SELECT DISTINCT id,
a,
b,
c,
d
FROM ab_table
WHERE type = 'FULL'),
new_price
AS (SELECT DISTINCT id,
a,
b,
c
FROM ab_table
WHERE type = 'HALF')
SELECT *
FROM (SELECT *
FROM price
UNION
SELECT *
FROM new_price) t
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.