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? - sql

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;

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

SQL Query to count multiple values from one table into specific view

I like to request your help. I can get the results seperated but now i want to create a query which has it perfect for a external person. my explanation:
I have a statistics database with in this database a table when some records comes in and each records has several columns with values etc...
Now one of these columns is called "MT"
MT Column can have only one of the following values per records: A,B,C,D,E
The records also have a columne called TotalAmount which indicate a size of a value outside the database. This TotalAmount column is numeric without decimals and can have a value between 1 and 10.000.
And the last part is the records it self, the table has X amount of records.
So Basicly i need to create a query which seperates each MT value and calculates the amount of records per MT and the sum of TotalAmount.
This is on SQL Server 2005.
Many thanks for your assistance!
Very hard to guess without a full db schema. But I think you need.
SELECT MT, Count(*), SUM (TotalAmout)
FROM YourTable
GROUP BY MT

SQL update statement to populate numerical series for each distinct subset of table records

I need the SQL update statement to assign consecutive sequence numbers to subsets of records in a table. I'm using MS access.
Let's say the current table has records like:
notebook,blue
notebook.Yellow
pencil,yellow
chair,blue
desk,green
desk,blue
I would like to add another field to the table and populate it as follows:
notebook,blue,1
notebook.Yellow,1
pencil,yellow,2
chair,blue,2
desk,green,1
desk,blue,3
you see that I have given a consecutive number assignment based on a certain set of criteria. In this example, the criteria was a distinct value in the second field (in real life, the criteria will be a distinct combination of values from several fields, but all the relevant fields are within the same table... no join is needed to get the criteria). since there are three records with blue in field 2, these are numbered 1,2,3. And since there are two records with yellow, they are numbered 1,2.
So I can't derive the numbering from the row number, since I have several numbering series in the same table all starting with 1.
Also, I need it to be a query where I don't have to explicitly specify the value in the second field. I just want each unique value in the second field to get its own numbering series. that is, I don't want to have to explicitly write one query to generate the numbers for "blue", and write a separate query to generate the numbers for "yellow"
The maximum number of records in the series is under 1000. So I don't mind if I would need to create and auxiliary table with 1000 records, with a field containing the values 1 to 1000. Then the update statement to the primary table could pull in the next value from the auxiliary table.
But I don't know the SQL syntax to use for this update statement, or for the update statement for any other approach. So I need your advice.
I'm not sure how to do this with a single SQL statement, but here are 2 SQL statements that could be used to handle each case:
insert into table ('desk', 'blue', 1)
where not exists (select field3 from table where field1 = 'desk' and field2 = 'blue');
insert into table (field1, field2, field3)
select field1, field2, count(1) + 1
from table
where field1 = 'desk'
and field2 = 'blue'
group by field1, field2;
Create Table #TableAutoIncrement (ID int identity(1000 , 1) , item varchar(20), COLOR varchar(20) )
Insert INTO #TableAutoIncrement
(item, COLOR )
SELECT item, COLOR FROM YOURTABLE
--- GETTING all the values from the temporary table
SELECT * FROM #TableAutoIncrement
A colleague of mine worked out the necessary SQL. Here's the generalized solution (note that I really needed to number the multiple series in my data set based on a combination of two fields. In my simplified example in the original post, I was using only one field--color--but since I really need two fields, that's what I show in this solution.
SELECT *,
(SELECT COUNT(T1.ID)
FROM
[TableName] AS T1
WHERE T1.ID >= T2.ID and t1.[NameCriteriaField1] = t2.[NameCriteriaField1]
and t1.[NameCriteriaField2]= t2.[NameCriteriaField2])
AS Sequence into OutputResultsTableName
FROM
[TableName] AS T2
ORDER BY [NameCriteriaField2] , [NameCriteriaField1]
The source table is set up with "ID" as field with an integer value. Every record has a unique value of ID, but it does not matter if there are gaps in the ID or how the records are sequenced against the ID. (e.g., the typical MS access auto numbered primary key field serves this purpose)
This query is set up to assume that there are two fields in your data set that you want to use to group your records and assign a numerical series count to each record within each group. (Thus your table may contain multiple groups, and each group has its own numbering series starting with 1. But the way the query is formulated, there are exactly 2 criteria that define the group.) You cannot use any where clauses to further filter the records that get counted. Through experimentation, I found that adding where clauses gives unreliable results where records can get omitted. So if you need the results to be filtered so that some records are not to be included in the numerical series for a particular group, then do one of the following before running my query:
run a query to delete the undesired records from the source table
first copy all records from the source table into a new table and delete the records from the new table that should not be numbered, and run my query on the new table
deleting extraneous records before running this query is needed only if those records qualify as members of a group defined by criteria 1 and criteria 2. If there are extraneous records that don't match those two criteria, you can leave them in the table, because they will not impact the numbering of the records within the groups that you care about. They will just get their own independent numbering, which you can just ignore.**
The numbering of each group starts at 1, and the query dynamically defines the groups based on the distinct combinations of criteria1 and criteria2. However, if you have records that do not belong to any group, these records will all be numbered with 0. (Criteria1 and criteria2--at least to the extent of my testing--are non-null values. (In theory--at least on Microsoft Access, an empty string is different than Null, but I did not test this with empty strings either.) If you have records that have null in the criteria1 or criteria2 fields, MS Access consider these records as not belonging to any group and thus numbers them with 0. That is, these distinct groups need to define by non-null values for criteria1 and criteria2, and thus this is different than the way SQL DISTINCT statement works.
If you need to have NULL as a valid criteria for defining the group (and thus to have groups defined by NULL numbered), it's very simple. Prior to running my query, first run an update statement that changes all instances of null values in criteria1 or criteria2 to the phrase "placeholder for null field". Then run my query. On the result set (after the numbering has been assigned to the groups), run another update to change all occurrences of the placeholder phrase back to null.
Adjustment to syntax if your group is defined by only one field criteria
SELECT *,
(SELECT COUNT(T1.ID)
FROM
[TableName] AS T1
WHERE T1.ID >= T2.ID and t1.[NameCriteriaField1] = t2.[NameCriteriaField1] )
AS Sequence into OutputResultsTableName
FROM
[TableName] AS T2
ORDER BY [NameCriteriaField2] , [NameCriteriaField1]
Adjustment to syntax if your group is defined by combination of 3 field criteria
SELECT *,
(SELECT COUNT(T1.ID)
FROM
[TableName] AS T1
WHERE T1.ID >= T2.ID and t1.[NameCriteriaField1] = t2.[NameCriteriaField1]
and t1.[NameCriteriaField2]= t2.[NameCriteriaField2]
and t1.[NameCriteriaField3]= t2.[NameCriteriaField3])
AS Sequence into OutputResultsTableName
FROM
[TableName] AS T2
ORDER BY [NameCriteriaField2] , [NameCriteriaField1]

Get fields from one column to another in Access

Below i have a table where i need to get fields from one column to three columns.
This is how i would like the data to end up
Column1
Music
Column2
com.sec.android.app.music
Column3
com.sec.android.app.music.MusicActionTabActivity
Give the table a numeric autonumber id
Remove the rows with no data with a select where blank spaces or null
Find records with no point in the content with a select
Use the previous query as a source and use the id to find the id + 1 to find the next record and do the same with + 2 to find the second row
Build a table to hold the new structure and use the query as a source to insert the new created data in the new table with the 3 columns structure.
This is an example using sql server
Test table design
Data in table
Query
Look at the query from the inside. The first query inside clean the null records. Then the second query find the records with out point. This records are the reference to find the related two records. Then the id of the records with out point are used to make a query in the select adding 1 for the next record and then other query adding 2 to find the other record. Now you only need to create a table to insert this data, using this query as the source.

Joining two tables that has no columns in common

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)