Click here to monitor SSC
SQLServerCentral is supported by Red Gate Software Ltd.
 
Log in  ::  Register  ::  Not logged in
 
 
 
        
Home       Members    Calendar    Who's On


Add to briefcase

Please Give Me the Query For Below Mentioned Resultant Tables. Expand / Collapse
Author
Message
Posted Friday, July 20, 2012 5:46 AM
SSC Rookie

SSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC Rookie

Group: General Forum Members
Last Login: Wednesday, October 23, 2013 3:02 AM
Points: 25, Visits: 72
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.
Post #1332892
Posted Friday, July 20, 2012 6:11 AM


SSChampion

SSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampion

Group: General Forum Members
Last Login: Yesterday @ 2:08 PM
Points: 12,903, Visits: 31,972
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

--There is no spoon, and there's no default ORDER BY in sql server either.
Actually, Common Sense is so rare, it should be considered a Superpower. --my son
Post #1332907
Posted Monday, July 23, 2012 12:49 AM
SSC Rookie

SSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC Rookie

Group: General Forum Members
Last Login: Wednesday, October 23, 2013 3:02 AM
Points: 25, Visits: 72
Thanks for replying Boss for my question....
Its working fine thanks a lot.
Post #1333612
Posted Wednesday, September 5, 2012 4:43 AM
Grasshopper

GrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopper

Group: General Forum Members
Last Login: Sunday, December 16, 2012 9:28 PM
Points: 15, Visits: 167
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
Post #1354430
Posted Wednesday, September 5, 2012 5:16 AM


SSChampion

SSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampion

Group: General Forum Members
Last Login: Yesterday @ 2:08 PM
Points: 12,903, Visits: 31,972
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

--There is no spoon, and there's no default ORDER BY in sql server either.
Actually, Common Sense is so rare, it should be considered a Superpower. --my son
Post #1354437
Posted Thursday, September 6, 2012 1:00 AM
Grasshopper

GrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopper

Group: General Forum Members
Last Login: Sunday, December 16, 2012 9:28 PM
Points: 15, Visits: 167
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
Post #1355065
Posted Thursday, September 6, 2012 6:14 AM
Grasshopper

GrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopper

Group: General Forum Members
Last Login: Sunday, December 16, 2012 9:28 PM
Points: 15, Visits: 167
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
Post #1355251
Posted Thursday, September 6, 2012 11:38 PM


Hall of Fame

Hall of FameHall of FameHall of FameHall of FameHall of FameHall of FameHall of FameHall of FameHall of Fame

Group: General Forum Members
Last Login: Yesterday @ 4:14 AM
Points: 3,618, Visits: 5,254
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!
Post #1355750
Posted Friday, September 21, 2012 12:32 AM
Grasshopper

GrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopper

Group: General Forum Members
Last Login: Sunday, December 2, 2012 6:21 AM
Points: 16, 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
Post #1362444
« Prev Topic | Next Topic »

Add to briefcase

Permissions Expand / Collapse