Below error is generated while creating a inner join on 2 tables in PostgreSQL with distinct clause.Here is my query :
select distinct public."firstapp_offer_Vendor".user_id from
(SELECT
public.firstapp_bid.id,
public.firstapp_bid."Bid",
public.firstapp_bid.offer_id,
public."firstapp_offer_Vendor".user_id,
public."firstapp_offer_Vendor".offer_id
FROM
public.firstapp_bid
inner JOIN public."firstapp_offer_Vendor"
ON public."firstapp_offer_Vendor".offer_id = public.firstapp_bid.offer_id) as foo;
but as i execute it,this error is generated.Please help.
ERROR: syntax error at or near "."
LINE 11: ...r".offer_id = public.firstapp_bid.offer_id) public."firstapp...
try this
select distinct user_id from
(SELECT
fb.id,
fb.Bid,
fb.offer_id,
fov.user_id,
fov.offer_id
FROM
public.firstapp_bid as fb
inner JOIN public.firstapp_offer_Vendor as fov
ON fov.offer_id = fb.offer_id) as foo;
Related
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 have written a query when I'm trying to run in pgAdmin it is showing this error ERROR: missing FROM-clause entry for table "sfv". Please anyone help me with this query.
Query
select distinct
u.user_id, u.login_name,u.first_name,COALESCE(u.last_name,' ') as last_name,
COALESCE(u.status,' ') as status, COALESCE(u.mobile,' ') as mobile,COALESCE(r.role_id,0) as role_id ,
COALESCE(r.role_name,'') as role_name,COALESCE(rm.entity_key,'') as entity_key,COALESCE(e.name,' ') as name
from sys_user as u
left join sys_user_role_map as rm
on u.user_id=rm.user_id
left join sys_role as r
on rm.role_id=r.role_id
left join sys_entity_user_map as eu
on eu.user_id=u.user_id
left join sys_org as e
on e.org_key =rm.entity_key group by u.user_id,r.role_id,r.role_name,rm.entity_key,e.name
having regexp_replace(upper (concat(first_name ,last_name)) , '[\s+]', '', 'g') like name and sfv.is_modifiable = '1' and sfv.field_code = 'Active' order by u.user_id desc
In a Knex query I'm getting that a column is undefined and I don't know how to resolve
the query I have in Knex
this.builder.orderBy(
this.tx(messageTable)
.max(messageColumns.createdAt)
.where(messageColumns.conversationId, 'conversation.id')
, direction)
The undefined happen on the last part of the .where(messageColumns.conversationId, 'conversation.id')
the SQL I want to get it to work with Knex is as follow
SELECT
*
FROM
"conversation"
ORDER BY
(
SELECT
max("created_at")
FROM
"message"
WHERE
"conversation_id" = conversation.id)
DESC
The query is wrong , try to do this:
select c.* from conversation c
join (
select conversation_id, max("created_at") created_at
from message
group by conversation_id
) m on c.conversation_id = m.conversation_id
order by m.created_at DESC
I'm trying to get lateral to work in a Postgres 9.5.3 query.
select b_ci."IdOwner",
ci."MinimumPlaces",
ci."MaximumPlaces",
(select count(*) from "LNK_Stu_CI" lnk
where lnk."FK_CourseInstanceId" = b_ci."Id") as "EnrolledStudents",
from "Course" c
join "DBObjectBases" b_c on c."Id" = b_c."Id"
join "DBObjectBases" b_ci on b_ci."IdOwner" = b_c."Id"
join "CourseInstance" ci on ci."Id" = b_ci."Id",
lateral (select ci."MaximumPlaces" - "EnrolledStudents") x
I want the right-most column to be the result of "MaximumPlaces" - "EnrolledStudents" for that row but am struggling to get it to work. At the moment PG is complaining that "EnrolledStudents" does not exist - which is exactly the point of "lateral", isn't it?
select b_ci."IdOwner",
ci."MinimumPlaces",
ci."MaximumPlaces",
(select count(*) from "LNK_Stu_CI" lnk
where lnk."FK_CourseInstanceId" = b_ci."Id") as "EnrolledStudents",
lateral (select "MaximumPlaces" - "EnrolledStudents") as "x"
from "Course" c
join "DBObjectBases" b_c on c."Id" = b_c."Id"
join "DBObjectBases" b_ci on b_ci."IdOwner" = b_c."Id"
join "CourseInstance" ci on ci."Id" = b_ci."Id"
If I try inlining the lateral clause (shown above) in the select it gets upset too and gives me a syntax error - so where does it go?
Thanks,
Adam.
You are missing the point with LATERAL. It can access columns in tables in the FROM clause, but not aliases defined in SELECT clause.
If you want to access alias defined in SELECT clause, you need to add another query level, either using a subquery in FROM clause (AKA derived table) or using a CTE (Common Table Expression). As CTE in PostgreSQL acts as an optimization fence, I strongly recommend going with subquery in this case, like:
select
-- get all columns on the inner query
t.*,
-- get your new expression based on the ones defined in the inner query
t."MaximumPlaces" - t."EnrolledStudents" AS new_alias
from (
select b_ci."IdOwner",
ci."MinimumPlaces",
ci."MaximumPlaces",
(select count(*) from "LNK_Stu_CI" lnk
where lnk."FK_CourseInstanceId" = b_ci."Id") as "EnrolledStudents",
from "Course" c
join "DBObjectBases" b_c on c."Id" = b_c."Id"
join "DBObjectBases" b_ci on b_ci."IdOwner" = b_c."Id"
join "CourseInstance" ci on ci."Id" = b_ci."Id"
) t
I've written a simple query that uses a WITH clause, but I'm getting this error:
Error : ERROR: missing FROM-clause entry for table "cte"
Here's the query, where I'm clearly putting a FROM clause. I know this must be simple but I'm just not seeing what I've done wrong. Thanks.
WITH cte AS (
SELECT cident, "month"
FROM orders_extended io
WHERE io.ident = 1 -- 1 will be replaced with a function parameter
)
SELECT *
FROM orders_extended o
WHERE o.cident = cte.cident AND o."month" = cte."month"
ORDER BY o."month" DESC, o.cname
The message didn't lie.
WITH cte AS (
SELECT cident, "month"
FROM orders_extended io
WHERE io.ident = 1 -- 1 will be replaced with a function parameter
)
SELECT o.*
FROM orders_extended o
INNER JOIN cte ON (o.cident = cte.cident and o."month" = cte."month")
ORDER BY o."month" DESC, o.cname