Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9077707
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T19:26:18+00:00 2026-06-16T19:26:18+00:00

I have date time field called transaction_date, in a report i need to select

  • 0

I have date time field called transaction_date, in a report i need to select last calendar month, how do i do this ? (this should work for a month like January too)

I came up with following but this only works if the month is NOT january,

SELECT SUM(amount)  AS pay_month FROM `users_payment`  WHERE  MONTH(transaction_datetime)= MONTH(NOW()) AND YEAR(transaction_datetime)=YEAR(NOW())

there are lot of examples using INTERVAL functions but this only select the time interval not the calendar month as i wanted too..

like

SELECT SUM(amount)  AS `year_month` FROM `users_payment`   WHERE  DATE_ADD(NOW(), INTERVAL -1 MONTH) < transaction_datetime 

but this is not what i want, i want to select sales sum of the DECEMBER only last year (remember there are other years too in the table which i dont want i.e 1979, 1981…etc)

same report next section, i need to select last 2 calender months, I dont know have any idea on how to do this too.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-16T19:26:19+00:00Added an answer on June 16, 2026 at 7:26 pm

    Have you tried the following

    SELECT SUM(amount)  AS `year_month` FROM `users_payment`
    WHERE MONTH(DATE_ADD(NOW(), INTERVAL -1 MONTH)) = MONTH(transaction_datetime)
    

    The above should work to show previous month; it does not distinguish between years however.

    On second thought, I see what you are trying to do – To get all the transactions for a given month. Try something like this instead.

    SELECT SUM(amount)  AS `year_month` FROM `users_payment`
    WHERE transaction_datetime BETWEEN date_format(NOW() - INTERVAL 1 MONTH, '%Y-%m-01')
       AND last_day(NOW() - INTERVAL 1 MONTH)
    

    This will list all the transactions for the previous calendar month. Alter the INTERVAL values to select multiple months.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

i have a date/time field called MyDate. Originally, its format is mm/dd/yyyy and there
I have a field called dPostTime , which has the date and time stored
I have a table in SQL Server 2005 which has a date time field.
I have table that contain date and time field. id|date|time ========= 1|01/01/2001|10:45 2|01/02/2002|11:45 3|01/03/2003|12:45
I have a sqlite3 database with a datetime field, the format of the date/time
I have Database with date field. I see the time like: 1900-01-01 13:38:00.000 How
I have a MySQL table where there is a 'date_added' (date) field, 'time_added' (time)
Currently i have a column called date as varchar instead of date time. the
I have a custom UserType which stores a date/time value in a TIMESTAMP field
I have numerous documents containing a field called date which is simply a unix

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.