Question : Adapting an existing query to include counts & maxrows

Hi Folks,

I have an existing query, which I previously had help putting together on here (http://www.experts-exchange.com/Web_Development/Web_Languages-Standards/Cold_Fusion_Markup_Language/Q_26424862.html).  This query works great and allows me to get both the primary result (the 'status') and the secondary result ('the comments') with just one query.  I've included by output code so you can see how it's used.

I'm now looking to take this a step further with 2 additions;

Firstly, I want to restrict the number of maxrows returned - but applying that to the primary only (the 'status').  If I simply add a maxrows to the query, it includes comments within that (so, if I put maxrows="10" and the first status has 9 comments, I only get one status (plus the 9 comments) returned, rather than 10 statuses and their comments.

Secondly, I'd like to add a 'count' for the number of comments for each status.  I've considered a few options here, such as using a cfset to add during the loop, but I'm not convinced that is the best route.

Perhaps I'm asking too much to be able to do all of these within a single query?
1:
2:
3:
4:
5:
6:
7:
8:
<cfquery name="livestream" datasource="#datasource#">
SELECT s.sID, s.wUserID, s.sStatus, s.sCreatedDate, u.FirstName, u.Surname, c.cComment, c.wUserID, c.cDate, cu.FirstName AS cFirstName, cu.Surname AS cSurname
FROM statusupdates s
LEFT JOIN users u  ON s.wUserID=u.wUserID
LEFT JOIN comments c ON c.inreplyto = s.sID
LEFT JOIN users cu ON c.wUserID=cu.wUserID
ORDER BY s.sCreatedDate DESC, s.sID
</cfquery>
1:
2:
3:
4:
5:
6:
7:
8:
9:
<cfoutput query="livestream" group="sID">
#FirstName# #Surname# #sStatus#
   <!--- Comment(s) --->
   <cfoutput>
   <cfif len(trim(cComment))>  
   #cFirstname# #cSurname# #cComment#
   </cfif>
   </cfoutput>
</cfoutput>

Answer : Adapting an existing query to include counts & maxrows

>>. Firstly, I want to restrict the number of maxrows returned

Ideally that's something that should be done at the db level.  It could also be done in CF code. But there's no point incurring the overhead of pulling back data that won't be used.  

You could probably use a derived table to limit the results in SQL. In other words, select the TOP 10 updates first, then JOIN to the other tables.  The exact syntax and structure is database dependent.  MS SQL use TOP, MySQL uses LIMIT, etc....  

Also, any "top x" records scenario involves ordering the records by "some column".   I'm assuming you want the latest 10 updates...?  Some databases restrict what operations you can perform in a derived table. So it's very likely your actual sql will need tweaking...

<!---
Not tested.  Psuedo SQL ....
--->
SELECT s.sID, s.wUserID, s.sStatus, .....Other Columns ...
FROM    <!--- this is a derived table to grab the latest 10 updates .....--->
              (
                    SELECT TOP 10 sID, wUserID, sStatus,  ......OtherColumns....
                    FROM    statusupdates
                    ORDER BY sCreatedDate DESC
             )    s
             LEFT JOIN users u  ON s.wUserID=u.wUserID
             LEFT JOIN comments c ON c.inreplyto = s.sID
             LEFT JOIN users cu ON c.wUserID=cu.wUserID
ORDER BY s.sCreatedDate DESC, s.sID

>> Secondly, I'd like to add a 'count' for the number of comments for each status.  

Do you want to display the count before OR after the comments.  If you want to display it after the comments, then using a cfset within the loop is fine. If you need to display it _before_ the comments, then obviously you'll need to know the totals ahead of time.
Random Solutions  
 
programming4us programming4us