Joining two tables that has no columns in common - sql

I am working with two tables with no columns that I can easily join to and get the data I want.
Information about the tables:
I do see something in common in both tables that I might be able to use to join, but I am not sure how it can be done.
Table1: has a column called File_Name. This column captures the imported file location.
example: C:\123\3455\344534\3fjkfj.txt. max Lenth = 200.
Table2: has a column called batch_ID and contains all the records imported by the file listed in table1.
The batch_ID column is exact same thing has the File_Name column in table1.
However, the difference is that it only allows the lenth = 50.
Pretty much it only shows last 50 characters of a filename and directory (50 characters from right to left.)
max lenth = 50
Example: ..\344534\3fjkfj.txt (basically cuts off characters if more than 50 in lenth).
How would I join these tables on those two columns? I know I can create a function and temp tables, but how can I do it without it?
Thanks!

Select Columns
From Table1
Inner Join Table2
On Right(Table1.ColumnA, 50) = Right(Table2.ColumnB, 50)

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

How to get the differences between two - kind of - duplicated tables (sql)

Prolog:
I have two tables in two different databases, one is an updated version of the other. For example we could imagine that one year ago I duplicated table 1 in the new db (say, table 2), and from then I started working on table 2 never updating table 1.
I would like to compare the two tables, to get the differences that have grown in this period of time (the tables has preserved the structure, so that comparison has meaning)
My way of proceeding was to create a third table, in which I would like to copy both table 1 and table 2, and then count the number of repetitions of every entry.
In my opinion, this, added to a new attribute that specifies for every entry the table where he cames from would do the job.
Problem:
Copying the two tables into the third table I get the (obvious) error to have two duplicate key values in a unique or primary key costraint.
How could I bypass the error or how could do the same job better? Any idea is appreciated
Something like this should do what you want if A and B have the same structure, otherwise just select and rename the columns you want to confront....
SELECT
*
FROM
B
WHERE NOT EXISTS (SELECT * FROM A)
if NOT EXISTS doesn't work in your DBMS you could also use a left outer join comparing the rows columns values.
SELECT
A.*
from
A left outer join B
on A.col = B.col and ....

How to update numerical column of one table based on matching string column from another table in SQL

I want to update numerical columns of one table based on matching string columns from another table.i.e.,
I have a table (let's say table1) with 100 records containing 5 string (or text) columns and 10 numerical columns. Now I have another table that has the same structure (columns) and 20 records. In this, few records contain updated data of table1 i.e., numerical columns values are updated for these records and rest are new (both text and numerical columns).
I want to update numerical columns for records with the same text columns (in table1) and insert new data from table2 into table1 where text columns are also new.
I thought of taking an intersect of these two tables and then update but couldn't figure out the logic as how can I update the numerical columns.
Note: I don't have any primary or unique key columns.
Please help here.
Thanks in advance.
The simplest solution would be to use two separate queries, such as:
UPDATE b
SET b.[NumericColumn] = a.[NumericColumn],
etc...
FROM [dbo].[SourceTable] a
JOIN [dbo].[DestinationTable] b
ON a.[StringColumn1] = b.[StringColumn1]
AND a.[StringColumn2] = b.[StringColumn2] etc...
INSERT INTO [dbo].[DestinationTable] (
[NumericColumn],
[StringColumn1],
[StringColumn2],
etc...
)
SELECT a.[NumericColumn],
a.[StringColumn1],
a.[StringColumn2],
etc...
FROM [dbo].[SourceTable] a
LEFT JOIN [dbo].[DestinationTable] b
ON a.[StringColumn1] = b.[StringColumn1]
AND a.[StringColumn2] = b.[StringColumn2] etc...
WHERE b.[NumericColumn] IS NULL
--assumes that [NumericColumn] is non-nullable.
--If there are no non-nullable columns then you
--will have to structure your query differently
This will be effective if you are working with a small dataset that does not change very frequently and you are not worried about high contention.
There are still a number of issues with this approach - most notably what happens if either the source or destination table is accessed and/or modified while the update statement is running. Some of these issues can be worked around other ways but so much depends on the context of how the tables are used that it is difficult to provide a more effective generically-applicable solution.

Data space after join function is "huge"

My table after using the join function has grown from two tables with 27mb and 37mb to 2930mb. This table size is too large too use further on with my project for me.
I am running on Microsoft SQL Server and after joining the two tables the data space for the table have grown considerably. There are about 40000 rows in the first table and 428 in the second. Number of rows after joining is 37000. This should be correct.
I suspect it might be because of how the data type for the column has been defined. I imported one of the tables from excel (the one with 40000 rows) and therefore the program only took a sample of the first rows to define the datatype. Some of the rows further below in the table exceeded the given data type given (mostly nvarchar(255) when some rows had about 400 characters). Therefore i changed the first column where now almost every column has a field with 300 characters in the forst row. SQL then automatically changed to nvarchar(MAX) for these columns and it worked.
The join function i used is this one:
SELECT * into NewTable
FROM Table1
JOIN Table2
ON Table2.Row1=Table1.Row2;
I would like to have the same table as result, but not larger than 50mb.
The screenshot is from after the join operation

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;