I have created two CTE tables in my query which look at my data at different levels during 1 day. There is bunch of data added and everything runs smoothly. If I add SELECT * FROM level1_cte
to the end it will produce correct output.
Q1: Why can't I query all of my cte tables at once? They don't depend on each others.
WITH level1
AS
(
SELECT [order] FROM test
),
level2
([route], tag)
AS (
SELECT
[route],
tag
FROM test
)
SELECT * FROM level1
SELECT * FROM level2
This will produce error and I need to comment out one of the tables to be able to look at only one.
Q2: I would like to write the data from these 2 tables to a three permanent tables (level1, level2). Is that possible or do I need to duplicate and create two different procedures from this query?
DMBS is SQL Server
DATA:
create table test (
"date" date,
"order" varchar(10),
route varchar(10),
tag varchar(10),
test varchar(10))
insert into test values
('2022-01-01','A','AB','NA','AB'),
('2022-01-01','A','AB','NA','AB'),
('2022-01-01','A','AB','NA','AB'),
('2022-01-01','B','BB','NA','AB'),
('2022-01-01','B','BB','NA','AB'),
('2022-01-01','B','BB','this','AB')