Ms Access Query - database

If there are two tables. Table A has table_code as PK and its FK in Table B.
How can query be designed so that the results display all those values of table_code which is in Table A but not in Table B?
Tried all three joins
Tried Criteria is null and is not null

try
SELECT tableA.table_code
FROM tableA LEFT JOIN tableB ON tableA.table_code = tableB.table_code
WHERE (((tableB.table_code) Is Null));
if this doesn't help, show us the SQL you tried.

Related

Issue with join on SQL Server tables with same plugin and same datatype

I want to join two table using same storage plugin. But One Of the Column showing null value.
I am using this query:-
select * from SqlServer.test_mopslive.reports as Reports join
SqlServer.test_mopslive.reportsetting as ReportSetting on Reports.ID = ReportSetting.ID
Here SqlServer is Storage Plugin Name, test_mopslive is Database Name, reports, reportsetting are Table Names.
While executing this query T_ID showing Null.
But If I am using two different storage plugin name with same credential it works properly.
TABLE 1:-
create table Reports (ID bigint, Name varchar(25));
insert into Reports values (29, 'SqlThree');
insert into Reports values (30, 'SqlTwo');
insert into Reports values (31, 'SqlThree');
TABLE 2:-
CREATE TABLE ReportSetting
(
P_id bigint not null auto_increment primary key,
Name varchar(25),
ID bigint,
CONSTRAINT fk_ID FOREIGN KEY (ID)
REFERENCES Reports(ID));
insert into ReportSetting values (1,'My_Sreekant1', 29);
insert into ReportSetting values (2,'My_Sreekant2', 30);
insert into ReportSetting values (3,'My_Sreekant3', 31);
Is it possible to join two table using same storage plugin name? If yes,then What am I doing wrong in this query?
You keep changing the text of your question and code - you were using a RIGHT OUTER JOIN before and now, you're using plain JOIN which is the same as INNER JOIN.
Since you're using an INNER JOIN, you will not get any rows from either table that do not fulfill your join condition:
join SqlServer.test_mopslive.reportsetting ReportSetting
on Reports.ID = ReportSetting.ID
If you run this query (against SQL Server - I don't know how Drill works), you will not have any rows where Reports.ID or ReportSetting.ID are not equal, nor will you have any rows where either of them are null. Meaning, if a Report does not have any entries in the ReportSetting table, that Report does not show up in your result set, and, if a ReportSetting does not have a match in the Reports table, it will not show up in your result set.
If you were using a RIGHT OUTER JOIN, you would get all the rows in the JOIN target table (ReportSetting), with data from the JOIN source (Reports) where available or else null in those fields.
If you were using a LEFT OUTER JOIN, you would get all the rows in the JOIN source table (Reports), with data from the JOIN target (Report Settings) where available or else null in those fields.
If you were using a FULL OUTER JOIN, you will get all rows from both tables. with nulls in fields where there is not a match.

Need help on a sql query to get data from multiple tables

I have a couple of tables that have data in them that I am looking to get information from. Here is the rundown....In table 1 I have bunch of columns that I am pulling data from, one of the columns is a user ID (which is a number)that was the last userID to modify a record. In table 2 I want to pull in the name of that user based on the ID that is pulled from the other table (this table has both the userID and the username).
so my final query would have the columns in table 1 as well as the username from table 2 to show that was the user to last edit the record. I assume this has to be done in a nested select statement but for the life of me I cannot come up with the correct syntax.
Can anyone help me out?
Thanks
Jeff
Yes, you need a very basic join that link both tables together.
Select t1.UserID,
t2.UserName
FROM table1 t1 INNER JOIN
table2 t2 ON t1.userid=t2.userid
select t1.*, t2.{username} from table1 as t1
join table2 as t2 on t1.{userId}=t2.{userid};
change {username} with the actual column name of user
similarly {userId} with appropriate column name in tables.
Hope it helps you.
this is standard inner join query, to learn more consider reading: http://www.w3schools.com/sql/

How to find missing rows?

I have two identical tables A and B. And both the tables have same fields, as an example Table A (bin, storage, plant) and B (bin, storage, plant). But when I checked the data, table A has 5238 rows and B has 5249 rows. So I dont know which 11 rows are missing. I need help to write a query where I can find those missing rows.
Thanks for the help in advance.
Can use the EXCEPT command for your problem:
SELECT bin
FROM tableB
EXCEPT
SELECT bin
FROM tableA;
Shows all bins which are in tableB but not in tableA.
select *
from tableA
full outer join tableB on tableA.bin = tableB.bin
where tableA.bin is null or tableB.bin is null
SQL-Server allows a full outer join. You can select all records from both table and limit the result to those where the join does not find matches on the other table.

Proper way to filter a table using values in another table in MS Access?

I have a table of transactions with some transaction IDs and Employee Numbers. I have two other tables which are basically just a column full of transactions or employees that need to be filtered out from the first.
I have been running my query like this:
SELECT * FROM TransactionMaster
Where TransactionMaster.TransID
NOT IN (SELECT TransID from BadTransactions)
AND etc...(repeat for employee numbers)
I have noticed slow performance when running these types of queries. I am wondering if there is a better way to build this query?
If you want all TransactionMaster rows which don't include a TransID match in BadTransactions, use a LEFT JOIN and ask for only those rows where BadTransactions.TransID Is Null (unmatched).
SELECT tm.*
FROM
TransactionMaster AS tm
LEFT JOIN
BadTransactions AS bt
ON tm.TransID = bt.TransID
WHERE bt.TransID Is Null;
That query should be relatively fast with TransID indexed.
If you have Access available, create a new query using the "unmatched query wizard". It will guide you through the steps to create a similar query.

UPDATE query from OUTER JOINed tables or derived tables

Is there any way in MS-Access to update a table where the data is coming from an outer joined dataset or a derived table? I know how to do it in MSSQL, but in Access I always receive an "Operation must use updateable query" error. The table being updated is updateable, the source data is not. After reading up on the error, Microsoft tells me that the error is caused when the query would violate referential integrity. I can assure this dataset will not. This limitation is crippling when trying to update large datasets. I also read that this can supposedly be remedied by enabling cascading updates. If this relationship between my tables is defined in the query only, is this a possibility? So far writing the dataset to a temp table and then inner joining that to the update table is my only solution; that is incredibly clunky. I would like to do something along the lines of this:
UPDATE Table1
LEFT JOIN Table2 ON Table1.Field1=Table2.Field1
WHERE Table2.Field1 IS Null
SET Table1.Field1= Table2.Field2
or
UPDATE Table1 INNER JOIN
(
SELECT Field1, Field2
FROM Table2, Table3
WHERE Field3=’Whatever’
) AS T2 ON Table1.Field1=T2.Field1
SET Table1.Field1= T2.Field2
Update Queries are very problematic in Access as you've been finding out.
The temp table idea is sometimes your only option.
Sometimes using the DISTINCTROW declaration solves the problem (Query Properties -> Unique Records to 'Yes'), and is worth trying.
Another thing to try would be to use Aliases on your tables, this seems to help out the JET engine as well.
UPDATE Table3
INNER JOIN
(Table1 INNER JOIN Table2 ON Table1.uid = Table2.uid)
ON
(Table3.uid = Table2.uid)
AND
(Table3.uid = Table1.uid)
SET
Table2.field=NULL;
What I did is:
1. Created 3 tables
2. Establish relationships between them
3. And used the query builder to update a field in Table2.
There seems to be a problem in the query logic. In your first example, you LEFT JOIN to Table2 on Field1, but then have
Table2.Field1 IS NULL
in the WHERE clause. So, this limits you to records where no JOIN could be made. But then you try and update Table 1 with data from Table2, despite there being no JOIN.
Perhaps you could explain what it is you are trying to do with this query?

Resources