Please Give Me the Query For Below Mentioned Resultant Tables.

  • 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.

  • 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!

  • Thanks for replying Boss for my question....

    Its working fine thanks a lot.

  • 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:-)

  • 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!

  • 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

  • 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

  • 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![/I]

    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

  • 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