September 1, 2015 at 6:57 am
So, I have a table "table a"
This has two columns. "username" and "companyid"
A sample of data for "companyid" is - "1,2,3,4".
How would I go about join from "table a" to "table b" which holds the company description?
I have only ever joined to a table if companyid in table a has one entry, not multiple.
September 1, 2015 at 7:23 am
TSQL Tryer (9/1/2015)
So, I have a table "table a"This has two columns. "username" and "companyid"
A sample of data for "companyid" is - "1,2,3,4".
How would I go about join from "table a" to "table b" which holds the company description?
I have only ever joined to a table if companyid in table a has one entry, not multiple.
SELECT ...
FROM tablea a
INNER JOIN tableb b
ON b.companyid = a.companyid
For fast, accurate and documented assistance in answering your questions, please read this article.
Understanding and using APPLY, (I) and (II) Paul White
Hidden RBAR: Triangular Joins / The "Numbers" or "Tally" Table: What it is and how it replaces a loop Jeff Moden
September 1, 2015 at 7:25 am
You shouldn't have multiple CompanyID's stored this way. You'll want to normalize your data. You can't do joins that way unless the actual ID in tableB was "1,2,3,4".
September 1, 2015 at 7:31 am
I've not designed the database or the table. I'm simply trying to get a report out of it
September 1, 2015 at 7:37 am
yb751 (9/1/2015)
You shouldn't have multiple CompanyID's stored this way. You'll want to normalize your data. You can't do joins that way unless the actual ID in tableB was "1,2,3,4".
Good catch yb.
You will need to split the string up, resolve those ID's. Use the tried and tested "House Function":
SELECT ...
FROM tablea a
CROSS APPLY dbo.DelimitedSplit8k (a.CompanyID, ',') split
INNER JOIN tableb b
ON b.companyid = split.item
http://www.sqlservercentral.com/articles/Tally+Table/72993/[/url]
For fast, accurate and documented assistance in answering your questions, please read this article.
Understanding and using APPLY, (I) and (II) Paul White
Hidden RBAR: Triangular Joins / The "Numbers" or "Tally" Table: What it is and how it replaces a loop Jeff Moden
September 1, 2015 at 7:38 am
TSQL Tryer (9/1/2015)
I've not designed the database or the table. I'm simply trying to get a report out of it
In that case I suggest breaking out tableA in a temp table before joining it on tableB.
Here is an excellent splitter you could use.
http://www.sqlservercentral.com/articles/Tally+Table/72993/
September 1, 2015 at 7:42 am
ChrisM@Work (9/1/2015)
yb751 (9/1/2015)
You shouldn't have multiple CompanyID's stored this way. You'll want to normalize your data. You can't do joins that way unless the actual ID in tableB was "1,2,3,4".Good catch yb.
You will need to split the string up, resolve those ID's. Use the tried and tested "House Function":
SELECT ...
FROM tablea a
CROSS APPLY dbo.DelimitedSplit8k (a.CompanyID, ',') split
INNER JOIN tableb b
ON b.companyid = split.item
http://www.sqlservercentral.com/articles/Tally+Table/72993/[/url]
NP...I'm the worse for missing that kind of stuff usually. 😉
Noticed you beat me to the Split8K sermon. LOL At least the OP should have everything he/she needs now.
Viewing 7 posts - 1 through 6 (of 6 total)
You must be logged in to reply to this topic. Login to reply