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. helping writing sql query

helping writing sql query

Scheduled Pinned Locked Moved Database
databasetutorial
9 Posts 7 Posters 1 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.
  • M Offline
    M Offline
    michaelgr1
    wrote on last edited by
    #1

    Hello, FOr example i have the following table class name 1 m 1 n 1 a 1 b 1 c 2 d 2 e 2 f I need the following result: class name 1 m,n,a,b,c 2 d,e,f I mean grouping by class and all the names in one row

    B P P R 4 Replies Last reply
    0
    • M michaelgr1

      Hello, FOr example i have the following table class name 1 m 1 n 1 a 1 b 1 c 2 d 2 e 2 f I need the following result: class name 1 m,n,a,b,c 2 d,e,f I mean grouping by class and all the names in one row

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

      Use Cursor


      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

      modified on Monday, September 20, 2010 9:16 AM

      1 Reply Last reply
      0
      • M michaelgr1

        Hello, FOr example i have the following table class name 1 m 1 n 1 a 1 b 1 c 2 d 2 e 2 f I need the following result: class name 1 m,n,a,b,c 2 d,e,f I mean grouping by class and all the names in one row

        P Offline
        P Offline
        PIEBALDconsult
        wrote on last edited by
        #3

        michaelgr1 wrote:

        all the names in one row

        Do you mean "in one field"? If using Sql Server, you can write a custom aggregation function. There's an example somewhere on MSDN or the help. It may not be the best solution to this problem, but it's good to know how to do it.

        M 1 Reply Last reply
        0
        • P PIEBALDconsult

          michaelgr1 wrote:

          all the names in one row

          Do you mean "in one field"? If using Sql Server, you can write a custom aggregation function. There's an example somewhere on MSDN or the help. It may not be the best solution to this problem, but it's good to know how to do it.

          M Offline
          M Offline
          michaelgr1
          wrote on last edited by
          #4

          I need it in a field. How can i do it? can you show me please? BTW, I have only read only access to the DB

          1 Reply Last reply
          0
          • M michaelgr1

            Hello, FOr example i have the following table class name 1 m 1 n 1 a 1 b 1 c 2 d 2 e 2 f I need the following result: class name 1 m,n,a,b,c 2 d,e,f I mean grouping by class and all the names in one row

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

            Use the pivot command. You can find more information here[^].

            I have CDO, it's OCD with the letters in the right order; just as they ruddy well should be

            Forgive your enemies - it messes with their heads

            My blog | My articles | MoXAML PowerToys | Onyx

            1 Reply Last reply
            0
            • M michaelgr1

              Hello, FOr example i have the following table class name 1 m 1 n 1 a 1 b 1 c 2 d 2 e 2 f I need the following result: class name 1 m,n,a,b,c 2 d,e,f I mean grouping by class and all the names in one row

              R Offline
              R Offline
              RyanEK
              wrote on last edited by
              #6

              You can use xml path

              declare @tmp table (class int, name char)
              insert into @tmp values (1, 'm')
              insert into @tmp values (1, 'n')
              insert into @tmp values (1, 'a')
              insert into @tmp values (1, 'b')
              insert into @tmp values (1, 'c')
              insert into @tmp values (2, 'd')
              insert into @tmp values (2, 'e')
              insert into @tmp values (2, 'f')

              select class,
              replace(replace((
              select replace(name,' ','*') as 'data()'
              from @tmp t2
              where t1.class = t2.class
              for xml path('')),' ',','),'*',' ') as name
              from @tmp t1
              group by class

              M I 2 Replies Last reply
              0
              • R RyanEK

                You can use xml path

                declare @tmp table (class int, name char)
                insert into @tmp values (1, 'm')
                insert into @tmp values (1, 'n')
                insert into @tmp values (1, 'a')
                insert into @tmp values (1, 'b')
                insert into @tmp values (1, 'c')
                insert into @tmp values (2, 'd')
                insert into @tmp values (2, 'e')
                insert into @tmp values (2, 'f')

                select class,
                replace(replace((
                select replace(name,' ','*') as 'data()'
                from @tmp t2
                where t1.class = t2.class
                for xml path('')),' ',','),'*',' ') as name
                from @tmp t1
                group by class

                M Offline
                M Offline
                Mycroft Holmes
                wrote on last edited by
                #7

                I hate XML X| but it is an excellent answer!

                Never underestimate the power of human stupidity RAH

                1 Reply Last reply
                0
                • R RyanEK

                  You can use xml path

                  declare @tmp table (class int, name char)
                  insert into @tmp values (1, 'm')
                  insert into @tmp values (1, 'n')
                  insert into @tmp values (1, 'a')
                  insert into @tmp values (1, 'b')
                  insert into @tmp values (1, 'c')
                  insert into @tmp values (2, 'd')
                  insert into @tmp values (2, 'e')
                  insert into @tmp values (2, 'f')

                  select class,
                  replace(replace((
                  select replace(name,' ','*') as 'data()'
                  from @tmp t2
                  where t1.class = t2.class
                  for xml path('')),' ',','),'*',' ') as name
                  from @tmp t1
                  group by class

                  I Offline
                  I Offline
                  i j russell
                  wrote on last edited by
                  #8

                  I find using STUFF to be slightly easier to read;

                  SELECT class,
                  STUFF(
                  (
                  SELECT
                  ',' + name
                  FROM @tmp t2
                  WHERE t2.class = t1.class
                  FOR XML PATH('')
                  ), 1, 1, '') AS data
                  FROM @tmp t1
                  GROUP BY class

                  M 1 Reply Last reply
                  0
                  • I i j russell

                    I find using STUFF to be slightly easier to read;

                    SELECT class,
                    STUFF(
                    (
                    SELECT
                    ',' + name
                    FROM @tmp t2
                    WHERE t2.class = t1.class
                    FOR XML PATH('')
                    ), 1, 1, '') AS data
                    FROM @tmp t1
                    GROUP BY class

                    M Offline
                    M Offline
                    Mycroft Holmes
                    wrote on last edited by
                    #9

                    I'd like to STUFF all xml and it's derivatives where the sun don't shine.

                    Never underestimate the power of human stupidity RAH

                    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