Access "find unmatched query" return records which are available both tables when one of the fields is empty - sql

I am trying to list records from table N that do not have a match in table O
The result is shown in query N Without Matching O
As one can see on above image, it is returning records which exist in both tables, which it should not.
In above example table N and table O are identical (copy)
The query is as follows:
SELECT N.*
FROM N LEFT JOIN O ON (N.[F8] = O.[F8]) AND (N.[F7] = O.[F7]) AND (N.[F6] = O.[F6]) AND (N.[F5] = O.[F5]) AND (N.[F4] = O.[F4]) AND (N.[F3] = O.[F3]) AND (N.[F2] = O.[F2]) AND (N.[F1] = O.[F1])
WHERE (((O.F1) Is Null));
I guess it is because of empty values (e.g. field F5 in the example above)
Further down the table other fields may be empty as well
My question is:
How do I return unmatched records from a table which may contain empty cells

Try using Nz on that field (F5):
SELECT N.*
FROM N LEFT JOIN O ON (N.[F8] = O.[F8]) AND (N.[F7] = O.[F7]) AND (N.[F6] = O.[F6]) AND (Nz(N.[F5]) = Nz(O.[F5])) AND (N.[F4] = O.[F4]) AND (N.[F3] = O.[F3]) AND (N.[F2] = O.[F2]) AND (N.[F1] = O.[F1])
WHERE (((O.F1) Is Null));

Related

Find matching rows in database table using SQL where no matching key is present

I have an old table with legacy data and approx 10,000 rows and a new table with about 500 rows. The columns are the same in both tables. I need to compare a few columns in the new table with the old one and report on data that is duplicated in the new table.
I've researched articles with similar issues, attempted table joins and where exists / where not exists clauses but I just can't get the SQL right. I have included my latest version.
One issue causing trouble for me, I think, is that there is no "Key" as such like a userid or similar unique identifier in either table.
What I want to do is find the data in the "new" table where all rows except for the "reference_number" (doesn't matter if it does or does not) is duplicated, i.e. exists already in the "old" table.
I have this so far...
select
old.reference_number
new.reference_number
new.component
new.privileges
new.protocol
new.authority
new.score
new.means
new.difficulty
new.hierarchy
new.interaction
new.scope
new.conf
new.integrity
new.availability
new.version
from old, new
where
old.component = new.component
old.privileges = new.privileges
old.protocol = new.protocol
old.authority = new.authority
old.score = new.score
old.means = new.means
old.difficulty = new.difficulty
old.hierarchy = new.hierarchy
old.interaction = new.interaction
old.scope = new.scope
old.conf = new.conf
old.integrity = new.integrity
old.availability = new.availability
old.version = new.version
I have tried this here but it doesn't seem to pull out ALL of the data for some reason.
It is evident that actually there are MORE rows in the old table that are duplicated in the new table but I'm only getting a small number of rows returned from the query.
Can anyone spot why that might be, is there another way I should be approaching this?
If it matters, this is Postgresql.
Thanks for any help given.
The following should do what you want:
select distinct o.reference_number,
n.reference_number,
n.component,
n.privileges,
n.protocol,
n.authority,
n.score,
n.means,
n.difficulty,
n.hierarchy,
n.interaction,
n.scope,
n.conf,
n.integrity,
n.availability,
n.version
from new n
inner join old o
on o.component = n.component and
o.privileges = n.privileges and
o.protocol = n.protocol and
o.authority = n.authority and
o.score = n.score and
o.means = n.means and
o.difficulty = n.difficulty and
o.hierarchy = n.hierarchy and
o.interaction = n.interaction and
o.scope = n.scope and
o.conf = n.conf and
o.integrity = n.integrity and
o.availability = n.availability and
o.version = n.version
You should use left join and then select only rows with new values is null. sql should be something like this:
select
old.reference_number
new.reference_number
new.component
new.privileges
new.protocol
new.authority
new.score
new.means
new.difficulty
new.hierarchy
new.interaction
new.scope
new.conf
new.integrity
new.availability
new.version
from old
left join new
on
old.component = new.component
old.privileges = new.privileges
old.protocol = new.protocol
old.authority = new.authority
old.score = new.score
old.means = new.means
old.difficulty = new.difficulty
old.hierarchy = new.hierarchy
old.interaction = new.interaction
old.scope = new.scope
old.conf = new.conf
old.integrity = new.integrity
old.availability = new.availability
old.version = new.version
where new.component is null

Sql not returning columns metadata when the actual values are null

I have an issue in information link SQL query which joins multiple tables where some dynamic column names are returned. They are stored in label and value attribute of the below query. Please note we had certain fields in the database which when null doesn't appear as column in the output. For e.g a field test1 in the database has all values null then test1 doesn't get returned. However, even if a single value (row) of test1 has some data, I see test1 in the output. The requirement is always have this field in the output irrespective of whether it has the data for not. Please see the query below and point out what is causing issue.
SELECT
I1."item_uid" AS "ITEMUID",
I1."item_reference_uid" AS "ITEMREFERENCEUID",
w2."workflow_abbrv" AS "STATUS",
s3."label" AS "LABEL",
s3."value" AS "VALUE",
a4."active_status_desc" AS "ACTIVESTATUSDESC",
I5."item_reference_uid" AS "PARENTID",
I5."item_desc_display" AS "PARENTDESCDISPLAY"
FROM
("synaptica"."dbo"."sub_elements" s3 LEFT OUTER JOIN "synaptica"."dbo"."ITEMS_REPOSITORY" I1 ON I1."item_uid" = s3."item_uid")
LEFT OUTER JOIN("synaptica"."dbo"."ITEMS_REPOSITORY" I5
RIGHT OUTER JOIN(select "syndetic_key_item", "rel_type_abbrv", "syndetic_rel_item" from "synaptica"."dbo"."SYNDETIC_NETWORK" s55
left join "synaptica"."dbo"."RELATIONSHIP_TYPES" R66 ON S55."syndetic_rel_type" = R66."rel_type_uid"
where (R66."rel_type_abbrv" = 'PhysParent' or R66."rel_type_abbrv" is null))S6
ON
I5."item_uid" = S6."syndetic_rel_item") ON I1."item_uid" = S6."syndetic_key_item",
"synaptica"."dbo"."workflow_types" w2,
"synaptica"."dbo"."active_statuses" a4,
"synaptica"."dbo"."OBJECT_CLASSES" O8,
"synaptica"."dbo"."approval_statuses" a9
WHERE
(I1."item_active_status" = a4."active_status_uid")
AND (a9."approval_status_uid" = I1."item_approval_status")
AND (I1."item_object_class" = O8."object_class_uid")
AND (I1."item_workflow" = w2."workflow_uid")
AND ((left(O8."object_class_abbrv",
7) = left(?VER,
7)))
AND (s3."label" <> 'CLS360_OBJECT_ID')
AND (RIGHT(O8."object_class_abbrv",
10) = 'PHYS CLASS')

How to return a potential NULL in SQL while performing a comparison on the potentially null value

Developing a query that will return information about an item stored across 4 tables. When all the fields have values my query works fine, however some of the data has null fields (which I can't change) that I need to perform my comparison on. When these show up, the row doesn't show up in the query results, even if all the other fields have values.
Here is what I have so far:
select [Item_NO], [Color_Name], [size_code], [style_code], [PERM_UNIT_PRICE]
FROM [USICOAL].[dbo].[ITEM], [USICOAL].[dbo].[COLOR], [USICOAL].[dbo].[SIZE], [USICOAL].[dbo].[STYLE]
where [ITEM_NO] in ('191202002944', '191202003026')
AND [USICOAL].[dbo].[ITEM].[COLOR_ID] = [USICOAL].[dbo].[COLOR].[COLOR_ID]
AND [USICOAL].[dbo].[ITEM].[SIZE_ID] = [USICOAL].[dbo].[SIZE].[SIZE_ID]
AND [USICOAL].[dbo].[ITEM].[STYLE_ID] = [USICOAL].[dbo].[STYLE].[STYLE_ID]
For these 2 items numbers, the Size_ID field is null. How can I get the results to reflect this null?
SELECT
[Item_NO]
,[Color_Name]
,[size_code]
,[style_code]
,[PERM_UNIT_PRICE]
FROM
[USICOAL].[dbo].[ITEM] i
LEFT OUTER JOIN
[USICOAL].[dbo].[COLOR] c
ON c.[COLOR_ID] = i.[COLOR_ID]
LEFT OUTER JOIN
[USICOAL].[dbo].[SIZE] s
ON s.[SIZE_ID] = i.[SIZE_ID]
LEFT OUTER JOIN
[USICOAL].[dbo].[STYLE] t
ON t.[STYLE_ID] = i.[STYLE_ID]
WHERE
[ITEM_NO] in ('191202002944', '191202003026')

sql query is not working properly

i am trying to get non matching records from two table by comparing some columns which are common in both tables.i am using sql query to get the result. my first table is snd_marketvisits this table have properties like id ,pjpCode , section code, popCode .pop_name and landmark similary my 2nd table have pjpcode , section code, popcode popname are common and there are some other fields.i want to get the names of the pop which are not in second table but present in snd_marketvisit table by comparing popcode, sectioncode and pjpcode in both tables.
SELECT *
FROM snd_marketvisits sm
LEFT JOIN snd_marketvisit_pops sp ON
sm.distributorCode = sp.distributor AND
sm.pjpCode = sp.pjp AND
sm.sectionCode = sp.sectionCode AND
sm.popCode = sp.popCode
WHERE
sm.sectionCode = '00016' AND
sm.pjpCode = '0001' AND
sm.distributorCode = '00190A'
It depends on the database, as far as I know, but if you ask for NULL inside your yoined fields you should get only the rows without a match.
SELECT *
FROM snd_marketvisits sm
LEFT JOIN snd_marketvisit_pops sp ON
sm.distributorCode = sp.distributor AND
sm.pjpCode = sp.pjp AND
sm.sectionCode = sp.sectionCode AND
sm.popCode = sp.popCode
WHERE
sm.sectionCode = '00016' AND
sm.pjpCode = '0001' AND
sm.distributorCode = '00190A'
AND sp.distributor IS NULL

Comparing multiple row and columns in sql server

I had two tables first one is the real table second one is the temporary. I had to compare this two table . First table had 'KartelaKod' field should match with temp's 'KartelaKodu' field, First's 'OzellikKod' field should match with temp's 'OzellikKodu',first's 'AltKod' field should match with temp's 'StokKodu'.
Finally if it doesn't match exactly it should return ''.
PS: You should have to group first table according to VrtUrunKod.
In example if MAM|002's (which is VrtUrunKod) OzellikKod field doesn't match with temp table's OzellikKodu field, it should look for another VrtUrunKod record.
Edit1:isnull((Select TOP 1 VrtUrunKod From KonfigOlusmusOzellikler k Where Sirket_Kod = #sp_Sirket_Kod and AnaUrunKod = #spStokKod and
exists(Select * From #TempDegisen t Where t.KartelaKodu = k.KartelaKodu and t.OzellikKodu = k.OzellikKod and t.StokKodu = k.AltKod)),'') this is what i written so far you can ignore Sirket_Kod field and parameter.
This should produce a list of rows where there is a k with no matching t, and at with no matching k. If no rows result, all rows are matched:
SELECT *
FROM
KonfigOlusmusOzellikler k
FULL OUTER JOIN
#TempDegisen t
ON
t.KartelaKodu = k.KartelaKodu and
t.OzellikKodu = k.OzellikKod and
t.StokKodu = k.AltKod
WHERE t.KartelaKodu IS NULL OR k.KartelaKodu IS NULL