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 Query

SQl Query

Scheduled Pinned Locked Moved Database
database
10 Posts 5 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.
  • A Offline
    A Offline
    Abdul Rahman Hamidy
    wrote on last edited by
    #1

    Dear All, I am looking to query followings in Sql 2005 table as Table A ID Name 1 A 1 B 1 C 2 D 2 E 2 F I want the result as ID Name 1 A,B,C 2 D,E,F any ideas

    Abdul Rahaman Hamidy Database Developer Kabul, Afghanistan

    B P A 3 Replies Last reply
    0
    • A Abdul Rahman Hamidy

      Dear All, I am looking to query followings in Sql 2005 table as Table A ID Name 1 A 1 B 1 C 2 D 2 E 2 F I want the result as ID Name 1 A,B,C 2 D,E,F any ideas

      Abdul Rahaman Hamidy Database Developer Kabul, Afghanistan

      B Offline
      B Offline
      Blue_Boy
      wrote on last edited by
      #2

      You have to use cursor. check this.[^]


      I Love T-SQL "Don't torture yourself,let the life to do it for you." If my post helps you kindly save my time by voting my post. www.aktualiteti.com

      A 1 Reply Last reply
      0
      • A Abdul Rahman Hamidy

        Dear All, I am looking to query followings in Sql 2005 table as Table A ID Name 1 A 1 B 1 C 2 D 2 E 2 F I want the result as ID Name 1 A,B,C 2 D,E,F any ideas

        Abdul Rahaman Hamidy Database Developer Kabul, Afghanistan

        P Offline
        P Offline
        Pranay Rana
        wrote on last edited by
        #3

        Following one of the solution work for you but there must be better solution than this SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO -- ============================================= -- Author: <Author,,Name> -- Create date: <Create Date, ,> -- Description: <Description, ,> -- ============================================= CREATE FUNCTION tableaValue ( @id int ) RETURNS varchar(50) AS BEGIN declare @name varchar(50) declare @tempString varchar(50) DECLARE tablea_cursor CURSOR FOR select name from tablea where id = @id -- Variable value from the outer cursor OPEN tablea_cursor FETCH NEXT FROM tablea_cursor INTO @name WHILE @@FETCH_STATUS = 0 BEGIN SELECT @tempString= isnull(@tempString,'') + ',' + @name FETCH NEXT FROM tablea_cursor INTO @name END CLOSE tablea_cursor DEALLOCATE tablea_cursor -- Return the result of the function RETURN @tempString END GO select dbo.tableaValue(id),id from tablea group by id

        J 1 Reply Last reply
        0
        • B Blue_Boy

          You have to use cursor. check this.[^]


          I Love T-SQL "Don't torture yourself,let the life to do it for you." If my post helps you kindly save my time by voting my post. www.aktualiteti.com

          A Offline
          A Offline
          Abdul Rahman Hamidy
          wrote on last edited by
          #4

          Thanks. I have used for Xml Path and I have done it in SQl 2005. but i want to do it only with pure T-Sql which I need the same thing in Ms Access also. any ideas for Ms Access 2003/2007

          Abdul Rahaman Hamidy Database Developer Kabul, Afghanistan

          1 Reply Last reply
          0
          • P Pranay Rana

            Following one of the solution work for you but there must be better solution than this SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO -- ============================================= -- Author: <Author,,Name> -- Create date: <Create Date, ,> -- Description: <Description, ,> -- ============================================= CREATE FUNCTION tableaValue ( @id int ) RETURNS varchar(50) AS BEGIN declare @name varchar(50) declare @tempString varchar(50) DECLARE tablea_cursor CURSOR FOR select name from tablea where id = @id -- Variable value from the outer cursor OPEN tablea_cursor FETCH NEXT FROM tablea_cursor INTO @name WHILE @@FETCH_STATUS = 0 BEGIN SELECT @tempString= isnull(@tempString,'') + ',' + @name FETCH NEXT FROM tablea_cursor INTO @name END CLOSE tablea_cursor DEALLOCATE tablea_cursor -- Return the result of the function RETURN @tempString END GO select dbo.tableaValue(id),id from tablea group by id

            J Offline
            J Offline
            J4amieC
            wrote on last edited by
            #5

            You dont even need the cursor to build a CSV, you can use COALESCE

            DECLARE @nameCsv VARCHAR(MAX)
            SET @nameCSV = ''

            SELECT @nameCsv = COALESCE(name + ',','') + @nameCsv
            FROM tablea

            SELECT LEFT(@nameCsv,LEN(@nameCsv)-1) AS NameCsv

            P 1 Reply Last reply
            0
            • J J4amieC

              You dont even need the cursor to build a CSV, you can use COALESCE

              DECLARE @nameCsv VARCHAR(MAX)
              SET @nameCSV = ''

              SELECT @nameCsv = COALESCE(name + ',','') + @nameCsv
              FROM tablea

              SELECT LEFT(@nameCsv,LEN(@nameCsv)-1) AS NameCsv

              P Offline
              P Offline
              Pranay Rana
              wrote on last edited by
              #6

              its good but it nned it id wise see the question he need csv value which group by id i hope this will on id basis thanks for reply

              J 1 Reply Last reply
              0
              • P Pranay Rana

                its good but it nned it id wise see the question he need csv value which group by id i hope this will on id basis thanks for reply

                J Offline
                J Offline
                J4amieC
                wrote on last edited by
                #7

                I have no idea what you just said :confused:

                1 Reply Last reply
                0
                • A Abdul Rahman Hamidy

                  Dear All, I am looking to query followings in Sql 2005 table as Table A ID Name 1 A 1 B 1 C 2 D 2 E 2 F I want the result as ID Name 1 A,B,C 2 D,E,F any ideas

                  Abdul Rahaman Hamidy Database Developer Kabul, Afghanistan

                  A Offline
                  A Offline
                  AprNgp
                  wrote on last edited by
                  #8

                  how abt this ?

                  select b.id, b.name from TableA b
                  where b.id in (select distinct a.id from TableA a)

                  Apurv “Never trust a computer you can’t throw out a window.” (Steve Wozniak) “There are only two industries that refer to their customers as ‘users’.” (Edward Tufte)

                  A 1 Reply Last reply
                  0
                  • A AprNgp

                    how abt this ?

                    select b.id, b.name from TableA b
                    where b.id in (select distinct a.id from TableA a)

                    Apurv “Never trust a computer you can’t throw out a window.” (Steve Wozniak) “There are only two industries that refer to their customers as ‘users’.” (Edward Tufte)

                    A Offline
                    A Offline
                    Abdul Rahman Hamidy
                    wrote on last edited by
                    #9

                    Thanks for reply. the query returns my table as it is, not the output which I am searching for

                    Abdul Rahaman Hamidy Database Developer Kabul, Afghanistan

                    A 1 Reply Last reply
                    0
                    • A Abdul Rahman Hamidy

                      Thanks for reply. the query returns my table as it is, not the output which I am searching for

                      Abdul Rahaman Hamidy Database Developer Kabul, Afghanistan

                      A Offline
                      A Offline
                      AprNgp
                      wrote on last edited by
                      #10

                      Oh .. sry .. I think 'cursors' can only solve this ...

                      Apurv “Never trust a computer you can’t throw out a window.” (Steve Wozniak) “There are only two industries that refer to their customers as ‘users’.” (Edward Tufte)

                      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