Click here to Skip to main content
15,913,685 members
Home / Discussions / Database
   

Database

 
GeneralRe: Validateing values in Stored Procedures Pin
Colin Angus Mackay5-May-05 0:21
Colin Angus Mackay5-May-05 0:21 
GeneralPlease help me: need sp4 Pin
Chak4-May-05 15:29
Chak4-May-05 15:29 
GeneralRe: Please help me: need sp4 Pin
Colin Angus Mackay4-May-05 19:37
Colin Angus Mackay4-May-05 19:37 
GeneralProb using Oracle 9i Pin
vishalmishra4-May-05 11:46
vishalmishra4-May-05 11:46 
GeneralRe: Prob using Oracle 9i Pin
Colin Angus Mackay4-May-05 12:00
Colin Angus Mackay4-May-05 12:00 
GeneralRe: Prob using Oracle 9i Pin
vishalmishra4-May-05 12:14
vishalmishra4-May-05 12:14 
GeneralNewbie GROUP BY question Pin
moredip4-May-05 11:01
moredip4-May-05 11:01 
GeneralRe: Newbie GROUP BY question Pin
Colin Angus Mackay4-May-05 11:36
Colin Angus Mackay4-May-05 11:36 
Okay - Before I answer your question I am going to do my usual pointer to this article on SQL Injection Attacks and How to Prevent Them[^] whenever I see code that could potentially be attacked.

On to your question. What you need to do is take the statement you have already and use that as a subquery and have the table join onto itself via the subquery. It sounds more difficult than it is.

SELECT main.ActivityID, main.PercentComplete, main.WhenComplete
FROM Activity_Session_Summary AS main
INNER JOIN (SELECT ActivityID, MAX(WhenCompleted) AS WhenComplete
            FROM Activity_Session_Summary
            WHERE StudentID = %s AND ActivityID IN ( %s )
            GROUP BY ActivityID) AS sub
ON main.ActivityID = sub.ActivityID AND main.WhenComplete = sub.WhenComplete
WHERE StudentID = '%s' AND ActivityID IN ( %s )


I've left your format placeholders even though they represent a potential security risk.

Your original query has now been made the subquery and as you can see, nothing has changed there except that I named the aggregate column so I could refer to it later.

The main query selects the data that you want and joins onto the subquery. You will notice from the join that it is joined on the result of the aggregate as well as the ActivityID (I'm guessing that is a foreign key onto another table). Finally, your where clause is repeated on the main query, although it may not be needed - I don't know enough about your data model to make that determination. If it isn't needed it won't do any harm.

Does this help?


My: Blog | Photos | Next SQL Presentation
WDevs.com - Open Source Code Hosting, Blogs, FTP, Mail and More


GeneralRe: Newbie GROUP BY question Pin
moredip4-May-05 11:50
moredip4-May-05 11:50 
GeneralRe: Newbie GROUP BY question Pin
Colin Angus Mackay4-May-05 11:57
Colin Angus Mackay4-May-05 11:57 
GeneralRe: Newbie GROUP BY question Pin
moredip4-May-05 12:11
moredip4-May-05 12:11 
GeneralRe: Newbie GROUP BY question Pin
Colin Angus Mackay4-May-05 12:13
Colin Angus Mackay4-May-05 12:13 
GeneralAverage formula with SQL table Pin
Joey Picerno4-May-05 10:20
Joey Picerno4-May-05 10:20 
GeneralRe: Average formula with SQL table Pin
Colin Angus Mackay4-May-05 11:42
Colin Angus Mackay4-May-05 11:42 
GeneralRe: Average formula with SQL table Pin
Joey Picerno4-May-05 15:33
Joey Picerno4-May-05 15:33 
GeneralRe: Average formula with SQL table Pin
Joey Picerno4-May-05 15:45
Joey Picerno4-May-05 15:45 
GeneralRe: Average formula with SQL table Pin
Colin Angus Mackay4-May-05 19:33
Colin Angus Mackay4-May-05 19:33 
GeneralRe: Average formula with SQL table Pin
Joey Picerno5-May-05 3:51
Joey Picerno5-May-05 3:51 
GeneraltactDatagrid with VB.net and SQL Pin
gingesh4-May-05 8:35
gingesh4-May-05 8:35 
GeneralRe: tactDatagrid with VB.net and SQL Pin
keith maddox4-May-05 10:31
keith maddox4-May-05 10:31 
GeneralBulk Copy Tables from .net to sql Pin
Mtognetti4-May-05 4:51
Mtognetti4-May-05 4:51 
GeneralRe: Bulk Copy Tables from .net to sql Pin
MBGeorge4-May-05 20:45
MBGeorge4-May-05 20:45 
GeneralOracleHelper for testing Pin
Nizar Abdeljaoued4-May-05 4:17
Nizar Abdeljaoued4-May-05 4:17 
GeneralDumb Q: C++, a DB and regional settings Pin
CherezZaboro4-May-05 3:50
CherezZaboro4-May-05 3:50 
GeneralRe: Dumb Q: C++, a DB and regional settings Pin
Colin Angus Mackay4-May-05 4:14
Colin Angus Mackay4-May-05 4:14 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.