Same query, multiple fields with results depending on different criteria

Spurious

Active Member
Joined
Dec 14, 2010
Messages
439
Hi all,

I got a database with Date, Message and Author.
I want a query to give me the number of messages the author has sent in 1 day, 1 week and 1 month.
Basically 3 output fields:
1. count where Date >Date(1 day ago)
2. count where Date >Date(1 week ago)
3. count where Date >Date(1 month ago)

Is it possible to express this in a single query?
 

Excel Facts

Excel Joke
Why can't spreadsheets drive cars? They crash too often!
Sure. Use IF statements to see if the Message Date field falls in your Date window (returning a one if True and a zero if False), and then make it an Aggregate Query to sum up those counts, i.e.
Code:
SELECT 
    MessageTable.Author, 
    Sum(IIf([MessageTable]![MessageDate] Between (Date()-1) And Date(),1,0)) AS 1DayAgo, 
    Sum(IIf([MessageTable]![MessageDate] Between (Date()-7) And Date(),1,0)) AS 1WeekAgo, 
    Sum(IIf([MessageTable]![MessageDate] Between (Date()-30) And Date(),1,0)) AS 1MonthAgo
FROM 
    MessageTable
GROUP BY 
    MessageTable.Author;
 
Upvote 0
Thank you! Works perfectly.

Do you know by any chance, if this works in a MySQL database as well or if I have to find different ways there?
 
Upvote 0
I have worked with Access and SQL, but not MySQL. The concepts are all pretty much the same, though sometimes the syntax is slightly different. For example, DATE() in Access returns the current date, but SQL uses GETDATE(), which also returns a time component.

But Aggregate and Nested Queries do exist in all three, so the structure should be the same.
 
Upvote 0
One thing is that MySQL doesn't have IIf, the equivalent of that is CASE.
 
Upvote 0

Forum statistics

Threads
1,215,942
Messages
6,127,807
Members
449,408
Latest member
Bharathi V

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top