SQL Left Join only returning matching records - sql

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

Related

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

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

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.

How do you return each record in a table as a single concatenated string that contains at least 1 of 4 values for each record in a different table?

I have a table with 92k records with only one column, containing notes about leads.
I have another table with 32k lead records with 3 phone columns and one email column among others.
I want to query the 92k records to see if they contain any of the numbers or emails, then concatenate all those records and set that concatenated string as the value of the Notes column of the 32k table.
I have created a spreadsheet that will work but it's been processing for hours and is only half way through.
enter image description here
=IFERROR(ifs(
not(isblank(H30627)),Join(char(10),QUERY(Tasks!A:A,"SELECT A Where A
Contains '"&H30627&"'",0)),
not(isblank(F30627)),Join(char(10),QUERY(Tasks!A:A,"SELECT A Where A
Contains '"&F30627&"'",0)),
not(isblank(E30627)),JOIN(char(10),QUERY(Tasks!A:A,"SELECT A Where A
Contains '"&E30627&"'",0)),
not(isblank(D30627)),Join(char(10),QUERY(Tasks!A:A,"SELECT A Where A
Contains '"&D30627&"'",0))),"")
I felt that Bigquery would save a lot of time but I am a SQL noob and this is returning a left outer join error without the STRING_AGG and 0 modified rows with it.
#standardSQL
UPDATE sfdc.workingFin
SET Notes = (SELECT STRING_AGG(string_field_0) from sfdc.tasks where
string_field_0 LIKE (SELECT Email from sfdc.workingFin))
WHERE TRUE
You can use a correlated subquery:
UPDATE sfdc.workingFin wf
SET Notes = (SELECT STRING_AGG(string_field_0)
FROM sfdc.tasks t
WHERE t.string_field_0 LIKE wf.Email
)
WHERE true;

Losing data during joins in Access SQL

I have two tables, with a many to one relationship, and I am using a left join to get all the data from the many table and merge it with the data from the one table.
My first table looks like:
create table tasks (date text(6),task text(20),completed text(1))
Second table:
create table completer (data text(6),name text(20))
Keep in mind that date in the completer table is a primary key so only one name is associated with each date.
I can write a query that joins these tables and creates the correct structure:
select tasks.task,completer.name,tasks.completed from tasks left join completer on tasks.date=completer.date;
The problem with my query is my binary "completed" variable that is either 0, or 1 is always 0 after the join, no matter what value it is in the original table.
Why am I losing the data associated with the completed variable? I've tried using Access's yes/no variable instead, but that has the same problem with losing information during the join.
Thanks

Why am I loosing table data from adding additional join statement in SQL Server?

So I have table X with let's say 50 rows of data in it and it is being joined to another table with only 2 rows in it. When I "normal" JOIN them together only the overlapping data will show, i.e. the two rows of data that are found in the larger collection of 50. In my case I want all 50 rows to persist so I use a LEFT JOIN and I am returned all 50 rows like planned.
Now I want to start adding and joining in other tables to get additional data about these rows and have them display togehter, whenever there is no data, I am fine with getting null. Now I'm adding a new JOIN and it's only going to find data for those two rows and not for the other 48, which is perfectly fine and I would like NULL to be displayed where no match is found. My problem is that rather than doing that, displaying null, it is instead removing the 48 rows will partial null data in their columns entirely and only showing the two that match, WHY?
I can provide code if needed, I thought this may be easier to understand.
If you inner join the third table, there are only 2 rows that can match (I assume you don't match on nulls). Only 2 rows from the second table have non-null values in them, so only those 2 can produce a result if you do an inner join with the third table.
You will need another left join in this case. This will return all the rows from the second table, including those that are all null (as the result of the first left join).