July 20, 2012 at 5:46 am
Hi All,
I have two tables for example,
tblCategories(CategoryID[INT],CategoryName[VARCHAR])
tblEmployees(EmployeeID[INT],EmployeeName[VARCHAR],CategoryIDs[VARCHAR])
Example OUTPUT Like,
tblCategories:
CategoryID CategoryName
1 AAA
2 BBB
3 CCC
tblEmployees:
EmployeeID EmployeeName CategoryIDs
1 XXX 1,3
2 YYY 2,3
3 ZZZ 1,2
I want to join both the tables & i want to get the result
is it possible?if it is possible how to join the table
Please give me the query
& also result for me.
Thanks In Advance,
Venkatesh.
July 20, 2012 at 6:11 am
nice first post, you provided quite a bit of info.
my first point, is in the future, if you can provide your data as CREATE TABLE / INSERT INTO statements like this, anyone can easily see your data and provide test scripts for you:
drop table tblEmployees
drop table tblCategories
CREATE TABLE tblCategories(CategoryID[INT],CategoryName[VARCHAR](30))
CREATE TABLE tblEmployees(EmployeeID[INT],EmployeeName[VARCHAR](30),CategoryIDs[VARCHAR](30))
INSERT INTO tblCategories( CategoryID,CategoryName)
SELECT '1','AAA' UNION ALL
SELECT '2','BBB' UNION ALL
SELECT '3','CCC'
INSERT INTO tblEmployees(EmployeeID,EmployeeName,CategoryIDs)
SELECT '1','XXX','1,3' UNION ALL
SELECT '2','YYY','2,3' UNION ALL
SELECT '3','ZZZ','1,2'
Next, you want to avoid storing multiple values in a single column, otherwise you run into problems like you are having now.
instead of having '1,2' as a varchar field in a row, you should have two rows in the table, both with foreign keys to the category instead.
a work around for that is to use the DelimitedSpilt8K splitter function here on SSC:
http://www.sqlservercentral.com/articles/Tally+Table/72993/
with that function, you can split that comma-delimited list into seperate rows (like they should have been)
SELECT * FROM tblEmployees
cross apply master.dbo.DelimitedSplit8K(CategoryIDs,',')
then using those results as a sub query, you could join them on category:
select * from tblCategories
LEFT OUTER JOIN ( SELECT * FROM tblEmployees
cross apply master.dbo.DelimitedSplit8K(CategoryIDs,',')
) MyAlias
On tblCategories.CategoryID = MyAlias.Item
hope that helps get you started.
Lowell
July 23, 2012 at 12:49 am
Thanks for replying Boss for my question....
Its working fine thanks a lot.
September 5, 2012 at 4:43 am
we can do without using master.dbo.DelimitedSplit8K()
SELECT A.employeeid,A.employeename,B.categoryname,C.categoryname FROM
(
SELECT employeeid,employeename,
SUBSTRING(categoryids,1,CHARINDEX(',',categoryids)-1) categoryid1,
SUBSTRING(categoryids,CHARINDEX(',',categoryids)+1,LEN(categoryids)) categoryid2
FROM
tblemployees
)A
INNER JOIN
tblcategories B ON A.categoryid1=B.categoryid
INNER JOIN
tblcategories C ON A.categoryid2=C.categoryid
i hope this will be useful to someone:-)
September 5, 2012 at 5:16 am
swathi that's a great way to show an alternate solution.
The only thing that bothers me is that solution only works if there are only 9 categories, 1-9, since you are assuming a single digit category Id;
As soon as he has more categories than that, he'll need to rewrite it.
Lowell
September 6, 2012 at 1:00 am
Hi Lowell
thank you
I am quite new to SQL SERVER.I didn't understand your solution for this. my question is where can i find delimitedsplit8k()
function?and how can i use it? could you please explain it
Thanks& Regards
Swathi
September 6, 2012 at 6:14 am
Can I use below function instead of DelimitedSplit8K() function
CREATE FUNCTION SPLIT
(
@RowData nvarchar(2000),
@SplitOn nvarchar(5)
)
RETURNS @RtnValue table
(
Id int identity(1,1),
Data nvarchar(100)
)
AS
BEGIN
Declare @Cnt int
Set @Cnt = 1
While (Charindex(@SplitOn,@RowData)>0)
Begin
Insert Into @RtnValue (data)
Select
Data = ltrim(rtrim(Substring(@RowData,1,Charindex(@SplitOn,@RowData)-1)))
Set @RowData = Substring(@RowData,Charindex(@SplitOn,@RowData)+1,len(@RowData))
Set @Cnt = @Cnt + 1
End
Insert Into @RtnValue (data)
Select Data = ltrim(rtrim(@RowData))
Return
END
and required query is
select * from tblCategories
LEFT JOIN ( SELECT * FROM tblEmployees
cross apply dbo.SPLIT(CategoryIDs,',')
) A
On tblCategories.CategoryID = A.DATA
September 6, 2012 at 11:38 pm
swathi nareddy (9/6/2012)
Can I use below function instead of DelimitedSplit8K() functionCREATE FUNCTION SPLIT
(
@RowData nvarchar(2000),
@SplitOn nvarchar(5)
)
RETURNS @RtnValue table
(
Id int identity(1,1),
Data nvarchar(100)
)
AS
BEGIN
Declare @Cnt int
Set @Cnt = 1
While (Charindex(@SplitOn,@RowData)>0)
Begin
Insert Into @RtnValue (data)
Select
Data = ltrim(rtrim(Substring(@RowData,1,Charindex(@SplitOn,@RowData)-1)))
Set @RowData = Substring(@RowData,Charindex(@SplitOn,@RowData)+1,len(@RowData))
Set @Cnt = @Cnt + 1
End
Insert Into @RtnValue (data)
Select Data = ltrim(rtrim(@RowData))
Return
END
and required query is
select * from tblCategories
LEFT JOIN ( SELECT * FROM tblEmployees
cross apply dbo.SPLIT(CategoryIDs,',')
) A
On tblCategories.CategoryID = A.DATA
Yes, but only if poor performance is a requirement of your application.
My thought question: Have you ever been told that your query runs too fast?
My advice:
INDEXing a poor-performing query is like putting sugar on cat food. Yeah, it probably tastes better but are you sure you want to eat it?
The path of least resistance can be a slippery slope. Take care that fixing your fixes of fixes doesn't snowball and end up costing you more than fixing the root cause would have in the first place.
Need to UNPIVOT? Why not CROSS APPLY VALUES instead?[/url]
Since random numbers are too important to be left to chance, let's generate some![/url]
Learn to understand recursive CTEs by example.[/url]
[url url=http://www.sqlservercentral.com/articles/St
September 21, 2012 at 12:32 am
We can also use comma separted list to a table instead of DelimitedSplit8K function.Both function would produce same results
CREATE FUNCTION [dbo].[SplitList]
(
@List nvarchar(max),
@SplitOn nvarchar(5)
)
RETURNS @RtnValue table
(
Id int identity(1,1),
Value nvarchar(100)
)
AS
BEGIN
While (Charindex(@SplitOn,@List)>0)
Begin
Insert Into @RtnValue (value)
Select
Value = ltrim(rtrim(Substring(@List,1,Charindex(@SplitOn,@List)-1)))
Set @List = Substring(@List,Charindex(@SplitOn,@List)+len(@SplitOn),len(@List))
End
Insert Into @RtnValue (Value)
Select Value = ltrim(rtrim(@List))
Return
END
Viewing 9 posts - 1 through 8 (of 8 total)
You must be logged in to reply to this topic. Login to reply