views:

283

answers:

4

Hello! I'm hitting the DB for a 100 records from a MySQL DB that have a DateAdded (timestamp) column.

I'd like to find a SMART way to display the records as follows in Coldfusion, w/o hitting the database multiple times.

Today: - records..... Yesterday: - records..... Earlier This Week: - records..... Earlier This Month: - records..... Older: - records.....

Thanks for the ideas on how to get this done smartly in ColdFusion.

A: 

multiple db calls would have a pretty negligible cost in most apps

but, this can all be done in one call, if you selet all the records and order by time desc.

end the current section when the time switches (when it becomes older than 1 day, older than 2 days, older than 8 days, etc)

basically you would use DateCompare function in CF to compare DateAdd to that section's cut-off point. some pseudocode:

today

while dateadd < tomorrow print record

yesterday

while dateadd < 1 week ago print record

earlier this week

while dateadd < 1 month ago print record

jspcal
How can I make the switch in Coldfusion elegantly?
AnApprentice
You could make the "switch" by keeping a variable that keeps track of the section you are on. When that section no longer matches the "LastSection", create the next header.
Neil N
Neil can you provide an example in CF that shows how that looping would work?
AnApprentice
+1  A: 

you'd have to start on the SQL Side, either a Store Procedure or a User Defined function to add the Today/Yesterday/etc label to each record.

Perhaps:

Select a, b, myFunction(DateColumn) as GroupLabel 
From myTable Order By GroupLabel

Where myFunction takes the date value and returns Today/Yesterday/etc

Neil N
Why was this downvoted?
Neil N
This is a great approach, and will also work for any ad-hoc reports designed directly against the database.
Antony
+2  A: 

Query of Queries? - http://livedocs.adobe.com/coldfusion/8/htmldocs/using_recordsets_3.html#1157970

<h1>Today:</h1>
<cfquery name="todayRecords" dbtype="query">
    select *
    from originalQuery
    where DateAdded = #createODBCDate(year(now()), month(now()), day(now()))#
</CFQUERY>
<cfdump var="#todayRecords#">

You may want to use the BETWEEN SQL operator. See: http://livedocs.adobe.com/coldfusion/8/htmldocs/using_recordsets_7.html

You may also use <cfqueryparam> tag. See: http://livedocs.adobe.com/coldfusion/8/htmldocs/Tags_p-q_18.html#1102474

Query of Queries might not be the most efficient, but in my opinion it is the cleanest way to represent your intent and it just works.

Henry
If the column really is a timestamp, and contains both a date and time, then a straight equality comparison will not work. Either a range check (or truncating the time) would be neededWHERE DateAdded <= #TodayAtMidnight# AND DateAdded < #TomorrowAtMidnight#
Leigh
The edit function does not seem to be working. Correction: WHERE DateAdded >= #TodayAtMidnight# AND DateAdded < #TomorrowAtMidnight#
Leigh
@Leigh, yes, and you may use BETWEEN
Henry
@Henry - Yes, it can be done with BETWEEN. But it is easier to use >= and < IMO. Since BETWEEN is _inclusive_, it will include a little more information than is desired (if not constructed correctly).
Leigh
A: 

I agree with Neil. If it were me I would do it all in a Stored Proc and just let the DB hand CF the final results for output.

Eric