SitePoint Sponsor

User Tag List

Results 1 to 4 of 4
  1. #1
    SitePoint Addict
    Join Date
    Nov 2004
    Location
    New Jersey
    Posts
    317
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Selecting values from different rows as one row, when Grouping by another field?

    Say I have a table like this:
    Code SQL:
    ID	Guess
    1	50
    1	30
    1	20
    2	40
    2	35
    2	25
    3	60
    3	15
    3	25
    With "ID" as an Index.

    And I want a query that groups by the "ID", and returns all the "Guess" values for each "ID" in the same row, like this:
    Code SQL:
    ID	Guess1	Guess2	Guess3
    1	50	30	20
    2	40	35	25
    3	60	15	25
    How do I do this?

  2. #2
    SQL Consultant gold trophysilver trophybronze trophy
    r937's Avatar
    Join Date
    Jul 2002
    Location
    Toronto, Canada
    Posts
    39,338
    Mentioned
    63 Post(s)
    Tagged
    3 Thread(s)
    Quote Originally Posted by Kadence View Post
    How do I do this?
    it is better that you do this in your application language (php or whatever)
    rudy.ca | @rudydotca
    Buy my SitePoint book: Simply SQL
    "giving out my real stuffs"

  3. #3
    SitePoint Addict
    Join Date
    Nov 2004
    Location
    New Jersey
    Posts
    317
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thank you, though for my program I kind of need to do this in SQL.

    Is there no way to do this using LEFT JOINs or something like that?

  4. #4
    SQL Consultant gold trophysilver trophybronze trophy
    r937's Avatar
    Join Date
    Jul 2002
    Location
    Toronto, Canada
    Posts
    39,338
    Mentioned
    63 Post(s)
    Tagged
    3 Thread(s)
    Quote Originally Posted by Kadence View Post
    Thank you, though for my program I kind of need to do this in SQL.
    that must be an awfully anemic programming language you're using

    no, you can't do this with left joins

    if you're on mysql, you could use GROUP_CONCAT, but that will create a single column, not three
    rudy.ca | @rudydotca
    Buy my SitePoint book: Simply SQL
    "giving out my real stuffs"


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •