SSIS - export from SQL table to multiple flat files based on first column values - sql

Here is an example of what the SQL Table looks like:
Name Class Grade
Jesse English A
Jesse Math C
Jesse History A
Scott Math B
Scott History B
Scott English A
Mike History A
Mike English D
I am trying to get SSIS to dynamically create a flat file for each person. Example:
Flat File name: Jesse
Name Class Grade
Jesse English A
Jesse Math C
Jesse History A
Flat File name: Scott
Name Class Grade
Scott Math B
Scott History B
Scott English A
Flat File name: Mike
Name Class Grade
Mike History A
Mike English D
I can easily create a static link between the sql table and the flat file but I plan on adding a lot of people to the table which would otherwise cause me to create a data flow task for each. This would not be ideal. I was hoping for a for each loop that identified the distinct values within the Name column and then output the qualified rows into a flat file.

This is how your package should look like :
right click->variable 1.student-->object
2.students-->string(for holding all names of students giving you required) result
In the data flow -- connection managers--right click-- flat file connection-->properties-->expression mention it as something like this ::"C:\\Users\\user\\Desktop\\ssis_stuff_from_stackoverflow\\citys.txt-"+ #[User::student] +".txt"
the package succesfully executes affecting 3 rows and adding 3 flat files in the folder
path
here is one good example

First run a query to find a recordset of unique students:
select distinct name from myTable
Then use the foreach loop to loop though and run the following parameterized query:
SELECT class, grade
FROM myTable
WHERE name = ?
Use a derived column to include the name to the resultset.
Put this in a flat file destination. The connectionstring for the output file will be dynamic.
These are the steps. If you get stuck, there are plenty of examples online, or feel free to ask.

Related

Alteryx Designer - How to retrieve only first and last name from field excluding middle initials?

I need help in writing SQL code in Alteryx Designer.
My table employees contains a column Name with values shown below. However, I need the expected output as shown below.
Please help.
Name:
Smith, Mary K
Koch, J B
Batoon Rene, Anne S
Vaughan-tre Doctor, Maria S
Excepted output:
Smith, Mary
Koch, J
Batoon Rene, Anne
Vaughan-tre, Maria
The middle initials and “Doctor” word is removed.
Not sure why you need to use SQL if you have the data in Alteryx?
So, you need to remove the right hand 2 characters and the word 'Doctor' from each record?
You could use the Formula tool, though I suspect there are numerous other ways:
replace (TrimRight([Name],' '+right([Name],1)),'Doctor','')

Columns into JSON array

I have the following table:
Name
Pets
John
Bird
John
Cow
John
Dog
Nina
Cow
Nina
Fish
Nina
Cat
I would like to output it like so:
Name
Pets
John
["Bird","Cow","Dog"]
Nina
["Cow","Fish","Cat"]
I have this starting point, that converts a single column to JSON.
SELECT JSON_ARRAY(GROUP_CONCAT(column_name SEPARATOR ',')) AS names
FROM table_name;
I'm new to working with arrays and JSON in SQL. Is this possible? What is the best solution?
This approach is already a proper solution for this current case, just need to add GROUP BY expression, and exchange the aliases such as
SELECT name, JSON_ARRAY(GROUP_CONCAT(pets)) AS pets
FROM t
GROUP BY name
where , is the default seperator, then adding that is redundant
Demo
P.S. seems your DB is MySQL (version at least 5.7+) or its extension which's so called MariaDB or SQLite. It's expected to tag the DBMS, and its version, which you're using.

Access Query for 2 tables with similar data

I have two tables that have a column labeled Name:
[CurrentRecords].[Name]
[tbl_vPC].[Name]
However, the CurrentRecords table has more info on each name such as jr, sr, II, III, etc, but the tbl_vPC does not contain that extra information.
Example:
CurrentRecords has: ROBINSON, ROBERT E JR
tbl_vPC has: ROBINSON, ROBERT E
CurrentRecords has: ALLEN, DUG V III
tbl_vPC has: ALLEN, DUG V
Is there a query where I can find all records in the CurrentRecords and all records in tbl_vPC that are similar?
Name is a reserved word. Should not use reserved words as names.
This is why name parts should be in separate fields. Parsing first and last names might not be terribly difficult because of the comma, assuming EVERY record has this convention and EVERY record has value in this field. Try in queries for each table:
x represents Name field
LastName: Left(x, InStr(x, ",")-1)
FirstName: Left(Mid(x, InStr(x,",")+2), InStr(Mid(x, InStr(x,",")+2)," "))

Updating a database column based on its similarity to another database column

I have a database table (Customers) with the following columns:
ID
FIRST_NAME
MIDDLE_INIT
LAST_NAME
FULL_NAME
I also have a database table (ENG) with the following columns:
ID
ENG_NAME
I want to replace all of the ENG.ENG_NAME entries with a FULL_NAME entry from the CUSTOMERS table
Here is the problem.
The ENG_NAME was hand-jammed through a web form and, so, has no consistency. For instance, one row might contain "Robin Hood". Another "Hood, Robin L". An another "Robin L Hood".
I want to search the entries in the CUSTOMERS table, find a close match, then replace the ENG.ENG_NAME with the CUSTOMERS.FULL_NAME.
Example:
ENG table CUSTOMERS table
ID ENG_NAME ID FULL_NAME FIRST_NAME MIDDLE_INIT LAST_NAME
================ ==================================================================
1 Hood,Robin 1 Robin L Hood Robin L Hood
2 Rob Hood 2 Maid M Marion Maid M Marion
3 Marion M 3 Friar F Tuck Friar F Tuck
4 Rob Garza 4 Robert A Garza Robert A Garza
Based on the data above, I would want ENG_NAME columns to be replaced like this:
ENG table
ID ENG_NAME
====================
1 Robin L Hood
2 Robin L Hood
3 Maid M Marion
4 Robert A Garza
Any thoughts on how to do this?
Thanks
This is not going to be a simple task, I would start at finding a good C# (or any .NET) algorithm that detects similar strings portions.
Then look at Compiling C# Code into SQL Stored Procedures and Invoke that code using SQL Server. This CLR Code can then write the results to a table for you to analyze and do whatever you want with it.
For More: CLR SQL Server User-Defined Function
I would do it in .NET using Levenshtein distance.
Start at 1 and you are going to have some ties and you need to decide
Then move to 2,3,4...
You could do in a CLR but how are you going to deal with ties? And you are going to have ties. How are you going to decide when it is not a match at all?
And I would put it in new column so you have a history of original data
Or a FK reference to customers table

Get matching names from SQL Server

My problem is, I have a database of people names and their achievements. Now, I have some paragraphs which contains the person names. I need to extract the names from those paragraphs. The web-end of the application will append a hyperlink of the extracted names with their activities.
Data in my database might look like:
Name | Achievement
----------------------------------------
Steve Jobs | Founder of Apple
Bill Gates | Founder of Microsoft
Now I have string like: After saving up some money, Steve Jobs took off for India in the search of enlightenment.
I need to find Steve Jobs from the above string and add to hyper link to that.
Any idea how to do this?
SQL Server really isn't the best place to do this, because a hyperlink is by definition HTML, but...
declare #s varchar(500) = 'After saving up some money, Steve Jobs took off for India in the search of enlightenment.'
select
#s = REPLACE(#s, name, ''+name+'')
from yourtable
where #s like '%'+name+'%'
select #s
You should save your data in some pre defined format for example, you can use *** at the start & end of person name like.
After saving up some money, ***Steve Jobs*** took off for India in the search of enlightenment.
Then you can extract the Person Names by finding *** through sql
EDIT:
I myself will try to find some other way to do it. For example i will save the Person Names in separate columns.