on mysql I see this error but can fix it. Anyone can help please?
select * from
(
(select a.* from sessions as a)
join
(
select b.customer_id, min(b.timestamp),
b.marketing_source as first_touch_source,
b.marketing_medium as first_touch_medium
from sessions as b
group by b.customer_id
) on a.customer_id = b=customer_id
) as T
3
Answers
I believe your query should read
Some DBMS require you to name all derived tables. Your query (I removed the unnessesary derived table T):
can be changed to:
To avoid confusion, you should choose another alias at the outer level, despite that the inner alias is not visible there.
Side note: The derived table d may or may not be valid SQL. It is not allowed in SQL92, but it is allowed in SQL99 if marketing_* is functionally dependent of customer_id.
You can further simplify it as:
I assume you meant to also join with
c.timestamp = d.ts
. If that is the case and you are on a recent version of MySQL (8+) you can use a window function instead of a self joinIf you are just trying to bring in the minimum date column you could try two approaches, second one will work if your version supports windows function
subqueries are always a mess so I would suggest cte’s if supported