sql query is not working properly - sql

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

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

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

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));

Record exist in one DB2 table but not in another table

I have 2 DB2 tables. I want to find out records that are in table A is not Table B with the following condition
I wrote this query and it is not working
SELECT A.CL_BATCH_DEPT,
A.CL_TRANS_CODE, A.CL_CUR_DOC_NO
FROM DBPA60AC.TB_ACCOUNT_EVENT A
LEFT JOIN DBPA60AC.TB_DOCUMENT B ON A.CL_CUR_DOC_NO = B.CL_DOCNO
WHERE A.CL_BATCH_DEPT = 'R07' AND A.CL_TRANS_CODE = '210'
AND A.CL_CUR_DOC_NO = "PI%" AND
B CL_DOCNO IS NULL
I am guessing that you want:
SELECT e.*
FROM DBPA60AC.TB_ACCOUNT_EVENT e LEFT JOIN
DBPA60AC.TB_DOCUMENT d
ON e.CL_CUR_DOC_NO = d.CL_DOCNO
WHERE e.CL_BATCH_DEPT = 'R07' AND
e.CL_TRANS_CODE = '210' AND
e.CL_CUR_DOC_NO LIKE 'PI%' AND
d.CL_DOCNO IS NULL;
That is, the comparison to PI% suggests that you really want LIKE.
Note that I also changed the aliases so they are meaningful.

How can I do a SQL join to get a value 4 tables farther from the value provided?

My title is probably not very clear, so I made a little schema to explain what I'm trying to achieve. The xxxx_uid labels are foreign keys linking two tables.
Goal: Retrieve a column from the grids table by giving a proj_uid value.
I'm not very good with SQL joins and I don't know how to build a single query that will achieve that.
Actually, I'm doing 3 queries to perform the operation:
1) This gives me a res_uid to work with:
select res_uid from results where results.proj_uid = VALUE order by res_uid asc limit 1"
2) This gives me a rec_uid to work with:
select rec_uid from receptor_results
inner join results on results.res_uid = receptor_results.res_uid
where receptor_results.res_uid = res_uid_VALUE order by rec_uid asc limit 1
3) Get the grid column I want from the grids table:
select grid_name from grids
inner join receptors on receptors.grid_uid = grids.grid_uid
where receptors.rec_uid = rec_uid_VALUE;
Is it possible to perform a single SQL that will give me the same results the 3 I'm actually doing ?
You're not limited to one JOIN in a query:
select grids.grid_name
from grids
inner join receptors
on receptors.grid_uid = grids.grid_uid
inner join receptor_results
on receptor_results.rec_uid = receptors.rec_uid
inner join results
on results.res_uid = receptor_results.res_uid
where results.proj_uid = VALUE;
select g.grid_name
from results r
join resceptor_results rr on r.res_uid = rr.res_uid
join receptors rec on rec.rec_uid = rr.rec_uid
join grids g on g.grid_uid = rec.grid_uid
where r.proj_uid = VALUE
a small note about names, typically in sql the table is named for a single item not the group. thus "result" not "results" and "receptor" not "receptors" etc. As you work with sql this will make sense and names like you have will seem strange. Also, one less character to type!

Selecting more than one column to query in sql

I am having a bit of trouble, probably from my understanding of SQL. Here is the SQL I am currently using:
CREATE TEMPORARY TABLE Temp
(
sPropertyCode VARCHAR(9),
sDataDate DATE,
PRIMARY KEY (sPropertyCode)
);
INSERT IGNORE Temp (sPropertyCode, sDataDate)
SELECT sPropertyCode, sDataDate
FROM tasks as t, task_data AS d
WHERE t.iTaskId = d.iTaskId
AND iRemoved = 1
AND sDataType = 'sAgencyAgreementDate'
AND iBusinessStreamId = 9;
SELECT t.sPropertyCode, sDataDate, SFirstSeen, sTaskType
FROM tasks AS t, temp AS a
WHERE iRemoved = 1
AND iBusinessStreamId = 9
AND sTaskType IN ('RF', 'IF', 'CM')
AND t.sPropertyCode = a.sPropertyCode
ORDER BY sPropertyCode, sFirstSeen;
DROP TABLE Temp;
So the references 'RF', 'IF' and 'CM' are tasks that we receive. Each propertycode can touch each of these tasks once, and only once. I would like to show the date that each one of these was touched by the propertycode. It is working at the moment but it is showing it in three columns with the tasks types in one column. I would like each task to show in a seperate column with the date it was first seen in its own corresponding column.
So from the picture below is how it is currently laid out with the code above.
And here is how I would like it to look, instead of the tasks showing down the side, I would like them to show accross in columns with their own specific dates
Thank you in advance :)
SELECT t.sPropertyCode, sDataDate, SFirstSeen, a1.sTaskType, a2.sTaskType, a3.sTaskType
FROM tasks AS t
INNER JOIN temp AS a1 on t.sPropertyCode = a1.sPropertyCode and a1.sTaskType = 'RF'
INNER JOIN temp as a2 on t.sPropertyCode = a2.sPropertyCode and a2.sTaskType = 'IF'
INNER JOIN temp as a3 on t.sPropertyCode = a3.sPropertyCode and a3.sTaskType = 'CM'
WHERE iRemoved = 1 AND iBusinessStreamId = 9
ORDER BY sPropertyCode, sFirstSeen;
You can also user outer join if not always have all 3 tast type.
What is the difference in meaning between the CM,IF and RF columns for any row in your 5-col table ? They're always the very same value in the example you listed.