Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. Database & SysAdmin
  3. Database
  4. SQL server query

SQL server query

Scheduled Pinned Locked Moved Database
databasesql-serversysadminxmlhelp
3 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • S Offline
    S Offline
    Swap9
    wrote on last edited by
    #1

    id_no first_name last_name 1 X ABC 2 Y ABC 3 M pqr 4 N PQR Here is the query I am running : SELECT STUFF((select stuff1 from(SELECT distinct T1.id_no 'sequenceno', ('/' + Col2) + ',' + STUFF((SELECT STUFF2 FROM(SELECT distinct T2.id_no 'sequenceno' ,'-' + substring(first_name, 1, 1)'STUFF2' FROM Table2 AS T2 WHERE T2.last_name = T1.last_name AND T2.id_no = 1234 AND T2.id_no = T1.id_no) dt2 order BY dt2.sequenceno FOR XML PATH(''),TYPE ).value('.', 'varchar(MAX)'), 1, 1, '') 'stuff1' FROM Table1 AS T1) dt order BY dt.sequenceno FOR XML PATH(''),TYPE).value('.', 'VARCHAR(MAX)'), 1, 1, '') AS Result OUTPUT - ABc,X-Y/ABc,X-Y/pqr,M-N/PQR,M-N Desired OUTPUT - ABc,X-Y/pqr,M-N Can you pls help me on this ?

    Richard Andrew x64R R 2 Replies Last reply
    0
    • S Swap9

      id_no first_name last_name 1 X ABC 2 Y ABC 3 M pqr 4 N PQR Here is the query I am running : SELECT STUFF((select stuff1 from(SELECT distinct T1.id_no 'sequenceno', ('/' + Col2) + ',' + STUFF((SELECT STUFF2 FROM(SELECT distinct T2.id_no 'sequenceno' ,'-' + substring(first_name, 1, 1)'STUFF2' FROM Table2 AS T2 WHERE T2.last_name = T1.last_name AND T2.id_no = 1234 AND T2.id_no = T1.id_no) dt2 order BY dt2.sequenceno FOR XML PATH(''),TYPE ).value('.', 'varchar(MAX)'), 1, 1, '') 'stuff1' FROM Table1 AS T1) dt order BY dt.sequenceno FOR XML PATH(''),TYPE).value('.', 'VARCHAR(MAX)'), 1, 1, '') AS Result OUTPUT - ABc,X-Y/ABc,X-Y/pqr,M-N/PQR,M-N Desired OUTPUT - ABc,X-Y/pqr,M-N Can you pls help me on this ?

      Richard Andrew x64R Online
      Richard Andrew x64R Online
      Richard Andrew x64
      wrote on last edited by
      #2

      This is not a good question. The SQL code is mostly unreadable and there is no description of what the desired output should be. The example desired output does not describe the format well enough.

      The difficult we do right away... ...the impossible takes slightly longer.

      1 Reply Last reply
      0
      • S Swap9

        id_no first_name last_name 1 X ABC 2 Y ABC 3 M pqr 4 N PQR Here is the query I am running : SELECT STUFF((select stuff1 from(SELECT distinct T1.id_no 'sequenceno', ('/' + Col2) + ',' + STUFF((SELECT STUFF2 FROM(SELECT distinct T2.id_no 'sequenceno' ,'-' + substring(first_name, 1, 1)'STUFF2' FROM Table2 AS T2 WHERE T2.last_name = T1.last_name AND T2.id_no = 1234 AND T2.id_no = T1.id_no) dt2 order BY dt2.sequenceno FOR XML PATH(''),TYPE ).value('.', 'varchar(MAX)'), 1, 1, '') 'stuff1' FROM Table1 AS T1) dt order BY dt.sequenceno FOR XML PATH(''),TYPE).value('.', 'VARCHAR(MAX)'), 1, 1, '') AS Result OUTPUT - ABc,X-Y/ABc,X-Y/pqr,M-N/PQR,M-N Desired OUTPUT - ABc,X-Y/pqr,M-N Can you pls help me on this ?

        R Offline
        R Offline
        RNA Team
        wrote on last edited by
        #3

        Try this

        DECLARE @T TABLE(ID_NO INT IDENTITY,FIRST_NAME VARCHAR(50), LAST_NAME VARCHAR(50))
        INSERT INTO @T VALUES('X','ABC'),('Y','ABC'),('M','PQR'),('N','PQR')
        --SELECT * FROM @T
        ;WITH CTE AS(
        SELECT
        Rn = ROW_NUMBER() OVER(PARTITION BY t2.LAST_NAME ORDER BY (SELECT 1))
        ,t2.LAST_NAME
        + ',' +
        STUFF(( SELECT '-' + t1.FIRST_NAME
        FROM @T t1
        WHERE t1.LAST_NAME = t2.LAST_NAME
        FOR XML PATH(''),TYPE) .value('.','NVARCHAR(MAX)'),1,1,'')AS Concat_FirstName
        FROM @T t2
        )
        SELECT
        STUFF(( SELECT '/' + c1.Concat_FirstName
        FROM CTE c1
        WHERE c1.Rn = c2.Rn
        FOR XML PATH(''),TYPE)
        .value('.','NVARCHAR(MAX)'),1,1,'') AS Concat_FirstName
        FROM CTE c2 WHERE Rn= 1
        GROUP BY c2.Rn

        Result Concat_FirstName ABC,X-Y/PQR,M-N Hope this helps

        1 Reply Last reply
        0
        Reply
        • Reply as topic
        Log in to reply
        • Oldest to Newest
        • Newest to Oldest
        • Most Votes


        • Login

        • Don't have an account? Register

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • World
        • Users
        • Groups