complex query

  • create table #tab1( empno int, mark1 int, rank1 int)

    insert into #tab1 values (1,50,2)

    insert into #tab1 values (1,60,3)

    insert into #tab1 values (1,70,4)

    insert into #tab1 values (2,10,1)

    insert into #tab1 values (2,20,2)

    desired output is

    empno mark1 rank1

    1 | 50,60,70 | 2,3

    2 | 10,20 | 1,2

    can tht be acheived?.. any suggestion pls?

    Thanks in Advance

  • Once you've read that article, this code should make sense:

    SELECT CONVERT(VARCHAR(12), Emp.empno) + ' | ' +

    STUFF(CA1.marks.value('./text()[1]', 'varchar(max)'), 1, 1, '') + ' | ' +

    STUFF(CA2.ranks.value('./text()[1]', 'varchar(max)'), 1, 1, '')

    FROM (

    SELECT DISTINCT T1.empno

    FROM #tab1 AS T1

    ) AS Emp

    CROSS

    APPLY (

    SELECT ',' + CONVERT(VARCHAR(12), T2.mark1)

    FROM #tab1 AS T2

    WHERE T2.empno = Emp.empno

    ORDER BY

    T2.empno

    FOR XML PATH(''), TYPE

    ) AS CA1 (marks)

    CROSS

    APPLY (

    SELECT ',' + CONVERT(VARCHAR(12), T3.rank1)

    FROM #tab1 AS T3

    WHERE T3.empno = Emp.empno

    ORDER BY

    T3.empno

    FOR XML PATH(''), TYPE

    ) AS CA2 (ranks)

    ;

  • Really Nice Solution given at the link http://www.sqlservercentral.com/articles/comma+separated+list/71700/

    WITH CTE AS

    (

    SELECT DISTINCT

    empno

    FROM #tab1

    )

    SELECT empno,

    mark1 = STUFF((SELECT ',' + convert(char(50),mark1)

    FROM #tab1

    WHERE empno = CTE.empno

    ORDER BY mark1

    FOR XML PATH(''),

    TYPE).value('.','varchar(max)'),1,1,''),

    rank1 = STUFF((SELECT ',' + convert(char(50),rank1)

    FROM #tab1

    WHERE empno = CTE.empno

    ORDER BY rank1

    FOR XML PATH(''),

    TYPE).value('.','varchar(max)'),1,1,'')

    FROM CTE

    ORDER BY empno;

  • ramarkishna (3/4/2011)


    Really Nice Solution

    That's almost there, but the output isn't quite right (multiple columns and huge blank spaces). Also the performance will not be as good as the solution already posted.

  • SQLkiwi (3/4/2011)


    Once you've read that article, this code should make sense:

    SELECT CONVERT(VARCHAR(12), Emp.empno) + ' | ' +

    STUFF(CA1.marks.value('./text()[1]', 'varchar(max)'), 1, 1, '') + ' | ' +

    STUFF(CA2.ranks.value('./text()[1]', 'varchar(max)'), 1, 1, '')

    FROM (

    SELECT DISTINCT T1.empno

    FROM #tab1 AS T1

    ) AS Emp

    CROSS

    APPLY (

    SELECT ',' + CONVERT(VARCHAR(12), T2.mark1)

    FROM #tab1 AS T2

    WHERE T2.empno = Emp.empno

    ORDER BY

    T2.empno

    FOR XML PATH(''), TYPE

    ) AS CA1 (marks)

    CROSS

    APPLY (

    SELECT ',' + CONVERT(VARCHAR(12), T3.rank1)

    FROM #tab1 AS T3

    WHERE T3.empno = Emp.empno

    ORDER BY

    T3.empno

    FOR XML PATH(''), TYPE

    ) AS CA2 (ranks)

    ;

    Thanks a lot.... but here #tab1 will be already derived from CTE.... so it will take more table scan... u suggest me to go with this?

  • MonsterRocks (3/4/2011)


    Thanks a lot.... but here #tab1 will be already derived from CTE.... so it will take more table scan... u suggest me to go with this?

    It depends on how expensive it is to run the CTE more than once. If the output from the CTE is relatively small, you could consider storing it in a temporary table beforehand. My post was to aid you in understanding how to apply the technique mentioned in the article - not necessarily as a cut-and-paste solution to your real problem.

  • i am not fond of copy and paste... just want to a better way to achieve it... since i m a beginner i dont knw how efficient to use more than one CROSS APPLY along with a CTE..thats y i came to you experienced people....Guidance and idea i expect.... thanks a lot for ur effort and valuable ideas

  • Thanks for the plug Dave.

    Wayne
    Microsoft Certified Master: SQL Server 2008
    Author - SQL Server T-SQL Recipes


    If you can't explain to another person how the code that you're copying from the internet works, then DON'T USE IT on a production system! After all, you will be the one supporting it!
    Links:
    For better assistance in answering your questions
    Performance Problems
    Common date/time routines
    Understanding and Using APPLY Part 1 & Part 2

  • SQLkiwi (3/4/2011)


    Once you've read that article, this code should make sense:

    SELECT CONVERT(VARCHAR(12), Emp.empno) + ' | ' +

    STUFF(CA1.marks.value('./text()[1]', 'varchar(max)'), 1, 1, '') + ' | ' +

    STUFF(CA2.ranks.value('./text()[1]', 'varchar(max)'), 1, 1, '')

    FROM (

    SELECT DISTINCT T1.empno

    FROM #tab1 AS T1

    ) AS Emp

    CROSS

    APPLY (

    SELECT ',' + CONVERT(VARCHAR(12), T2.mark1)

    FROM #tab1 AS T2

    WHERE T2.empno = Emp.empno

    ORDER BY

    T2.empno

    FOR XML PATH(''), TYPE

    ) AS CA1 (marks)

    CROSS

    APPLY (

    SELECT ',' + CONVERT(VARCHAR(12), T3.rank1)

    FROM #tab1 AS T3

    WHERE T3.empno = Emp.empno

    ORDER BY

    T3.empno

    FOR XML PATH(''), TYPE

    ) AS CA2 (ranks)

    ;

    Hmm, I should have thought of using CROSS APPLY in that article. Thanks for enlightening us (yet again)!

    Wayne
    Microsoft Certified Master: SQL Server 2008
    Author - SQL Server T-SQL Recipes


    If you can't explain to another person how the code that you're copying from the internet works, then DON'T USE IT on a production system! After all, you will be the one supporting it!
    Links:
    For better assistance in answering your questions
    Performance Problems
    Common date/time routines
    Understanding and Using APPLY Part 1 & Part 2

Viewing 10 posts - 1 through 9 (of 9 total)

You must be logged in to reply to this topic. Login to reply