Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

SQL to summarize data by month

Status
Not open for further replies.

goodkarma

Programmer
Aug 28, 2003
17
US
I am trying to create and insert statement that looks at data for a month and sums it by dept, area and month. I want to have it run automatically on the first of every month for data that is 60 days ago. How do I do that so that I get all the data for months that have 31 days?? I was going to go something like this
reporting_date >= sysdate - 60 and reporting_date <= sysdate - 30. But I don't think that will get it all. My date in the new table is going to be YYYY-MM. Is there a way to take the month out of the sysdate and then subtract 2 from that and only look at the dates that have that specific month? I'm probably not explaining myself very well so if you have any questions let me know!!

I am using an Oracle database with SQLPlus.
Any help would be GREATLY appreciated.
Thanks
 
First, this is a forum for the MySQL database, so solutions posted here might not work on an Oracle system. You could check out the Oracle forum (there has to be one!) instead.

However, in MYSQL, if it's a standard DATE field that you are checking, you could use:[tt]
WHERE
datefld >= DATE_SUB(CURDATE(),INTERVAL 2 MONTH)
AND datefld < DATE_SUB(CURDATE(),INTERVAL 1 MONTH)[/tt]

If it's possibly after the 1st of the month, you could use:[tt]
WHERE
datefld >= DATE_FORMAT(DATE_SUB(CURDATE(),INTERVAL 2 MONTH),'%Y-%m-01')
AND datefld < DATE_FORMAT(DATE_SUB(CURDATE(),INTERVAL 1 MONTH),'%Y-%m-01')
[/tt]



-----
ALTER world DROP injustice, ADD peace;
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top