SQL Clone
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


Please Give Me the Query For Below Mentioned Resultant Tables.


Please Give Me the Query For Below Mentioned Resultant Tables.

Author
Message
venkidesaik
venkidesaik
SSC Journeyman
SSC Journeyman (85 reputation)SSC Journeyman (85 reputation)SSC Journeyman (85 reputation)SSC Journeyman (85 reputation)SSC Journeyman (85 reputation)SSC Journeyman (85 reputation)SSC Journeyman (85 reputation)SSC Journeyman (85 reputation)

Group: General Forum Members
Points: 85 Visits: 73
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.
Lowell
Lowell
SSC Guru
SSC Guru (71K reputation)SSC Guru (71K reputation)SSC Guru (71K reputation)SSC Guru (71K reputation)SSC Guru (71K reputation)SSC Guru (71K reputation)SSC Guru (71K reputation)SSC Guru (71K reputation)

Group: General Forum Members
Points: 71288 Visits: 40930
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
--help us help you! If you post a question, make sure you include a CREATE TABLE... statement and INSERT INTO... statement into that table to give the volunteers here representative data. with your description of the problem, we can provide a tested, verifiable solution to your question! asking the question the right way gets you a tested answer the fastest way possible!
venkidesaik
venkidesaik
SSC Journeyman
SSC Journeyman (85 reputation)SSC Journeyman (85 reputation)SSC Journeyman (85 reputation)SSC Journeyman (85 reputation)SSC Journeyman (85 reputation)SSC Journeyman (85 reputation)SSC Journeyman (85 reputation)SSC Journeyman (85 reputation)

Group: General Forum Members
Points: 85 Visits: 73
Thanks for replying Boss for my question....
Its working fine thanks a lot.
SWATH
SWATH
SSC Rookie
SSC Rookie (45 reputation)SSC Rookie (45 reputation)SSC Rookie (45 reputation)SSC Rookie (45 reputation)SSC Rookie (45 reputation)SSC Rookie (45 reputation)SSC Rookie (45 reputation)SSC Rookie (45 reputation)

Group: General Forum Members
Points: 45 Visits: 171
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:-)
Lowell
Lowell
SSC Guru
SSC Guru (71K reputation)SSC Guru (71K reputation)SSC Guru (71K reputation)SSC Guru (71K reputation)SSC Guru (71K reputation)SSC Guru (71K reputation)SSC Guru (71K reputation)SSC Guru (71K reputation)

Group: General Forum Members
Points: 71288 Visits: 40930
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
--help us help you! If you post a question, make sure you include a CREATE TABLE... statement and INSERT INTO... statement into that table to give the volunteers here representative data. with your description of the problem, we can provide a tested, verifiable solution to your question! asking the question the right way gets you a tested answer the fastest way possible!
SWATH
SWATH
SSC Rookie
SSC Rookie (45 reputation)SSC Rookie (45 reputation)SSC Rookie (45 reputation)SSC Rookie (45 reputation)SSC Rookie (45 reputation)SSC Rookie (45 reputation)SSC Rookie (45 reputation)SSC Rookie (45 reputation)

Group: General Forum Members
Points: 45 Visits: 171
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
SWATH
SWATH
SSC Rookie
SSC Rookie (45 reputation)SSC Rookie (45 reputation)SSC Rookie (45 reputation)SSC Rookie (45 reputation)SSC Rookie (45 reputation)SSC Rookie (45 reputation)SSC Rookie (45 reputation)SSC Rookie (45 reputation)

Group: General Forum Members
Points: 45 Visits: 171
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
dwain.c
dwain.c
SSCoach
SSCoach (17K reputation)SSCoach (17K reputation)SSCoach (17K reputation)SSCoach (17K reputation)SSCoach (17K reputation)SSCoach (17K reputation)SSCoach (17K reputation)SSCoach (17K reputation)

Group: General Forum Members
Points: 17699 Visits: 6431
swathi nareddy (9/6/2012)
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


Yes, but only if poor performance is a requirement of your application.


My mantra: No loops! No CURSORs! No RBAR! Hoo-uh!

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?
Since random numbers are too important to be left to chance, let's generate some!
Learn to understand recursive CTEs by example.
Splitting strings based on patterns can be fast!
My temporal SQL musings: Calendar Tables, an Easter SQL, Time Slots and Self-maintaining, Contiguous Effective Dates in Temporal Tables
saltpepo
saltpepo
Valued Member
Valued Member (60 reputation)Valued Member (60 reputation)Valued Member (60 reputation)Valued Member (60 reputation)Valued Member (60 reputation)Valued Member (60 reputation)Valued Member (60 reputation)Valued Member (60 reputation)

Group: General Forum Members
Points: 60 Visits: 64
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
Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum

































































































































































SQLServerCentral


Search