Vba sql table join on matching field where 2nd field not matching - sql

Good day everyone. I'm sure this is a fairly common issue but I trawled the internet and I cannot find an answer that works in my case. I have 2 tables which both have 2 fields and both field names are the same. I need to select all records in table 1 where the 1st field matches table 2 1st field and the 2nd field in table 2 does not match field 2 in table 1. Sorry if I'm not explaining this very well. Table 1 has around 10K records with client-name and country. table 2 is a unique list of client names (around 1K) with client-name and country fields. so I need to select all the records from table 1 with same client-name but where country in table 2 is not the same as in table 1. I've been on this for several hours now so any help would be most welcome.
The data is in 1 Access Database and I'm using ADO connection. What I have is:-
SELECT [client-name], [country] FROM [table1] LEFT JOIN [table2] ON [table1].[client-name]
= [table2].[client-name] WHERE NOT [table1].[country] = [table2].[country];

Related

SQL Left Join only returning matching records

I have a table with 300+ records I am trying to join with another, instead of returning all records with the second tables matching records included,it only returns matching records.
Table vtRawDayData can have in it's StopPlaceIDs varchar field, null, a single ID in the form P12C1234 or multiple ID's P12C1234, P12C2345....
Table tblPools has a varchar ID field and an integer ServiceLevel_ID field
I want to return all 300+ records from the vtRawDayData Table with the ServiceLevel_ID where
there is an exact match to a single ID. So records with null or multiple ID's will have a null value in the ServiceLevel_ID field.
What I have with just pertinent fields:
Select
rdd.Vehicle,
rdd.Date,
rdd.StartLocation,
rdd.StartGeoFence,
rdd.StartPlaceIDs,
rdd.StartLatitude,
rdd.StartLongitude,
rdd.StartTime,
rdd.TravelTime,
rdd.StopLocation,
rdd.StopGeoFence,
rdd.StopPlaceIDs,
rdd.StopLatitude,
rdd.StopLongitude,
rdd.ArrivalTime,
rdd.StopDuration,
rdd.StopDurationSeconds,
rdd.IdleDuration,
rdd.DepartureTime,
rdd.Odometer,
rdd.IdleTimeSeconds,
rdd.StopDurationSeconds / 60 as StopDurationMinutes,
p.ServiceLevel_ID
FROM
vtRawDayData rdd
LEFT JOIN
tblPools p
WHERE
rdd.StopPlaceIDs = p.ID
This only returns 99 Exact matching records. I just want to add the ServiceLevel_ID to the records with a single ID in the StopPlaceIDs field
What am I missing?
Posted this after working on it for some time, getting sample data into my program from a .csv file while in design mode was time consuming. Then went in the shower and it came to me. I was hoping to get back before anyone saw it:)
Thanks

optimizing PGSQL SQL search queries on big texts ('like', full text search, ... )

We have a software solution which is used by +200 customers. We recently switched to pgsql, because our former database was too slow handeling the search queries our customers use.
Our dabatabase looks like this:
TABLE A
1. ID
(+ some other fields which aren't important here)
TABLE B
This table is used to store 'data' on the items in table A. This is different for every customer. For example 'Type' can be 'CLIENTNAME' and value 'AZERTY'. One record on TABLE A can have infinite records in TABLE B. Mostly 1 record in Table A has between 5 - 10 records on Table B.
1. ID TABLE A
2. TYPE
3. VALUE
TABLE C
1. TABLE A ID
2. VERSIONNR
3. DESCRIPTION
This file has the different verions of the records in TABLE A. Each of these versions has an extended description. This can range from 0 characters to infinite.
Our problem: our customers are used on 'google-like' searching. For example: they type 'AZERTY' and we show all the records from TABLE A where the ID of TABLE A:
'AZERTY' is in the description of the most recent version of TABLE C
'AZERTY' is in one of the values of TABLE B
Additional problem: this search is a 'contains'. If they search 'ZER', they should also find the records with 'AZERTY' in it. Multiple arguments are an 'AND', if they search for 'ZER 123', we need to show all records where the description matches 'ZER' and '123' or the values match 'ZER' and '123'.
What we have done so far:
There is an option a user can check in/out whether they want to search the description or not. We mosty advice them to only search for the values and only use the description in case of need.
We make several search threads to the database for one search query, because searching all documents at once would take too much time.
Some time ago, on our former slow database engine, a collegue of mine made 'search tables', basically this is a table which contains all values on a TABLE A ID so there isn't need for any join in the SQL query when searching. It looks like this:
TABLE D
TABLE A ID
VALUES (all values from TABLE B for this TABLE A ID, seperated by a ' ')
DESCRIPTION (the description of the most recent version for this TABLE A ID)
Example record:
- 1
- ZER 123 CLIENT NAME NUMBER 7856 jsdfjklf 4556423
- DESCRIPTION CAN BE VERY LONG.
If a customer searches for 'ZER 123' this becomes:
"select TABLE_A_ID from TABLE_D where values like '%ZER%' and values like '%123%'"
Important:
Some of our customers have alot of records in TABLE A. +5.000.000, which means there are alot of records in TABLE B (+/- 50.000.000). Most of our customers have between 300.000 and 500.000 records in TABLE A.
My questions:
Is there a better / faster way to search through all the values then that search table? Without the search table i would have to make a join for every ' ' in the search argument of the customer, which will work too slow (i think?) if they have alot of records in TABLE A. For example:
select ID from TABLE_A
INNER JOIN TABLE_B Sub1 ON TABLE_A.ID = Sub1.TABLE_A_ID and Sub1.VALUE like '%ZER%'
INNER JOIN TABLE_B Sub2 on FILE_A.ID = Sub2.TABLE_A_ID and Sub2.VALUE like '%123%'
I have taken a look at the full text search in PGSQL. I don't think i can use it since you can't use it as like (= 'contains') ?
Is there any index I can use on the values (FILE B or search file) and description (FILE C or search file) to make the searches faster? I've read on it and i don't think there is any, because indexes aren't used when searching with "like '%ZER%'" ?
I hope i've explained this cleary.
Thanks in advance!
Your terminology is confusing, but I assume you mean "tables" when you write "files".
You cannot reasonably search in several tables with a single query, but you can search in several columns of a single table at the same time.
Based on your description, I would say that you need a trigram index on the concatenation of the relevant string columns in the table.

MS Access Populating Data in Column with Data from another table (SQL)

I'm needing help with a (probably very basic) command. On a test spreadsheet I have been asked to populate a column with data from another table in MS Access. However, to add the data from table 1 to table 2 it relies on another set of data to be matched with it.
In table 1 there is 2 Tables "Customer Number" and "CustomerID" - Both of which are full data sets 40,000 entries so I can't copy paste.
In table 2 the "CustomerID" is there but there are no entries.There are all entries for "Customer Number".
I need to add the customerid from table 1 to table 2 but only when the customer number is matching on both tables.
I have tried using Join function and insert but I only have been using access for about 3 days and need some help. Sorry for the longwinded explanation and hope the question is legible.
Thanks.
Does this work with JOIN?
UPDATE table2 t2 JOIN
table1 t1
ON t2.CustomerNumber = t1.CustomerNumber
SET t2.CustomerID = t1.CustomerID;

Inner Join query between 2 tables with multiple conditions

We have 2 tables Application and Address, Application table has columns like home_address,office_address and delivery_address and all these 3 column is of number type having values like say 111,112 and 113 for a row of it.It has many such rows. In a separate table we have say column like id and address having lot of records with different id's. we need to get output like say all above 3 columns plus 3 more columns like say full_homeaddress, full_officeaddress , full_deliveryaddress in every row of output.
If it was only one column like home_address in Application table, i would have easily done the join with primary key of Address table and one foreign key of Application table but here we need to display full address of 3 different fields simultaneosly in each row of output.
I think for one row of output, i will have to get records from 1 row of Application table and 3 different rows of Address table to display in every row of output. For these 3 columns of Application table , required addresses are in 3 different rows of Address table.
Kindly help how to write join for it
SELECT app.*,
home.address_description,
office.address_description,
delivery.address_description
FROM application app,
Address home,
Address office,
Address delivery
WHERE
app.homeaddressId = home.addressId(+)
AND app.officeAddressId = office.addressId(+)
AND app.deliveryAddressId = delivery.addressId(+)

Need SQL to shift entries from one table to another

Heres the situation. I have 2 tables here of the schema:
ID | COMPANY_NAME | DESC | CONTACT
ID | COMPANY_ID | X_COORDINATE | Y_COORDINATE
The first tabel contains a list of companies and the second contacts coordinates of the companies as mentioned.
The thing is that I want to merge the data in this table with the data in another set of tables which already have data. The other tables have similar structure but are already propopulated with data. The IDs are autoincremental.
SO if we have lets say companies marked 1-1000 in table1 and companies marked 1-500 in table 2. We need it merged such that ID number 1 in table 2 becomes ID 1001 when migrated to the other table. And side by side we would also want to migrated the entries in the coordinates table as well in such a way that they map with the new ids of the table. Can this be done in SQL or do I need to resort to using a script here for this kind of work.
i`m not sure i understand how many tables are there and who is table 1 ,2, but the problem is pretty clear. i think the easy way is:
back up all your database before you start this process
add a column to the destination table that will contain the original id.
insert all the records you want to merge (source) into the destination table, putting the original id in the column you added.
now you can update the geo X,Y data using the old ID
after all is done and good you can remove the original id column.
EDIT: in reply to your comment , i`ll add teh code here, since its more readable.
adapted from SQL Books Online: insert rows from another table
INSERT INTO MyNewTable (TheOriginalID, Desc)
SELECT ID, Desc
FROM OldTable;
Then you can do an update to the new table based on values from the old table like so:
UPDATE MyNewTable SET X = oldTable.X , Y = oldTable.Y where
FROM MYNewTable inner JOIN OldTable ON MYNewTable.TheOriginalID = OldTable.ID