Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
4.4k views
in Technique[技术] by (71.8m points)

sql - Getting the Monthwise count from date column in MySQL

I have a table that consists of the following data, I would like to know whether it is possible to get a Month (i.e Jan, Feb) wise count of Reservations that happened and also Month wise count for each location.

PNR Location Reservation Date Passenger Name Travel Date
PNR81239087 Mumbai 2019-10-01 12:19:00 Ram 2019-11-06 15:59:00
PNR81239090 Kerala 2019-10-01 15:18:00 Kannan 2019-12-03 19:18:00
PNR812390199 Mumbai 2019-10-01 17:19:00 Ram 2019-11-01 18:39:00

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

I think this will work for you

Month Wise Count (including all locations) :

select MONTHNAME(Reservation_Date) as Month, count(*)
from yourTable
group by MONTHNAME(Reservation_Date)

Monthwise count for each location :

select MONTHNAME(Reservation_Date) as Month, count(*), Location
from yourTable
group by MONTHNAME(Reservation_Date), Location

EDIT IN QUESTION :

Changed to Group by Year and Month in one column:

Month Wise Count (including all locations) :

select concat(MONTHNAME(Reservation_Date),'-',Year(Reservation_Date)) as Month-Year,
       count(*) as Count
from yourTable
group by concat(MONTHNAME(Reservation_Date),'-',Year(Reservation_Date))

Monthwise count for each location :

select concat(MONTHNAME(Reservation_Date),'-',Year(Reservation_Date)) as Month-Year,
       count(*) as Count, Location
from yourTable
group by concat(MONTHNAME(Reservation_Date),'-',Year(Reservation_Date)), Location

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...