sorry if this is so obviously easy but I am new to this! I am trying to add another WITH statement to a query that works perfectly fine, but breaks when I edit it.
The query I am trying is:
--- select the term name ---
with term_name as (select dfs.k_form_submission,
dfs.k_form,
dfs.k_district as "district_id",
dff.form_name,
ffr.question_response as "term",
dfs.submitted_at
from
prod_wh_pls.dim_pls_fa_forms dff
inner join
prod_wh_pls.dim_pls_fa_submissions dfs
on
dfs.k_form = dff.k_form
inner join
prod_wh_pls.fct_pls_fa_responses ffr
on
dfs.k_form_submission = ffr.k_form_submission
where
(dff.k_form = 423614 or dff.k_form = 423615 or dff.k_form = 423608 or dff.k_form = 423613 or dff.k_form = 423670 or dff.k_form = 423706)
and
(ffr.question_name = 'term' or ffr.question_name = 'session_name')
and
dfs.k_form_submission != 457891),
fixed_district as (
select case when ffr.question_response = '2103480' then '16f7e40689605582e8fcfe7bc52481c4' else dfs.k_district end as "k_district", ffr.question_response as "nces_id", ffr.k_form_submission
from
prod_wh_pls.fct_pls_fa_responses ffr
inner join
prod_wh_pls.dim_pls_fa_submissions dfs
on
ffr.k_form_submission = dfs.k_form_submission
where
(ffr.question_name) = 'nces_id' and (ffr.k_form = 423706))
--- final query ----
select
dfs.k_form_submission, dfs.k_form,
dfs.k_district as "district_id",
dff.form_name,
ffr.question_name,
ffr.question_response,
dfs.submitted_at,
(case when dfs.submitted_at < date '07-15-2022' then '2021-2022'
else tna.term end) as "term",
dfs.is_latest_submission,
dfs.completion_time
from
prod_wh_pls.dim_pls_fa_forms dff
inner join
prod_wh_pls.dim_pls_fa_submissions dfs
on
dfs.k_form = dff.k_form
inner join
prod_wh_pls.fct_pls_fa_responses ffr
on
dfs.k_form_submission = ffr.k_form_submission
left join
term_name tna
on
tna.k_form_submission = dfs.k_form_submission
where
(dff.k_form = 423614 or dff.k_form = 423615 or dff.k_form = 423608 or dff.k_form = 423613 or dff.k_form = 423670 or dff.k_form = 423706)
and
dfs.k_form_submission != 457891
and
dfs.k_district is not null
and
(dfs.completion_time not ilike '%day%' and dfs.completion_time not ilike '%hr%')
The query that follows after "final query" isn’t right I know – but I can’t even get past the problem of adding another "with" to get to the next part! I keep getting "syntax error at end of input" on the last line before — final query—. How do I fix this?
I have tried rearranging the order of the WITH statements, that didn’t work. I tried editing the final query code section but am getting nothing. So frustrated
2
Answers
Query seems ok. Please try after adding semicolon(;) at the end of query. If you are still getting this error then check the table names and column names.
It would be great if you can provide any dbfiddle link.
I have also made some changes to the query. Please try this one:
Here is some reverse engineered tables/columns based on your query: https://dbfiddle.uk/Df99nCG7 the query runs without error BUT I had to change the hardcoded date literal from DD-MM-YYYY into YYYY_MM_DD:
If this does not resolve the issue check the DDL I have used, does it differ much from your real DDL? Perhaps replace my DDL with the original and run it again. If you still have error provide the URL to that fiddle.