How to use Not Equals in Postgresql Joins - postgresql

I'm doing this:
select * from schema2."Student" a INNER JOIN
schema1."StudentMapping" b on ( a."StudentID" = b."StudentID")
where a."IsRemoved" = false AND b."IsRemoved" = false
to get only those records from Student table that are present in StudentMapping table, here IsRemoved column I'm using for soft deletion(i.e, whenever any record is to be deleted from any of those tables then only I'm setting it to true, so for IsRemoved = false records are present in the tables) and the query is working fine. Now what I wanted is to get all those records from Student table that are not present in StudentMapping table so I tried this:
select * from schema2."Student" a INNER JOIN
schema1."StudentMapping" b on ( a."StudentId" != b."StudentId")
where a."IsRemoved" = false AND
b."IsRemoved" = false
but this is giving lot of records, more than I expected, what is wrong with this query or is there another way in Postgresql to get all matching recording from one table that are not present in another table.

Do a LEFT OUTER JOIN and then in the where clause, specify to only show recods where the join resulted in a NULL:
select * from schema1."Student" a LEFT OUTER JOIN
schema1."StudentMapping" b on ( a."StudentID" = b."StudentID")
where a."IsRemoved" = false
and b.StudentID IS NULL

Related

how can i combine tables on condition base in postgres functions

SELECT status
from orders (CASE WHEN true THEN 'INNER JOIN runningmenus ON orders.runningmenu_id = runningmenus.id' ELSE '' END);
I am getting error near 'CASE', How can i combine table base on condition?
You want to use a join only if a condition is true.
The trick is to always do the join but to use the parameterized condition in the join condition. Fields from both tables will always be included in the output (which is a good thing, the output is clearly defined) but the data may be set or null.
SELECT *
from orders
INNER JOIN runningmenus
ON my_parameter_condition = true AND orders.runningmenu_id = runningmenus.id
Edit : Following #404 comment, the behavior is a bit more complex. You would have to make a left join and keep rows when the parameter condition is false (== no join) or when the joined table has a matching rown (== an inner join):
SELECT *
from orders
LEFT JOIN runningmenus
ON my_parameter_condition = true AND orders.runningmenu_id = runningmenus.id
WHERE (my_parameter_condition = false OR runningmenus.id IS NOT NULL)

How does this query populate data?

It is my understanding that when this query runs it would not populate any data any number of times it runs because of the where clause
where c.company_id = lot.company_id
and p.product_id = lot.product_id
and l.packlevel_id = lot.packlevel_id
It looks to me that at the very beginning when the table fact_table_lot is empty the where clause would return with empty data because it would not find anything in an empty table and it would happen everytime. Is my understanding wrong?
insert into fact_table_lot(company_id, product_id, packlevel_id, l_num, sn_count, comm_loct, comm_start, commdate_end, man_date, exp_date, user_id, created_datetime)
select c.company_id, p.product_id, l.packlevel_id, l_num, sn_count, comm_loct, comm_start, commdate_end, man_date, exp_date, user_id, sysdate
from staging_serials s
left outer join fact_table_lot lot on s.lotnumber = lot.l_num
join company c on c.lsc_company_id = s.companyid
join product p on s.compositeprodcode = p.compositeprodcode
join level l on l.unit_of_measure = p.packaginguom
where c.company_id = lot.company_id
and p.product_id = lot.product_id
and l.packlevel_id = lot.packlevel_id
and lot.created_datetime is null
In your query staging_serials s left outer join fact_table_lot lot on s.lotnumber= lot.l_num this will give the result set containing all records from staging_serials and since fact table is empty null values for those column from fact table. If you want no records to be returned use a inner join instead of left join.

UPDATE in postgresql with JOINS

I have a form where some input has a value that is made with this query:
SELECT e.tree.nombre
FROM d.p
JOIN e.theme ON id = id_capa
LEFT JOIN e.tree ON e.theme.id_tree = e.tree.id
WHERE id_capa = 816
e and d are schemas. The id_capa = 816 is passed as an argument to the query from the form that I'm editing. It returns a value correctly. Now I want to edit that value on my form, so I need to UPDATE but I have multiple tables, I read that I can't do an UPDATE with JOINS, how should I do that UPDATE?
In your original SQL query, the table d.p is not used, neither in SELECT nor in conditions. So it can be skipped; the query can be rewritten as:
SELECT e.tree.nombre
FROM e.theme
LEFT JOIN e.tree ON e.theme.id_tree = e.tree.id
WHERE e.theme.id = 816
Since the query selects values from table joined with LEFT JOIN, the result can be NULL, ie joined record from e.tree table can be missed. In this case there is nothing to update.
Existing matching record can be updated with the query:
UPDATE e.tree
SET nombre = <NEW_VALUE>
FROM e.theme
WHERE e.theme.id = 816 AND e.theme.id_tree = e.tree.id

T-SQL: Joining on two separate fields based on specific criteria in a query

I have a query in which I am trying to get additional fields from another table through a join field that I manually create. The issue is when the field I create is null, then I want to use another field to join on. I am not sure how to do that without getting duplicate results. I tried a UNION query, but that just displays everything where the values are null when the manually created field value is null. Here is the query:
SELECT
BU = m.BU,
BUFBA = m.BUFBA,
a.CostCenter,
Delegate = m.Delegate,
a.DistrictLookup,
PCOwner = m.PCOwner,
a.PGr,
a.POrg,
PrimaryContact = m.PrimaryContact,
WarehouseManager = m.WarehouseManager,
Zone = m.Zone,
ZoneFBA = m.ZoneFBA
FROM
(SELECT
e.CostCenter,
e.District,
DistrictLookup =
CASE
WHEN e.PGr IN ('N01','BQE','BQA') THEN 'GSS'
WHEN e.PGr = 'BQB' THEN 'BG'
WHEN e.PGr = 'BQF' THEN 'FP'
ELSE e.District
END,
e.PGr,
e.POrg
FROM dbo.E1P e (NOLOCK)
WHERE
e.CoCd = '4433'
) a
LEFT JOIN dbo.Mapping m (NOLOCK) ON m.District = a.DistrictLookup
When the DistrictLookup field is NULL, I need a different join to occur so that the additional fields populate. That join would be:
LEFT JOIN dbo.Mapping m (NOLOCK) ON m.CostCenter = a.CostCenter
How can I write in this second join and not get duplicate results? This is a separate join on different fields and I think it differs from the other methods of doing a conditional join. If it, can someone please explain how to implement that logic into my query?
I believe this is what you are after...
LEFT JOIN dbo.Mapping m (NOLOCK)
ON (a.DistrictLookup IS NOT NULL AND m.District = a.DistrictLookup)
OR (a.DistrictLookup IS NULL AND m.CostCenter = a.CostCenter)

how to select from one table where no matching row in another (but has multiple)

I have tried this:
SELECT *
FROM svc00100
WHERE NOT EXISTS (SELECT *
FROM svc00101
WHERE TECHSTAT = 'INA'
AND svc00100.TECHID = svc00101.TECHID)
I want to select rows from svc00100 but not if there is a row in svc00101 with the same TECHID and with TECHSTAT = 'INA'. But, there are multiple rows in svc00101 with the TECHID matching, some having 'INA' and some having other stuff. I want to eliminate/ignore any TECHID where there is any row in svc00101 with TECHID and 'INA' for techstat. Using SQL server BTW if that helps.
You can use left outer join and Where clause. Like this:
select svc00100.* from svc00100
left outer join svc00101 on TECHSTAT = "INA"
and svc00100.TECHID = svc00101.TECHID
where svc00101.KEY is null
Instead of KEY you should pass name of NOT NULL column. For example Primary Key.