I'm trying to make my first recursive query in SQL and get an unxpected syntax error with inner join clause. I don't understand what causes this error. The query is simple it is just to select all employee attached to their boss with simple rule: BOSS_POS_ID = POS_ID
Here is example of my data:
POS_ID;POS_NAME;BOSS_POS_ID
32520602;CEO;
32809988;Manager;32520602
35244656;Vice;32520602
35244652;CEO assistant;32520602
35042934;Manager;32520602
35255704;Manager;32520602
35342468;Director;32520602
34091164;Director;32520602
35236439;Excecutive;32520602
32809978;Director;32520602
Here is my query:
with recursive subordinates as
(
select POS_ID, POS_NAME, BOSS_POS_ID
from gdm.hr_oss
where
POS_ID = 32520602
and CAL_DAY = (select max(CAL_DAY) from gdm.hr_oss)
union select
e.POS_ID,
e.POS_NAME,
e.BOSS_POS_ID
from gdm.hr_oss e
where CAL_DAY = (select max(CAL_DAY) from gdm.hr_oss)
inner join subordinates s on s.POS_ID = e.BOSS_POS_ID
)
select * from subordinates;
Here is error:
ERROR. Execution failed on sql '
with recursive subordinates as
(
select POS_ID, POS_NAME, BOSS_POS_ID
from gdm.hr_oss
where
POS_ID = 32520602
and CAL_DAY = (select max(CAL_DAY) from gdm.hr_oss)
union select
e.POS_ID,
e.POS_NAME,
e.BOSS_POS_ID
from gdm.hr_oss e
where CAL_DAY = (select max(CAL_DAY) from gdm.hr_oss)
inner join subordinates s on s.POS_ID = e.BOSS_POS_ID
)
select * from subordinates;
': syntax error at or near "inner"
LINE 15: inner join subordinates s on s.POS_ID = e.BOSS_POS_ID
I tried to merge a quarterly table and a monthly table by using INNER JOIN LATERAL, but encountered error.
state_smm table, monthly
unemp table, quarterly
my code is:
SELECT st.state,
st.monthly_reporting_period AS month,
st.CPR,
up.unemp_rate AS Most_Recent_Quarterly_unemp
FROM state_smm AS st
INNER JOIN LATERAL (
-- Find the value for latest quarter that ended on or before the monthly date
SELECT unemp.unemp_rate
FROM unemp
WHERE unemp.state = st.property_state
AND unemp.quarter <= st.monthly_reporting_period
ORDER BY unemp.quarter DESC
limit 1
)AS up
and I got error:
ERROR: syntax error at end of input
LINE 14: )AS up
^
SQL state: 42601
Character: 434
How can I edit my code? Thanks a lot.
I have the following query to pull LOB field (HL_DELETED_RECORD which contains no more than 25 characters) content, and it works fine by itself:
SELECT REPLACE(REPLACE( CONVERT(VARCHAR(20), HL_DELETED_RECORD), '&', '&'), 'ý', ' ') AS FAC_CHANGES,
HL_REC_ID
FROM SEC_HIST WITH(NOLOCK)
When I include this query as part of a CTE there are no errors; the LOB text data does not display at all as:
WITH sec_audit AS
(
SELECT *...
),
hist_logs as
(
SELECT REPLACE(REPLACE( CONVERT(VARCHAR(20), HL_DELETED_RECORD), '&', '&'), 'ý', ' ') AS FAC_CHANGES,
HL_REC_ID
FROM SEC_HIST WITH(NOLOCK)
)
SELECT *
FROM sec_audit
INNER JOIN hist_logs WITH(NOLOCK) ON HL_REC_ID = HL_RECORD_ID
The LOB column is defined as: [HL_DELETED_RECORD] text NULL
And here's an HL_DELETED_RECORD data sample: 36371ý1025074ýLEC
Which is converted as follows: 36371 1025074 LEC
Anyone else experience this? Thanks!
I am running a query that Redshift is throwing an error on "SQL Error [500310][57014]:Amazon Invalid operation: Error converting text to date;"
However, when I remove the Joined tables, or remove the conversions from the WHERE clause, it no longer throws an error. Thus I suspect something else is going on and this error is misleading.
I have looked at the data and it none of the dates look strange or out of range.
Here is the query that throws an error:
SELECT m.member_id,
t.tracker_id,
DATE(ma.yyyymmdd) AS tracked_date,
DATE(ma.updated_date) AS updated_date
FROM import_genesis.member_activities ma
INNER JOIN master."member" m
ON ma.memberid = m.member_legacy_id
INNER JOIN master.action_activity aa
ON ma.activity_type = aa.activity_type
INNER JOIN master.tracker t
ON t.action_id = aa.action_id
WHERE DATE(ma.yyyymmdd) > DATE(DATEADD(DAY, -14, GETDATE()))
AND DATE(ma.yyyymmdd) <= GETDATE();
Here are two queries that do NOT throw an error:
SELECT DATE(ma.yyyymmdd) AS tracked_date,
DATE(ma.updated_date) AS updated_date
FROM import_genesis.member_activities ma
WHERE DATE(ma.yyyymmdd) > DATE(DATEADD(DAY, -14, GETDATE()))
AND DATE(ma.yyyymmdd) <= GETDATE();
SELECT m.member_id,
t.tracker_id,
DATE(ma.yyyymmdd) AS tracked_date,
DATE(ma.updated_date) AS updated_date
FROM import_genesis.member_activities ma
INNER JOIN master."member" m
ON ma.memberid = m.member_legacy_id
INNER JOIN master.action_activity aa
ON ma.activity_type = aa.activity_type
INNER JOIN master.tracker t
ON t.action_id = aa.action_id;
Any ideas as to what might be wrong or any potential workarounds?
I have two sqlserver 2005 tables: Task table and Travel table.
I am trying to write a query that selects all records from the Task table for a specific Empid, and if the Task is travel, also get the mileage value from another table.
I expect only one row to be selected, but all rows in the Task table are returned.
The query I am using is
Select Tasklog.TaskLogPkey, Tasklog.Empid , Tasklog.Task, TravelLog.Mileage from
Tasklog
left join TravelLog
on
TaskLog.EmpId = 12 and TaskLog.Task ='Travel' and
TaskLog.TaskLogPkey = TravelLog.TaskLogPkey
But it makes no difference if I don’t include
-- TaskLog.EmpId = 12 and TaskLog.Task ='Travel' and
What am I doing wrong?
Clearly the following doesn't work:
Select Tasklog.TaskLogPkey, Tasklog.Empid , Tasklog.Task, TravelLog.Mileage from
Tasklog where TaskLog.EmpId = 12 and TaskLog.Task ='Travel'
left join TravelLog
on
TaskLog.TaskLogPkey = TravelLog.TaskLogPkey
These are the tables I am using:
Create table TaskLog(
TaskLogPkey int not null,
Empid int,
Task varchar(30)
)
Insert Tasklog values(1,12,'Sales')
Insert Tasklog values(2,4,'Travel')
Insert Tasklog values(3,63,'Meeting')
Insert Tasklog values(4,12,'Travel')
Insert Tasklog values(5,12,'Email')
Insert Tasklog values(6,4,'Travel')
Insert Tasklog values(7,63,'Meeting')
Insert Tasklog values(8,12,'PhoneCall')
Create table TravelLog(
TaskLogPkey int not null,
Mileage int
)
Insert TravelLog values(2,45)
Insert TravelLog values(4,25)
Insert TravelLog values(6,18)
When you perform an Outer join ( a left join is a Left Outer Join), you get all the records in the inner side of that join, --- no matter what predicates (filters) you specify on the other outer side in the join conditions.
To filter the other side you need to add a Where clause.
Select t.TaskLogPkey, t.Empid , t.Task, v.Mileage
From Tasklog t
Left Join TravelLog v
On t.TaskLogPkey = v.TaskLogPkey
Where t.Task ='Travel'
And t.EmpId = 12
TaskLog.EmpId = 12 and TaskLog.Task ='Travel' is part of the selection criteria, NOT join criteria.
And "Charles Bretana" beats me by less than 1 minute.
You should try placing a WHERE clause with the additional criteria:
Select Tasklog.TaskLogPkey, Tasklog.Empid , Tasklog.Task, TravelLog.Mileage
from Tasklog
left join TravelLog
on TaskLog.TaskLogPkey = TravelLog.TaskLogPkey
where TaskLog.EmpId = 12
and TaskLog.Task ='Travel'
See SQL Fiddle with Demo