Advertisement
If you have a new account but are having problems posting or verifying your account, please email us on hello@boards.ie for help. Thanks :)
Hello all! Please ensure that you are posting a new thread or question in the appropriate forum. The Feedback forum is overwhelmed with questions that are having to be moved elsewhere. If you need help to verify your account contact hello@boards.ie

Stumped on SQL

Options
  • 05-01-2008 3:00pm
    #1
    Registered Users Posts: 876 ✭✭✭


    I'm trying to use a subquery in the sql below but not having any luck. Anyone spot my error?

    Basically, the subquery will return something like:

    loc1 dirs1 2
    loc2 dirs2 1
    loc1 dirs1 3

    and then I want the main query to give:

    loc1 5
    loc2 1

    sSQL = "SELECT [mainlist].Location, Sum(CountofNCP) AS SumofNCP FROM (SELECT [mainlist].Location, dirs.directions, Count(mainlist.ncp_tt) AS CountOfNCP FROM mainlist INNER JOIN dirs ON [mainlist].Location = dirs.areadirs GROUP BY 'Location, [mainlist].Level, dirs.directions HAVING [mainlist].Level = " & mylevel_form & " OR [mainlist].Level = " & mylevel_form -1 & " OR [mainlist].Level = " & mylevel_form +1 & ") ;"


Comments

  • Moderators, Politics Moderators Posts: 39,267 Mod ✭✭✭✭Seth Brundle


    As you don't say what is going wrong, I'm just guessing the following:
    The single quote at 'Location is the source.
    sSQL = "
    SELECT [mainlist].Location, Sum(CountofNCP) AS SumofNCP 
    FROM (
         SELECT [mainlist].Location, dirs.directions, Count(mainlist.ncp_tt) AS CountOfNCP 
         FROM mainlist 
         INNER JOIN dirs ON [mainlist].Location = dirs.areadirs 
         GROUP BY [B]'Location[/B], [mainlist].Level, dirs.directions 
         HAVING [mainlist].Level = " & mylevel_form & " 
         OR [mainlist].Level = " & mylevel_form -1 & " 
         OR [mainlist].Level = " & mylevel_form +1 & ") ;"
    


  • Registered Users Posts: 876 ✭✭✭sirpsycho


    The quote at 'Location was part of the problem but I forgot to add in the GROUP BY for the main query.


Advertisement