-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path14. CTES.sql
More file actions
50 lines (44 loc) · 1.08 KB
/
Copy path14. CTES.sql
File metadata and controls
50 lines (44 loc) · 1.08 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
use ecom;
-- CTES (Common Table Expressions)
-- Alternative for subqueries. CTES is also a temporary table similar to subquery.
-- We cannot put CTES in the where clause. We can only treat it as a table.
with cte_table as -- Encapsulate the temporary table result into cte_table
(select
*
from
dim_product
where
unit_price > (select
avg(unit_price) -- Here, we are using subquery in where clause, we cannot use CTEs here.
from
dim_product))
select
*
from
cte_table
where
product_name in ('Figure Method','Film Finally', 'Huge Change');
-- Lets say we have to use this table for next query, ie, inherit this as cte table.
with cte_table as -- Encapsulate the temporary table result into cte_table
(select
*
from
dim_product
where
unit_price > (select
avg(unit_price)
from
dim_product)
),
cte_table_2 as -- We wirte with command only one time.
(
select
*
from
cte_table
where
product_name in ('Figure Method','Film Finally', 'Huge Change')
)
select *
from cte_table_2
where category = 'Clothing';