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
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
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;
We created a view in Postgres and I am getting strange result.
View Name: event_puchase_product_overview
When I try to get records with *, I get the correct result. but when I try to get specific fields, I get wrong values.
I hope the screens attached here can explain the problem well.
select *
from event_purchase_product_overview
where id = 15065;
select id, departure_id
from event_puchase_product_overview
where id = 15065;
VIEW definition:
CREATE OR REPLACE VIEW public.event_puchase_product_overview AS
SELECT row_number() OVER () AS id,
e.id AS departure_id,
e.type AS event_type,
e.name,
p.id AS product_id,
pc.name AS product_type,
product_date.attribute AS option,
p.upcomming_date AS supply_date,
pr.date_end AS bid_deadline,
CASE
WHEN (pt.categ_id IN ( SELECT unnest(tt.category_ids) AS unnest
FROM ( SELECT string_to_array(btrim(ir_config_parameter.value, '[]'::text), ', '::text)::integer[] AS category_ids
FROM ir_config_parameter
WHERE ir_config_parameter.key::text = 'trip_product_flight.product_category_hotel'::text) tt)) THEN e.maximum_rooms
WHEN (pt.categ_id IN ( SELECT unnest(tt.category_ids) AS unnest
FROM ( SELECT string_to_array(btrim(ir_config_parameter.value, '[]'::text), ', '::text)::integer[] AS category_ids
FROM ir_config_parameter
WHERE ir_config_parameter.key::text = 'trip_product_flight.product_category_flight'::text) tt)) THEN e.maximum_seats
WHEN (pt.categ_id IN ( SELECT unnest(tt.category_ids) AS unnest
FROM ( SELECT string_to_array(btrim(ir_config_parameter.value, '[]'::text), ', '::text)::integer[] AS category_ids
FROM ir_config_parameter
WHERE ir_config_parameter.key::text = 'trip_product_flight.product_category_bike'::text) tt)) THEN e.maximum_bikes
ELSE e.maximum_seats
END AS departure_qty,
CASE
WHEN now()::date > pr.date_end AND po.state::text = 'draft'::text THEN true
ELSE false
END AS is_deadline,
pl.product_qty::integer AS purchased_qty,
pl.comments,
pl.price_unit AS unit_price,
rp.id AS supplier,
po.id AS po_ref,
po.state AS po_state,
po.date_order AS po_date,
po.user_id AS operator,
pl.po_state_line AS line_status
FROM event_event e
LEFT JOIN product_product p ON p.related_departure = e.id
LEFT JOIN product_template pt ON pt.id = p.product_tmpl_id
LEFT JOIN product_category pc ON pc.id = pt.categ_id
LEFT JOIN purchase_order_line pl ON pl.product_id = p.id
LEFT JOIN purchase_order po ON po.id = pl.order_id
LEFT JOIN purchase_order_purchase_requisition_rel prr ON prr.purchase_order_id = po.id
LEFT JOIN purchase_requisition pr ON pr.id = prr.purchase_requisition_id
LEFT JOIN res_partner rp ON rp.id = po.partner_id
LEFT JOIN ( SELECT p_1.id AS product_id,
pav.name AS attribute
FROM product_product p_1
LEFT JOIN product_attribute_value_product_product_rel pa ON pa.prod_id = p_1.id
LEFT JOIN product_attribute_value pav ON pav.id = pa.att_id
LEFT JOIN product_attribute pat ON pat.id = pav.attribute_id
WHERE pat.name::text <> ALL (ARRAY['Date'::character varying, 'Departure'::character varying]::text[])) product_date ON product_date.product_id = p.id
WHERE (p.id IN ( SELECT DISTINCT mrp_bom_line.product_id
FROM mrp_bom_line)) AND p.active
ORDER BY e.id, pt.categ_id, p.id;
If I add new event_event or new product_product I'll get a new definition of row_number in my view, then the column ID of my view is not stable.
at least you can't use row_number as Id of the view,
If you insist to use row_number, you can use the Order By "creation DATE" by this way all new records will be as last lines in the view and this will not change the correspondency between ID (row_number) and other columns.
Hope that helps !
Very likely the execution plan of your query depends on the columns you select. Compare the execution plans!
Your id is generated using the row_number window function. Now window functions are executed before the ORDER BY clause, so the order will depend on the execution plan and hence on the columns you select.
Using row_number without an explicit ordering doesn't make any sense.
To fix that, don't use
row_number() OVER ()
but
row_number() OVER (ORDER BY e.id, pt.categ_id, p.id)
so that you have a reliable ordering.
In addition, you should omit the ORDER BY clause at the end.
I have a query
SELECT cm.posted_at, s.id, s.name, s.device, co.status, co.last_seen, d.device_id
FROM station s LEFT JOIN chat_online co
ON s.id = co.station_id AND s.device = co.device_id
LEFT JOIN device d ON s.device=d.id
LEFT JOIN chat_message cm ON cm.station_id=s.id
WHERE s.status='1' AND (co.role='station' OR co.role IS NULL)
GROUP BY s.id
ORDER BY cm.posted_at DESC
where in chat_message have multiple entry for one station while executing above query i am getting the first entry of chat_message corresponding to each station_id. How can i retrieve the last entry of chat_message while left joining the chat_message with station
Thank in Advance
As i understood , you have data like,
and you need to query like this,
select cm.posted_at, s.id, s.name, s.device from station s
JOIN (
SELECT MAX(posted_at) posted_at,stationId
FROM chat_message
GROUP BY stationId
) cm ON (cm.stationId = s.id)
I’m doing a join between two tables in PostgreSQL, one has a primary key constraint on incidentnumber (opentickets) while the other table does not have the restraint and can have duplicate incidentnumbers (incommingtickets). The problem comes when trying to filter out duplicates. The query,
SELECT incommingtickets.*
FROM incommingtickets
LEFT JOIN opentickets
ON incommingtickets.incidentnumber = opentickets.incidentnumber
WHERE opentickets.incidentnumber IS NULL
AND incommingtickets.status NOT IN ('Closed','Cancelled', '')
works until it hits a duplicate, the I get the violates primary key message. If I add a distinct clause like,
SELECT DISTINCT ON (incommingtickets.incidentnumber) incommingtickets.*
FROM incommingtickets
LEFT JOIN opentickets
ON incommingtickets.incidentnumber = opentickets.incidentnumber
WHERE opentickets.incidentnumber IS NULL
AND incommingtickets.status NOT IN ('Closed','Cancelled', '')
I get an error,
pg_query(): Query failed: ERROR: missing FROM-clause entry for table
"incommingtickets" LINE 30: WHERE opentickets.incidentnumber =
incommingtickets.incident...
Use a WHERE clause that filters out the duplicates you don't want, although it's not very clear for me on why you want to join on a 'metric' such as number of tickets.
SELECT incommingtickets.*
FROM incommingtickets
WHERE incommingtickets.incidentnumber not in (
select
distinct
incidentnumber
FROM opentickets)
AND incommingtickets.status NOT IN ('Closed','Cancelled', '')
This way you are filetring out duplicates between both tables.
If what you want is to check or update the ticket's status of any tickets inside the opentickets table then try to get from the incommingtickets the maximum status like this:
WITH ticket_rows AS(
SELECT
rank() OVER (PARTITION BY ticket_id ORDER BY ticket_timestamp desc) as row_number,
ticket_id,
ticket_status,
ticket_timestamp
from incommingtickets
)
SELECT incommingtickets.*, opentickets_2.*
FROM opentickets o
LEFT JOIN ticket_rows ON ticket_rows.ticket_id= opentickets.ticket_id AND ticket_rows.row__number=1
If these are not your objectives pleae explain a bit better on what you are trying to achieve with that left join.