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. Single row for multiple results

Single row for multiple results

Scheduled Pinned Locked Moved Database
tutorial
14 Posts 9 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.
  • V Offline
    V Offline
    vanikanc
    wrote on last edited by
    #1

    Hello, I would like to show data as a single row instead of multiple rows. For example, a certain user has two status associated, like maybe "Pending Approval" and "Waiting Email Verfication". Instead of showing these statuses on two different rows, I would like to show as one row. Currently, John Smith Pending Approval John Smith Waiting Email Verfication Should be John Smith Pending Approval, Waiting Email Verfication There could such multiple states for this user, but all show in one row. Thanks so much for your time!

    B T L P N 6 Replies Last reply
    0
    • V vanikanc

      Hello, I would like to show data as a single row instead of multiple rows. For example, a certain user has two status associated, like maybe "Pending Approval" and "Waiting Email Verfication". Instead of showing these statuses on two different rows, I would like to show as one row. Currently, John Smith Pending Approval John Smith Waiting Email Verfication Should be John Smith Pending Approval, Waiting Email Verfication There could such multiple states for this user, but all show in one row. Thanks so much for your time!

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

      You can use C# code to do it. Or do you need to do it only in SQL side?


      I Love T-SQL "VB.NET is developed with C#.NET" If my post helps you kindly save my time by voting my post. www.cacttus.com

      V 1 Reply Last reply
      0
      • B Blue_Boy

        You can use C# code to do it. Or do you need to do it only in SQL side?


        I Love T-SQL "VB.NET is developed with C#.NET" If my post helps you kindly save my time by voting my post. www.cacttus.com

        V Offline
        V Offline
        vanikanc
        wrote on last edited by
        #3

        SQL - MS SQL. Thanks!

        B 1 Reply Last reply
        0
        • V vanikanc

          Hello, I would like to show data as a single row instead of multiple rows. For example, a certain user has two status associated, like maybe "Pending Approval" and "Waiting Email Verfication". Instead of showing these statuses on two different rows, I would like to show as one row. Currently, John Smith Pending Approval John Smith Waiting Email Verfication Should be John Smith Pending Approval, Waiting Email Verfication There could such multiple states for this user, but all show in one row. Thanks so much for your time!

          T Offline
          T Offline
          Tim Carmichael
          wrote on last edited by
          #4

          You can either build your state list directly in a stored procedure, or create a function that will build it for you. I encountered a similar situation yesterday: for a given item, multiple sources of data. I created a function to concatenate the items, comma delimited, and used the function in the query to return all of the data in a single row. Tim

          V 1 Reply Last reply
          0
          • V vanikanc

            SQL - MS SQL. Thanks!

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

            Can you post structure of your table? I guess your table contain unique column?


            I Love T-SQL "VB.NET is developed with C#.NET" If my post helps you kindly save my time by voting my post. www.cacttus.com

            1 Reply Last reply
            0
            • T Tim Carmichael

              You can either build your state list directly in a stored procedure, or create a function that will build it for you. I encountered a similar situation yesterday: for a given item, multiple sources of data. I created a function to concatenate the items, comma delimited, and used the function in the query to return all of the data in a single row. Tim

              V Offline
              V Offline
              vanikanc
              wrote on last edited by
              #6

              My apologizes. This link worked perfectly,

              http://www.sqlservercentral.com/scripts/Miscellaneous/32004/[^]

              , using COALESE

              1 Reply Last reply
              0
              • V vanikanc

                Hello, I would like to show data as a single row instead of multiple rows. For example, a certain user has two status associated, like maybe "Pending Approval" and "Waiting Email Verfication". Instead of showing these statuses on two different rows, I would like to show as one row. Currently, John Smith Pending Approval John Smith Waiting Email Verfication Should be John Smith Pending Approval, Waiting Email Verfication There could such multiple states for this user, but all show in one row. Thanks so much for your time!

                L Offline
                L Offline
                Lost User
                wrote on last edited by
                #7

                This should help.

                DECLARE @Col1 varchar(max)

                SELECT @Col1 = COALESCE(@Col1 + ', ', '') + CAST(Col1 AS varchar(100))
                FROM Table1
                WHERE (................)

                SELECT @EmployeeList

                1 Reply Last reply
                0
                • V vanikanc

                  Hello, I would like to show data as a single row instead of multiple rows. For example, a certain user has two status associated, like maybe "Pending Approval" and "Waiting Email Verfication". Instead of showing these statuses on two different rows, I would like to show as one row. Currently, John Smith Pending Approval John Smith Waiting Email Verfication Should be John Smith Pending Approval, Waiting Email Verfication There could such multiple states for this user, but all show in one row. Thanks so much for your time!

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

                  You can write your own aggregates with CLR Stored Procedures. CLR Stored Procedure and Creating It Step by Step[^] http://msdn.microsoft.com/en-us/library/ms190790.aspx[^] But I don't recommend it.

                  1 Reply Last reply
                  0
                  • V vanikanc

                    Hello, I would like to show data as a single row instead of multiple rows. For example, a certain user has two status associated, like maybe "Pending Approval" and "Waiting Email Verfication". Instead of showing these statuses on two different rows, I would like to show as one row. Currently, John Smith Pending Approval John Smith Waiting Email Verfication Should be John Smith Pending Approval, Waiting Email Verfication There could such multiple states for this user, but all show in one row. Thanks so much for your time!

                    N Offline
                    N Offline
                    Niladri_Biswas
                    wrote on last edited by
                    #9

                    Assuming Sql Server 2005 and above, try this

                    Declare @t table([Name] varchar(20),[Status] varchar(50))
                    insert into @t values
                    ('John Smith','Pending Approval'),('John Smith','Waiting Email Verfication')
                    --,('User2','Pending Approved'),('User2','Sent email'),('User2','Other activity')
                    --Select * from @t

                    select Result = [Name] + ' ' + Stuff((Select ',' + CAST([Status] as varchar(100)) from @t t2 where t1.[Name]
                    = t2.[Name] for XML Path('')),1,1,'')
                    from @t t1
                    group by t1.[Name]

                    /*
                    Result

                    John Smith Pending Approval,Waiting Email Verfication
                    */

                    Niladri Biswas

                    _ S 2 Replies Last reply
                    0
                    • N Niladri_Biswas

                      Assuming Sql Server 2005 and above, try this

                      Declare @t table([Name] varchar(20),[Status] varchar(50))
                      insert into @t values
                      ('John Smith','Pending Approval'),('John Smith','Waiting Email Verfication')
                      --,('User2','Pending Approved'),('User2','Sent email'),('User2','Other activity')
                      --Select * from @t

                      select Result = [Name] + ' ' + Stuff((Select ',' + CAST([Status] as varchar(100)) from @t t2 where t1.[Name]
                      = t2.[Name] for XML Path('')),1,1,'')
                      from @t t1
                      group by t1.[Name]

                      /*
                      Result

                      John Smith Pending Approval,Waiting Email Verfication
                      */

                      Niladri Biswas

                      _ Offline
                      _ Offline
                      _Zorro_
                      wrote on last edited by
                      #10

                      STUFF is what you're looking for indeed.

                      V 1 Reply Last reply
                      0
                      • _ _Zorro_

                        STUFF is what you're looking for indeed.

                        V Offline
                        V Offline
                        vanikanc
                        wrote on last edited by
                        #11

                        Thanks for your responses. Instead of a comma separated list, i would like it as line breaks. I am separating the information by pipes, and later doing a replace after calling the function like below. I am doing this -- SELECT replace(DBO.fn_getAssociateComments(168), '|', char(13)+char(10)) But it is putting any line breaks??Am I missing something? Thanks a bunch!

                        _ 1 Reply Last reply
                        0
                        • V vanikanc

                          Thanks for your responses. Instead of a comma separated list, i would like it as line breaks. I am separating the information by pipes, and later doing a replace after calling the function like below. I am doing this -- SELECT replace(DBO.fn_getAssociateComments(168), '|', char(13)+char(10)) But it is putting any line breaks??Am I missing something? Thanks a bunch!

                          _ Offline
                          _ Offline
                          _Zorro_
                          wrote on last edited by
                          #12

                          Hi, According to this stackoverflow thread [^], it should work. Don't know why it doesn't work for you... Did you try replacing CHAR(13) + CHAR(10) with "\r\n" just to try? Edit: Maybe you should take a look at this example too: [^]

                          1 Reply Last reply
                          0
                          • N Niladri_Biswas

                            Assuming Sql Server 2005 and above, try this

                            Declare @t table([Name] varchar(20),[Status] varchar(50))
                            insert into @t values
                            ('John Smith','Pending Approval'),('John Smith','Waiting Email Verfication')
                            --,('User2','Pending Approved'),('User2','Sent email'),('User2','Other activity')
                            --Select * from @t

                            select Result = [Name] + ' ' + Stuff((Select ',' + CAST([Status] as varchar(100)) from @t t2 where t1.[Name]
                            = t2.[Name] for XML Path('')),1,1,'')
                            from @t t1
                            group by t1.[Name]

                            /*
                            Result

                            John Smith Pending Approval,Waiting Email Verfication
                            */

                            Niladri Biswas

                            S Offline
                            S Offline
                            smcnulty2000
                            wrote on last edited by
                            #13

                            I keep bumping into a need for this. I'm tossing this into my code library. Very, very useful. In fact it was useful today in a problem.

                            _____________________________ Give a man a mug, he drinks for a day. Teach a man to mug...

                            1 Reply Last reply
                            0
                            • V vanikanc

                              Hello, I would like to show data as a single row instead of multiple rows. For example, a certain user has two status associated, like maybe "Pending Approval" and "Waiting Email Verfication". Instead of showing these statuses on two different rows, I would like to show as one row. Currently, John Smith Pending Approval John Smith Waiting Email Verfication Should be John Smith Pending Approval, Waiting Email Verfication There could such multiple states for this user, but all show in one row. Thanks so much for your time!

                              S Offline
                              S Offline
                              S Douglas
                              wrote on last edited by
                              #14

                              Without knowing the stucture of your tables it's kinda hard to say, but one approach could be to pivot the data into a temp table. Then you can maniulate the results as needed. http://blog.sqlauthority.com/2008/06/07/sql-server-pivot-and-unpivot-table-examples/[^]


                              Common sense is admitting there is cause and effect and that you can exert some control over what you understand.

                              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