ASP SQL Select in a Select issue

  • Hi all,

    I've seen this able to work on MySQL, but need it working on my ASP/MSSQL site.

    Here's the code:

    Code:

    SQL = "SELECT TOP 10 differential FROM (SELECT TOP 20 * FROM gamedetails where username = '"&request.Form("Username")&"' ORDER BY DATE_Played DESC) WHERE username = '"&request.Form("Username")&"' order by differential ASC"

    My idea is to get the last 20 records posted, and then select the best 10 from those 20 to get to a calculation i've got running.

    I think i'm close to getting it right, but i'm missing something.

    Any ideas?

    Thanks

    Chris

  • Hi,

    Got the problem fixed. Apparently need to post the question to get the juices flowing.

    SQL = "SELECT differential FROM (SELECT TOP 10 differential FROM (SELECT TOP 20 differential FROM gamedetails WHERE username = '"&request.Form("Username")&"' ORDER BY date_played DESC) AS XXX ORDER BY differential) AS YYY"

    Works a treat.

    C

  • Glad we could help. 🙂

    [font="Times New Roman"]-- RBarryYoung[/font], [font="Times New Roman"] (302)375-0451[/font] blog: MovingSQL.com, Twitter: @RBarryYoung[font="Arial Black"]
    Proactive Performance Solutions, Inc.
    [/font]
    [font="Verdana"] "Performance is our middle name."[/font]

  • Got some more efficient code for an FYI:

    SQL = "SELECT TOP 10 X.differential FROM ( " _

    & " SELECT TOP 20 differential FROM gamedetails " _

    & " WHERE username = '" & request.Form("Username") & "' " _

    & " ORDER BY DATE_Played DESC ) AS X " _

    & "ORDER BY X.differential ASC"

  • chris.pols (8/5/2009)


    Got some more efficient code for an FYI:

    SQL = "SELECT TOP 10 X.differential FROM ( " _

    & " SELECT TOP 20 differential FROM gamedetails " _

    & " WHERE username = '" & request.Form("Username") & "' " _

    & " ORDER BY DATE_Played DESC ) AS X " _

    & "ORDER BY X.differential ASC"

    FYI: you probably want to parametize that because right now it is very susceptible to SQL Injection attacks.

    [font="Times New Roman"]-- RBarryYoung[/font], [font="Times New Roman"] (302)375-0451[/font] blog: MovingSQL.com, Twitter: @RBarryYoung[font="Arial Black"]
    Proactive Performance Solutions, Inc.
    [/font]
    [font="Verdana"] "Performance is our middle name."[/font]

Viewing 5 posts - 1 through 5 (of 5 total)

You must be logged in to reply to this topic. Login to reply