Double join postgres - postgresql

I need to get the username value from the table "commons.user", using the field "peticion.id_usuario_gerente"
I do not know how to do a join or what is necessary for that field to bring me the value I really need "gerente"
peticion.id_usuario_gerente <-- Is an id that points to my table "commons.usuario"
SELECT empleado.id,
empleado.fecha_contratado,
empleado.fecha_fin_contrato,
empleado.persona_comun,
persona.nombre,
persona.apellido1,
persona.apellido2,
empleado.responsable,
peticion.id_peticion
peticion.id_usuario_gerente <-- need username
FROM rrhh.empleado as empleado
LEFT JOIN commons.persona as persona on empleado.persona_comun = persona.id
LEFT JOIN seleccion.peticion as peticion on empleado.peticion_contratacion = peticion.id_peticion;

You just need to add one more [left] join to your query.
SELECT empleado.id,
empleado.fecha_contratado,
empleado.fecha_fin_contrato,
empleado.persona_comun,
persona.nombre,
persona.apellido1,
persona.apellido2,
empleado.responsable,
peticion.id_peticion
peticion.id_usuario_gerente,
usuario_gerente.nombre -- the column name for the user name
FROM rrhh.empleado as empleado
LEFT JOIN commons.persona as persona
on empleado.persona_comun = persona.id
LEFT JOIN seleccion.peticion as peticion
on empleado.peticion_contratacion = peticion.id_peticion
LEFT JOIN commons.usuario as usuario_gerente -- additional join for the table
on peticion.id_usuario_gerente = usuario_gerente.id_usuario;

Related

If transaction_id is not null join on transaction_id else join on user id - in same join?

I would like to do a single left join from table a onto table b on transaction_id
select a.*, b.*
from tablea a
left join tableb b on b.transaction_id = a.transaction_id
However, there are cases where transaction id on either table is missing, in which case I would like to fall back onto joining on a.user_id = b.user_id. If user_id is also missing then fine, I still want to keep all records from a.
Is there a way I can tell postgres to try joining on one field and if it's missing on either table to then try joining on another field?
Is there a way to do this?
Add a 2nd join to tableb with the condition that the 1st join did not match:
select a.*,
coalesce(b1.col1, b2.col1), coalesce(b1.col2, b2.col2), .....
from tablea a
left join tableb b1 on b1.transaction_id = a.transaction_id
left join tableb b2 on b2.user_id = a.user_id and b1.transaction_id is null

LEFT OUTER JOIN IN ENTITY FRAMEWORK

I am trying left outer join entity framework by joining 4 tables:
var ssss = (from supplier in entity.Supplier_master
join city in entity.City_master on supplier.Supplier_City equals city.id
join state in entity.State_master on supplier.Supplier_State equals state.id
join country in entity.Country_master on supplier.Supplier_Country equals country.id
where supplier.Supplier_Code.Equals(sup_code)
select (new { supplier.Supplier_Code, supplier.Supplier_Name, city.City_Name, state.State_Name, country.Country_Name, supplier.Supplier_TradeMark })).ToList();
The above code is executed as inner join, please help me to find the solution to done left outer join.
Join the 2 table usering "into alias" then select from that result.
Example:
var query = from supplier in entity.Supplier_master
join city in entity.City_master on supplier.Supplier_City equals city.id into citySupplier
from cs in citySupplier.DefaultIfEmpty()
select new { your fields) };

Why does not adding distinct in this query produce duplicate rows?

This query was taken from a Rails application log...I'm trying to edit a massive postgresql statement I didn't write....If I don't add a distinct keyword after the SELECT, 2 duplicate rows appear for each braintree account. Why is this and is there another way to avoid having to use the distinct to avoid duplicates?
EDIT: I understand what distinct is supposed to do, the reason I'm asking is that it doesn't generate duplicates for other toy lines. By other toy lines, this query is building a "table" for a particular toy id (this specific example toys.id = 12). How do I figure out where the duplicate rows are being generated?
SELECT accounts.braintree_account_id as braintree_account_id,
accounts.braintree_account_id as braintree_account_id, format('%s %s', addresses.first_name,
addresses.last_name) as shipping_address_full_name,
users.email as email, addresses.line_1 as shipping_address_line_1,
addresses.line_2 as shipping_address_line_2, addresses.city as
shipping_address_city, addresses.state as shipping_address_state,
addresses.zip as shipping_address_zip_code, addresses.country
as shipping_address_country, CASE WHEN xy_shirt IS NULL THEN '' ELSE xy_shirt END, plans.name as plan_name, toys.sku as sku, to_char(accounts.created_at, 'MM/DD/YYYY HH24:MM:SS') as
account_created_at,
to_char(accounts.next_assessment_at, 'MM/DD/YYYY HH24:MM:SS') as account_next_assessment_at,
accounts.account_status as account_status FROM \"accounts\" INNER JOIN \"addresses\" ON
\"addresses\".\"id\" = \"accounts\".\"shipping_address_id\" AND \"addresses\".\"type\" IN
('ShippingAddress') LEFT OUTER JOIN shipping_methods ON
shipping_methods.account_id = accounts.id LEFT OUTER JOIN plans ON
accounts.plan_id = plans.id
LEFT OUTER JOIN users ON
accounts.user_id = users.id LEFT OUTER JOIN toys ON plans.toy_id = toys.id
LEFT OUTER JOIN account_variations ON accounts.id =
account_variations.account_id LEFT OUTER JOIN variations ON
account_variations.variation_id = variations.id
LEFT OUTER JOIN
choice_value_variations ON variations.id =
choice_value_variations.variation_id
LEFT OUTER JOIN choice_values ON
choice_value_variations.choice_value_id = choice_values.id LEFT OUTER
JOIN choice_types ON choice_values.choice_type_id = choice_types.id
LEFT
OUTER JOIN choice_type_toys ON choice_type_toys.toy_id = toys.id
AND choice_type_toys.choice_type_id = choice_types.id
LEFT OUTER JOIN
(SELECT * FROM crosstab('SELECT accounts.id, choice_types.id,
choice_values.presentation FROM accounts\n
LEFT JOIN account_variations ON
accounts.id=account_variations.account_id\n
LEFT JOIN variations ON account_variations.variation_id=variations.id\n
LEFT JOIN choice_value_variations ON
variations.id=choice_value_variations.variation_id\n
LEFT JOIN choice_values ON
choice_value_variations.choice_value_id=choice_values.id\n
LEFT JOIN choice_types ON choice_values.choice_type_id=choice_types.id
ORDER BY 1,2',\n 'select distinct choice_types.id
from choice_types JOIN choice_values ON choice_values.choice_type_id =
choice_types.id JOIN choice_value_variations ON
choice_value_variations.choice_value_id = choice_values.id JOIN
variations ON choice_value_variations.variation_id = variations.id JOIN choice_type_toys ON choice_type_toys.choice_type_id = choice_types.id JOIN toys ON toys.id = choice_type_toys.toy_id
where toys.id=12 ORDER
BY choice_types.id ASC')\n
AS (account_id int, xy_shirt
VARCHAR)) account_variation_view\n ON
accounts.id=account_variation_view.account_id WHERE
\"accounts\".\"account_status\" = 'active' AND
\"addresses\".\"flagged_invalid_at\" IS NULL AND \"toys\".\"id\" = 12
AND (NOT EXISTS (SELECT \"account_skipped_months\".* FROM
\"account_skipped_months\" WHERE
\"account_skipped_months\".\"month_year\" = 'JUL2016' AND
(account_skipped_months.account_id = accounts.id)))"
The purpose of using DISTINCT in a SELECT statement is to eliminate duplicate rows.

nesting joins right to left in postgres

I have a big query, but it all boils down to this:
SELECT * FROM user
LEFT JOIN tableA ON tableA.user_id = user.id
JOIN tableB ON tableB.a_id = tableA.id
Now, I get too few results. If the combination of user with (tableA x tableB) does not exist, I still want the user. So with syntax error, what I want is something like this:
SELECT * FROM user
LEFT JOIN (tableA ON tableA.user_id = user.id
JOIN tableB ON tableB.a_id = tableA.id)
is that possible, perhaps without RIGHT JOINS?
Of course, I don't want to change the second JOIN to LEFT JOIN, because that would give too many results.
You can try this:
SELECT * FROM user
LEFT JOIN (SELECT * FROM tableA
JOIN tableB ON tableB.a_id = tableA.id) t
ON t.user_id = user.id
You will need to select distinct columns in subquery here if any exists.

Check foreign keys

I want to extend my query for delete and update rule, but I just can not figure what column in what systable that is in.
My query so far:
select oct.name FKNeve,oft.name TAmit,ofc.name MAmit,ort.name TAmihez,orc.name MAmihez
from sysforeignkeys sfk
inner join sysobjects oct on sfk.constid = oct.id
inner join sysobjects oft on sfk.fkeyid = oft.id
inner join syscolumns ofc on sfk.fkey = ofc.colid and sfk.fkeyid = ofc.id
inner join sysobjects ort on sfk.rkeyid = ort.id
inner join syscolumns orc on sfk.rkey = orc.colid and sfk.rkeyid = orc.id
Oh and MSDE.
Use the OBJECTPROPERTY function (with 'CnstIsDeleteCascade'/'CnstIsUpdateCascade' as the second argument).