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
databasehelptutorial
6 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.
  • W Offline
    W Offline
    walalawll
    wrote on last edited by
    #1

    I have a table which has Class Name 1A Albert 1A Bill 1A Paul 1B Ricky 1B Kelvin 1B Walala I want to convert it to a table which is Class FULL LIST 1A Albert/Bill/Paul 1B Ricky/Kelvin/Walala I dont know how to write the query. Pls kindly help

    P E 2 Replies Last reply
    0
    • W walalawll

      I have a table which has Class Name 1A Albert 1A Bill 1A Paul 1B Ricky 1B Kelvin 1B Walala I want to convert it to a table which is Class FULL LIST 1A Albert/Bill/Paul 1B Ricky/Kelvin/Walala I dont know how to write the query. Pls kindly help

      P Offline
      P Offline
      Pete OHanlon
      wrote on last edited by
      #2

      Which database are you trying to run this in. If it's SQL Server 2005, you can use a thing called a Common Table Expression to help you out with this. If it's not, (and I apologise to the Oracle guys if they have something whizzbang like CTE's), you will need to write a recursive query. These aren't fun, but examples can be found on Google.

      Arthur Dent - "That would explain it. All my life I've had this strange feeling that there's something big and sinister going on in the world." Slartibartfast - "No. That's perfectly normal paranoia. Everybody in the universe gets that." Deja View - the feeling that you've seen this post before.

      W 1 Reply Last reply
      0
      • P Pete OHanlon

        Which database are you trying to run this in. If it's SQL Server 2005, you can use a thing called a Common Table Expression to help you out with this. If it's not, (and I apologise to the Oracle guys if they have something whizzbang like CTE's), you will need to write a recursive query. These aren't fun, but examples can be found on Google.

        Arthur Dent - "That would explain it. All my life I've had this strange feeling that there's something big and sinister going on in the world." Slartibartfast - "No. That's perfectly normal paranoia. Everybody in the universe gets that." Deja View - the feeling that you've seen this post before.

        W Offline
        W Offline
        walalawll
        wrote on last edited by
        #3

        Oh no.....I am SQL Server 2000....But anyway, thank for helping.

        1 Reply Last reply
        0
        • W walalawll

          I have a table which has Class Name 1A Albert 1A Bill 1A Paul 1B Ricky 1B Kelvin 1B Walala I want to convert it to a table which is Class FULL LIST 1A Albert/Bill/Paul 1B Ricky/Kelvin/Walala I dont know how to write the query. Pls kindly help

          E Offline
          E Offline
          Eric Dahlvang
          wrote on last edited by
          #4

          You could do something like this:

          CREATE FUNCTION GetNames (@cClass varchar(2))
          RETURNS varchar(1000) AS
          BEGIN

          DECLARE @cClassNames varchar(1000)
          DECLARE @cClassName varchar(50)

          SELECT @cClassNames = ''

          DECLARE namecursor CURSOR FOR
          SELECT DISTINCT Name
          FROM classtable
          WHERE Class = @cClass

          OPEN namecursor
          FETCH NEXT FROM namecursor INTO @cClassName
          WHILE @@FETCH_STATUS = 0
          BEGIN
          select @cClassNames = @cClassNames + @cClassName +'/'
          FETCH NEXT FROM namecursor INTO @cClassName
          END

          CLOSE namecursor
          DEALLOCATE namecursor
          RETURN(@cClassNames)
          END

          Call it like this:

          select class, dbo.GetNames(class) as [Full List] from (select distinct class from classtable) as tbl

          --EricDV Sig--------- Some problems are so complex that you have to be highly intelligent and well informed just to be undecided about them. - Laurence J. Peters

          P W 2 Replies Last reply
          0
          • E Eric Dahlvang

            You could do something like this:

            CREATE FUNCTION GetNames (@cClass varchar(2))
            RETURNS varchar(1000) AS
            BEGIN

            DECLARE @cClassNames varchar(1000)
            DECLARE @cClassName varchar(50)

            SELECT @cClassNames = ''

            DECLARE namecursor CURSOR FOR
            SELECT DISTINCT Name
            FROM classtable
            WHERE Class = @cClass

            OPEN namecursor
            FETCH NEXT FROM namecursor INTO @cClassName
            WHILE @@FETCH_STATUS = 0
            BEGIN
            select @cClassNames = @cClassNames + @cClassName +'/'
            FETCH NEXT FROM namecursor INTO @cClassName
            END

            CLOSE namecursor
            DEALLOCATE namecursor
            RETURN(@cClassNames)
            END

            Call it like this:

            select class, dbo.GetNames(class) as [Full List] from (select distinct class from classtable) as tbl

            --EricDV Sig--------- Some problems are so complex that you have to be highly intelligent and well informed just to be undecided about them. - Laurence J. Peters

            P Offline
            P Offline
            Pete OHanlon
            wrote on last edited by
            #5

            Nice - I was working on one which didn't use cursors (it used stored functions), but there's no need now as yours does the job perfectly.:-D

            Arthur Dent - "That would explain it. All my life I've had this strange feeling that there's something big and sinister going on in the world." Slartibartfast - "No. That's perfectly normal paranoia. Everybody in the universe gets that." Deja View - the feeling that you've seen this post before.

            1 Reply Last reply
            0
            • E Eric Dahlvang

              You could do something like this:

              CREATE FUNCTION GetNames (@cClass varchar(2))
              RETURNS varchar(1000) AS
              BEGIN

              DECLARE @cClassNames varchar(1000)
              DECLARE @cClassName varchar(50)

              SELECT @cClassNames = ''

              DECLARE namecursor CURSOR FOR
              SELECT DISTINCT Name
              FROM classtable
              WHERE Class = @cClass

              OPEN namecursor
              FETCH NEXT FROM namecursor INTO @cClassName
              WHILE @@FETCH_STATUS = 0
              BEGIN
              select @cClassNames = @cClassNames + @cClassName +'/'
              FETCH NEXT FROM namecursor INTO @cClassName
              END

              CLOSE namecursor
              DEALLOCATE namecursor
              RETURN(@cClassNames)
              END

              Call it like this:

              select class, dbo.GetNames(class) as [Full List] from (select distinct class from classtable) as tbl

              --EricDV Sig--------- Some problems are so complex that you have to be highly intelligent and well informed just to be undecided about them. - Laurence J. Peters

              W Offline
              W Offline
              walalawll
              wrote on last edited by
              #6

              Many Many Thanks. You give me a great great hand.

              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