I have a scenario like this in ssis - I have got two different inputs, out of which one is coming from a lookup nomatch output. The other input is actually a derived column that is takn from a single row single column result in an oledb source using sql query. The problem is that I need to join these two inputs and make it a single dataset to further push the data to the crm destination(Cozyroc).
I know union all just cant do the work since it works on union of rows from different datasets.
Also merge and merge join cannot be used since a common id or key need to be given inorder to join the two datasets, and i have got no such thing.
For example, my first dataset looks like:
usinessid userid name
--------- ------ ----
ret678 435 john
dfgt67 213 sam
and my second dataset is like:
systemid
------------------------
6666-777-kjtyr-213t-ytui
which is extracted using a single column single row query using the oledb source - sql command.
Is there a way to combine these two dataset so that the end result will be something like:
businessid userid name systemid
---------- ------ ---- ------------------------
ret678 435 john 6666-777-kjtyr-213t-ytui
dfgt67 213 sam 6666-777-kjtyr-213t-ytui
I want to do this without using a variable or using a derived column and hardcoding the systemid value.
Pardon my editing...
Any further inputs on this issue will be really helpful.
To combine the two datasets the way you've shown, you could use a simple cross join:
SELECT
t1.businessid,
t1.userid,
t1.name,
t2.systemid
FROM
table1 t1
CROSS JOIN
table2 t2
;
Related
i need to figure out how i can i accomplish this task that was given to me, you see, i have imported an Excel, cleaned out the information and used this information to start joining the tables i need to, when i started i realized i needed to make it very precisely so i needed the id of the data i'm using which doesn't come in this Excel document i imported (since the id are stored in the database and the Excel was built by other people who don't handle databases) so i have a workmate whom i asked about how to do this task, he told me to do the inner join on the columns in common, but the way i did it appeared an error and logically didn't work, therefore i thought extracting the id from the table they are stored would be a good idea (well maybe not) but i don't know how to do it nor if it Will work, i'll give you some examples of how the tables would look like:
table 1
----------------------
|ID|column_a|column_b|
|1 |2234 |3 |
|2 |41245 |23 |
|3 |442 |434 |
|4 |1243 |1 |
----------------------
table 2
---------------------------------
|creation_date|column_a|column_b|
|1/12/2018 |2234 |3 |
|4/31/2011 |41245 |23 |
|7/22/2014 |442 |434 |
|10/14/2017 |1243 |1 |
---------------------------------
as you can see, the values of the columns a and b match perfectly, so there could be a bridge between the two tables, i tried to join the data by the column a but did not work since the output was much larger that i should, i also tried doing a simple query with an IN statement but did not work either since i brought up nearly the entire databases duplicated (i'm working with big databases the table 1 contains nearly 35.000 rows and the table 2 contains nearly 10.000) extracting the ids ad if they were row files won't work since they are very different from what is in the id tables in the actual table i'm working with, so what do you think it would be the best way to achieve this task? any kind of help i would be grateful, thanks in advance.
EDIT
Based on the answer of R3_ i tried his query but adapted to my needs and worked in some cases but in others i got the cartesian product, the example i'm using is that i have in table 2 in column_a the number 1000 and column_b has number 1, table 1 has 10 ids for that combination of numbers since the 1000-1 number is not the same (technically it is, but it has stored different information and is usually differenced by the ID) so the output is either 10 rows (assuming that it is only picking those with id) or 450 not the 45 i need as result, the query i'm using is like this:
SELECT DISTINCT table_1.id, table_2.column_a, table_2.column_b --if i pick the columns from table 1 returns 10 rows if i pick them from table 2 it returns 450
FROM table_2
INNER JOIN table_1 ON table_2.column_a = table_1.column_a AND table_1.column_b = table_2.column_b
WHERE table_2.column_a = 1022 AND table_2.column_b = 1
so the big deal has to do with the 10 id that has that 1000-1 combination so the sql doesn't know how to identify where the id should go, how can i do to obtain those 45 i need?
also i figured out that if i do the general query, there are some rows missing, here is how i print it:
SELECT table_1.id, table_1.column_a, table_1.column_b
FROM table_2 --in this case i try switching the columns i return from table 1 or 2
INNER JOIN table_1 ON table_2.column_a = table_1.column_a AND table_2.column_b = table_1.column_b
the output of the latter example is 2666 rows and should be 2733, what am i doing wrong?
SELECT DISTINCT -- Adding DISTINCT clause for unique pairs of ID and creation_date
ID, tab1.column_a, tab1.column_b, creation_date
FROM [table 1] as tab1
LEFT JOIN [table 2] as tab2 -- OR INNER JOIN
ON tab1.column_a = tab2.column_a
AND tab1.column_b = tab2.column_b
-- WHERE ID IN ('01', '02') -- Filtering by desired ID
I have been searching for this answer for quite sometime and so far, have not found a solution. I am creating a Data Flow Task in SSIS, and I need to join 2 tables on different data sources using the LIKE operator, or FINDSTRING, or CONTAINS etc.
Here is what my data looks like:
Table1 :: Data Source 1
| PersonName | Address |
Josh LA, California US
Ted SF, California US
Beth NYC, New York US
Table2 :: Data Source 2
| StateID | StateName |
01 California
02 New York
I need to join Table2 on Table1 where Table1.Address LIKE '%' + Table2.StateName '%'.
My desired end result is this:
| PersonName | StateID |
Josh 01
Ted 01
Beth 02
I am doing this in SSIS, so Table1 and Table2 are gathered using an OLE DB Source component, now I need to find a way to perform a non-exact merge on those two tables.
I cannot use a script component
I have tried using a "Lookup Transformation" where Table1 is the input to the Lookup. Then go to -> Advanced -> "Modify the SQL statement", and I have tried 2 things that have not worked:
1)
select * from (select * from [dbo].[Table2]) [refTable]
where ? LIKE '%' + [refTable].[StateName] + '%'
2)
select * from (select * from [dbo].[Table2]) [refTable]
where FINDSTRING( ?, [refTable].[StateName], 1) > 0
Is there any way to achieve what I am looking for in SSIS without using a script component?
Maybe there is a way to use a fuzzy lookup? If so, how do I configure that? Is there like an advanced "Merge Join" object?
You are on the right track with your edited Lookup 1). I think you just need to remove the derived table e.g.
select * from from [dbo].[Table2]
where ? LIKE '%' + [StateName] + '%'
you cannot join two tables in different data-sources using a query in the Lookup- Transformation.
The query is used to define the data-source to be cached and looked upon and it is run directly on the defined data source for the Lookup Transformation. For eg if you want to lookup only states with state_id greater then 10 you define the query as
Select * from [dbo].[Table2] where StateID > 10
The Best way to implement this would be to have Table1 as your OLEDB Source and have script task or a derived column task that extracts the Statement from the column Address. Use Regex if you go with a script task. This new derived column would have the state alone.
After this we should have a Look-up Transformation which has based on Table2. The Lookup Condition would be on Statename in Table2 and Derived State column in Table1.
Another Option is to have a Merge-Join transformation which can be used to join data coming from disparate data sources.
Sorry if this is a basic question. I'm fairly new to SQL, so I guess I'm just missing the name of the concept to search for.
Quick overview.
First table (items):
ID | name
-------------
1 | abc
2 | def
3 | ghi
4 | jkl
Second table (pairs):
ID | FirstMember | SecondMember Virtual column (pair name)
-------------------------------------
1 | 2 | 3 defghi
2 | 1 | 4 abcjkl
I'm trying to build the virtual column shown in the second table
It could be built at the time any entry is made in the second table, but if done that way, the data in that column would get wrong any time one of the items in the first table is renamed.
I also understand that I can build that column any time I need it (in either plain requests or stored procedures), but that would lead to code duplication, since the second table can be involved in multiple different requests.
So is there a way to define a "virtual" column, that could be accessed as a normal column, but whose content is built dynamically?
Thanks.
Edit: this is on MsSql 2008, but an engine-agnostic solution would be preferred.
Edit: the example above was oversimplified in multiple ways - the major one being that the virtual column content isn't a straight concatenation of both names, but something more complex, depending on the content of columns I didn't described. Still, you've provided multiple paths that seems promising - I'll be back. Thanks.
You need to join the items table twice:
select p.id,
p.firstMember,
p.secondMember,
i1.name||i2.name as pair_name
from pairs as p
join items as i1 on p.FirstMember = i1.id
join items as i2 on p.SecondMember = i2.id;
Then put this into a view and you have your "virtual column". You would simply query the view instead of the actual pairs table wherever you need the pair_name column.
Note that the above uses inner joins, if your "FirstMember" and "SecondMember" columns might be null, you probably want to use an outer join instead.
You can use a view, which creates a table-like object from a query result, such as the one with a_horse_with_no_name provided.
CREATE VIEW pair_names AS
SELECT p.id,
p.firstMember,
p.secondMember,
CONCAT(i1.name, i2.name) AS pair_name
FROM pairs AS p
JOIN items AS i1 ON p.FirstMember = i1.id
JOIN items AS i2 ON p.SecondMember = i2.id;
Then to query the results just do:
SELECT id, pair_name FROM pair_names;
You could create a view for your 'virtual column', if you wanted to, like so:
CREATE VIEW aView AS
SELECT
p.ID,
p.FirstMember,
p.SecondMember,
a.name + b.name as 'PairName'
FROM
pairs p
LEFT JOIN
items a
ON
p.FirstMember = a.ID
LEFT JOIN
items b
ON
p.SecondMember = b.ID
Edit:
Or, of course, you could just use a similar select statement every time.
When selecting from tables you can name the results of a column using AS.
SELECT st.ID, st.FirstMember, st.SecondMember, ft1.Name + ft2.Name AS PairName
FROM Second_Table st
JOIN First_Table ft1 ON st.FirstMember = ft1.ID
JOIN First_Table ft2 ON st.SecondMember = ft2.ID
Should give you something like what you are after.
I'm new to SQL. I have a simple problem with getting the results from two different tables.
I have two tables in a database. The first table has a column with an id reference, which corresponds to rows in the second table. What SELECT do I need to perform to get a result such that the ids are repalced by all of the values in the second table. To visualize the tables I am discussing:
TABLE_USERS
===========
id username group
-- -------- -----
1 jim A
2 alice A
3 brandon B
TABLE_GROUPS
============
id groupname members
-- --------- -------
A designer 134
B photographer 39
DESIRED_SELECTION
=================
id username group
-- -------- -----
1 jim designer
2 alice designer
3 brandon photographer
Thanks!
You do, in fact, want to JOIN the two tables:
SELECT * FROM
TABLE_USERS LEFT JOIN TABLE_GROUPS
ON TABLE_USERS.group = TABLE_GROUPS.id
The trick of joining tables is to find the values that must match in the two tables, and use the on to tell SQL to match them. This table has a ID column to let you do that = you will join the table, ON, and then list the values that need to be equal.
If you do not want all of the columns in both tables, you can simply list only the columns you need in your final query. This means that instead of Select *, you list the columns you want. As shown below, if a column appears with the same name in both tables, you need to prepend the table name, so that SQL know which value you want.
SELECT TABLE_USERS.ID, Username, Groupname
FROM TABLE_USERS
LEFT JOIN TABLE_GROUPS
ON TABLE_USERS.group = TABLE_GROUPS.id
You want a JOIN:
SELECT
u.id,
username,
groupname
FROM
TABLE_USERS AS u
LEFT JOIN TABLE_GROUPS AS g
ON u.group = g.id
I have a TWO tables of data with following fields
table1=(ITTAG,ITCODE,ITDESC,SUPcode)
table2=(ACCODE,ACNAME,ROUTE,SALMAN)
This is my customer master table that contains my customer data such as customer code, customer name and so on...
Every Route has a supervisor (table1=supcode) and I need to know the supervisor name in my table which both supervisor name and code exist in one table.
table1 has contain all names separated by ITTAG. For example, supervisor's name has ITTAG='K'; also salesman's name has ITTAG='S'.
ITTAG ITCODE ITDESC SUPCODE
------ ------ ------ -------
S JT JOHN TOMAS TF
K WK VIKI KOO NULL
Now this is the result which I want
ACCODE ACNAME ROUTE SALEMANNAME SUPERVISORNAME
------- ------ ------ ------------ ---------------
IMC1010 ABC HOTEL 01 JOHN TOMAS VIKI KOO
I hope this this information is sufficient to get the query..
Your data structure is either not clear or incomplete. It would help if you showed the actual example data for Table1 too, but there would be trouble.
SELECT t2.ACCODE, t2.ACNAME, t2.ROUTE, a1.ITDESC AS Salesman, a2.ITDESC AS Supervisor
FROM table1 AS t1
JOIN table2 AS a1 ON t1.SALMAN = a1.ITCODE
JOIN table2 AS a2 ON t1.?????? = a2.SUPCODE
It is not clear whether I've managed the join between Table1 and Table2 for the salesman information correctly; it is plausible, but the join for the supervisor should be similar, and yet there isn't a way to make that work. Hence the '??????' in the query.
The basic technique for joining twice to a single table is to cite it twice with different aliases, as shown. I usually use one letter or a letter and a digit for the aliases, as shown.