SitePoint Sponsor

User Tag List

Results 1 to 16 of 16

Thread: Display Query Results in Order

Hybrid View

  1. #1
    SitePoint Evangelist
    Join Date
    Oct 2006
    Posts
    408
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Display Query Results in Order

    Hi,

    I want to display the event field based on the value of @securityID.
    After that I want to display all event fields in the database.

    How do I do this? Belwo is my code...

    Code:
    SELECT     e.eventID, e.event
    FROM         tble AS e LEFT OUTER JOIN
                          tbld AS d ON d.eventID = e.eventID
    ORDER BY 
                              (SELECT     d2.securityID
                                FROM          tbld AS d2 INNER JOIN
                                                       tble AS e2 ON e2.eventID = d2.eventID
                                WHERE      (d2.securityID = @securityID))
    Thanks.

  2. #2
    SQL Consultant silver trophybronze trophy
    SitePoint Award Recipient r937's Avatar
    Join Date
    Jul 2002
    Location
    Toronto, Canada
    Posts
    38,457
    Mentioned
    34 Post(s)
    Tagged
    1 Thread(s)
    Code:
    SELECT e.eventID
         , e.event
      FROM tble AS e 
    LEFT OUTER 
      JOIN tbld AS d 
        ON d.eventID = e.eventID
    ORDER 
        BY case when e.securityID = @securityID
                then 'sort these first'
                else 'sort these last'
           end
         , e.eventID
    r937.com | rudy.ca | Buy my SitePoint book: Simply SQL
    "giving out my real stuffs"

  3. #3
    SitePoint Evangelist
    Join Date
    Oct 2006
    Posts
    408
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    SQL Server gives the error...

    "Parameter is Incorrect"

  4. #4
    SQL Consultant silver trophybronze trophy
    SitePoint Award Recipient r937's Avatar
    Join Date
    Jul 2002
    Location
    Toronto, Canada
    Posts
    38,457
    Mentioned
    34 Post(s)
    Tagged
    1 Thread(s)
    does the query work if you do this --
    Code:
    ORDER 
        BY e.eventID
    r937.com | rudy.ca | Buy my SitePoint book: Simply SQL
    "giving out my real stuffs"

  5. #5
    SitePoint Evangelist
    Join Date
    Oct 2006
    Posts
    408
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    put it in a stored procedure and works ok.

    However, if rpws from tbld match the same rows from tble then tble rows are displayed multiple times.
    Tried SELECT DISTINCT but doesn't work with ORDER BY in SQL Server

  6. #6
    SQL Consultant silver trophybronze trophy
    SitePoint Award Recipient r937's Avatar
    Join Date
    Jul 2002
    Location
    Toronto, Canada
    Posts
    38,457
    Mentioned
    34 Post(s)
    Tagged
    1 Thread(s)
    yes, SELECT DISTINCT does work with ORDER BY in SQL Server

    however, SELECT DISTINCT will not resolve a one-to-many "problem"

    perhaps you could show us your latest query
    r937.com | rudy.ca | Buy my SitePoint book: Simply SQL
    "giving out my real stuffs"

  7. #7
    SitePoint Evangelist
    Join Date
    Oct 2006
    Posts
    408
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hi,

    Here is my query...
    Code:
        SELECT e.eventID, e.event
        FROM tble AS e 
        LEFT OUTER JOIN
        tbld AS d 
        ON d.eventID = e.eventID
        ORDER BY CASE WHEN d.securityID = @securityID 
        THEN 'sort these first' ELSE 'sort these last' END, e.eventID

  8. #8
    SQL Consultant silver trophybronze trophy
    SitePoint Award Recipient r937's Avatar
    Join Date
    Jul 2002
    Location
    Toronto, Canada
    Posts
    38,457
    Mentioned
    34 Post(s)
    Tagged
    1 Thread(s)
    try replacing WHEN d.securityID = @securityID with WHEN e.securityID = @securityID
    r937.com | rudy.ca | Buy my SitePoint book: Simply SQL
    "giving out my real stuffs"

  9. #9
    SQL Consultant silver trophybronze trophy
    SitePoint Award Recipient r937's Avatar
    Join Date
    Jul 2002
    Location
    Toronto, Canada
    Posts
    38,457
    Mentioned
    34 Post(s)
    Tagged
    1 Thread(s)
    by the way, for what reason are you doing a self-join?
    r937.com | rudy.ca | Buy my SitePoint book: Simply SQL
    "giving out my real stuffs"

  10. #10
    SitePoint Evangelist
    Join Date
    Oct 2006
    Posts
    408
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hi,

    e.g. My table has 3 rows with same ID and want them all displayed in textBox...

    event eventID
    ----------------- ---------------
    itemA 2
    itemB 2
    itemC 2

    so the textBox should display itemA,itemB,itemC

    Thank You

  11. #11
    SitePoint Evangelist
    Join Date
    Oct 2006
    Posts
    408
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    e.securityID does not exist. securityID only in tbld

    also tried...
    Code:
    SELECT CONCAT(e.event) AS myColumn FROM tble
    but expects another value.

  12. #12
    SQL Consultant silver trophybronze trophy
    SitePoint Award Recipient r937's Avatar
    Join Date
    Jul 2002
    Location
    Toronto, Canada
    Posts
    38,457
    Mentioned
    34 Post(s)
    Tagged
    1 Thread(s)
    please, don't confuse me, stick with eventID and event, not myField and ID

    to display 3 rows with the same id, just select them
    r937.com | rudy.ca | Buy my SitePoint book: Simply SQL
    "giving out my real stuffs"

  13. #13
    SitePoint Evangelist
    Join Date
    Oct 2006
    Posts
    408
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Sorry Rudy!
    I've been confusing myself with different stored procedures.

    My answer to post#9 is incorrect.
    Here is what I want...

    tble and tbld are related via eventID
    tbld can contain the same eventID for each row.
    So I use a JOIN to display the event based on @securityID value.
    Then I want to display all event fields in the tble database table.

    Problem is that, because tbld contains multiple references to the one eventID in tble, the same event is displayed a number f times.

    How do I prevent this??

    Thanks and sorry for confusing the both of us!

  14. #14
    SQL Consultant silver trophybronze trophy
    SitePoint Award Recipient r937's Avatar
    Join Date
    Jul 2002
    Location
    Toronto, Canada
    Posts
    38,457
    Mentioned
    34 Post(s)
    Tagged
    1 Thread(s)
    okay, i think i finally understand
    Code:
    SELECT e.eventID
         , e.event
         , MIN( CASE WHEN d.securityID = @securityID 
                     THEN 'sort these first' 
                     ELSE 'sort these last' 
                 END ) as sortkey
      FROM tble AS e 
    LEFT OUTER 
      JOIN tbld AS d 
        ON d.eventID = e.eventID
    GROUP
        BY e.eventID
         , e.event
    ORDER 
        BY sortkey
         , e.eventID
    r937.com | rudy.ca | Buy my SitePoint book: Simply SQL
    "giving out my real stuffs"

  15. #15
    SitePoint Evangelist
    Join Date
    Oct 2006
    Posts
    408
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Great! That works.

    Could you please explain how 'sort these first' works? Is that actual SQL code?

    Thank You.

  16. #16
    SQL Consultant silver trophybronze trophy
    SitePoint Award Recipient r937's Avatar
    Join Date
    Jul 2002
    Location
    Toronto, Canada
    Posts
    38,457
    Mentioned
    34 Post(s)
    Tagged
    1 Thread(s)
    i was wondering if you'd notice

    actually it could be anything

    evaluate the CASE expression for every d row, and it will produce one value or the other

    then, in the GROUP BY, take the minimum of all the values for each group, i.e. for each e row

    if one of the d.securityID values was equal to @securityID, then the MIN will equal the THEN value, simply because the THEN value is less than the ELSE value

    so all the events that had at least one @securityID row will have the lower value in the sortkey column, and therefore be sorted first

    why 'sort these first' and 'sort these last' as values? because "f" comes before "l"
    r937.com | rudy.ca | 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
  •