• Visitors can check out the Forum FAQ by clicking this link. You have to register before you can post: click the REGISTER link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below. View our Forum Privacy Policy.
  • Want to receive the latest contracting news and advice straight to your inbox? Sign up to the ContractorUK newsletter here. Every sign up will also be entered into a draw to WIN £100 Amazon vouchers!

SQL challenge

Collapse
X
  •  
  • Filter
  • Time
  • Show
Clear All
new posts

    SQL challenge

    I have data like this:

    Name Event

    Person1 Event1
    Person1 Event2
    Person1 Event3
    Person2 Event1
    Person2 Event2
    etc

    and I want to report it like this:

    Name: Count of Events: Events

    Person1 3 Event1/Event2/Event3
    Person2 2 Event1/Event2
    etc

    How do I do this in SQL?
    Hard Brexit now!
    #prayfornodeal

    #2
    You can't.

    Try writing a program.

    HTH.

    Comment


      #3
      Easy enough writing a program in SAS. But I don't believe you can't do it in SQL!
      Hard Brexit now!
      #prayfornodeal

      Comment


        #4
        Well then first part is easy-

        Select Name, count(*)
        from TableName
        Order By Name
        Group By Name;

        This will give you:

        Person1 3
        Person2 2

        But I can't see how you would select multiple columns from the same row to get your list of event names.

        Comment


          #5
          Originally posted by G8_Summit

          But I can't see how you would select multiple columns from the same row to get your list of event names.
          Well yes that is the problem ....
          Hard Brexit now!
          #prayfornodeal

          Comment


            #6
            dump the collated events to a temp table first then select on a join?

            Comment


              #7
              Originally posted by scotspine
              dump the collated events to a temp table first then select on a join?
              Don't you have top decide on a maximum number of events to do that?

              Comment


                #8
                Originally posted by scotspine
                dump the collated events to a temp table first then select on a join?
                But the question is how to collate (concatenate?) them in the first place ...

                Edited:
                Yes the number of events is unknown. Mind you its easy enough to get the max number of events by doing a count first and joining. Hmmm beginning to see a glimmer of how this might work ....
                Last edited by sasguru; 7 February 2006, 10:32.
                Hard Brexit now!
                #prayfornodeal

                Comment


                  #9
                  just loop through in sql and stitch them together

                  Comment


                    #10
                    Since when did looping become part of SQL ??

                    Looping is programmatical - thats why I said use a program.

                    Comment

                    Working...
                    X