I asked a question for a complex query yesterday, and I was offered an example. I really would like to get it to work but there was a syntax error in it that I can’t figure out. Keep in mind that I was just introduced to CTE’s earlier this week so hopefully this is an easy one.
I don’t think I need to post the full code on here, so I’ll just summarize
with cte as (select dateadd(hour, 1, cast(cast(getdate() -1 as date) as datetime)) as midnnight),
allhours as (
select 0 as hour, midnight as timestart, dateadd(hour, 1, timestart) as timeend from cte union all
select 1 as hour, dateadd(hour, 1, midnight), dateadd(hour, 2, midnight) from cte union all
....
select 23 as hour, dateadd(hour, 23, midnight), dateadd(hour, 24, midnight) from cte union all
)
select ah.hour,...
The (…) denotes unnecessary code that I omitted to make it less messy
But I am getting a syntax error on the parenthesis between select 23 and select ah.hour
“Incorrect syntax near ‘)’. Expecting SELECT, or ‘(‘.
Any help is greatly appreciated.
-J
You had a few syntax errors including a
UNION ALLthat was not needed at the bottom, and your firstSELECTin theallhourswas referencing an alias, so try this:see SQL Fiddle with Demo