简体   繁体   中英

Aggregating Month of Timestamps by User

I have a number of rows in a MySQL table, each with a username (string) and date (datetime) column. I am trying to aggregate the rows by month and year - so my desired output would be like so:

      Jan-17  Feb-17 ... Dec-18
User1      5       7          2
User2      3      10          6

The way I was previously aggregating dates (prior to including username) was to use sum, like so:

SUM(CASE WHEN YEAR(DATE_ADD(createdon, INTERVAL 6 month))="2017" AND MONTH(createdon)="7" THEN 1 ELSE 0 END) "Jul-17"

(the date_add is due to Financial Year starting in July) - however this just returns a value so when I try to GROUP BY username at the end of my query it errors.

Any help to achieve my desired output would be most appreciated!

There is a more easy way to have this using FROM_UNIXTIME function :

My schema SQL source :

CREATE TABLE `myTable` (
  `id` mediumint(8) unsigned NOT NULL auto_increment,
  `timestamp` varchar(255),
  `userid` mediumint default NULL,
  PRIMARY KEY (`id`)
) AUTO_INCREMENT=1;

INSERT INTO `myTable` (`timestamp`,`userid`) VALUES ("1485732487",4),("1485801735",5),("1485133400",5),("1485147453",5),("1485811775",5),("1485579678",2),("1485657366",5),("1485457462",4),("1485868162",4),("1485708628",1);
INSERT INTO `myTable` (`timestamp`,`userid`) VALUES ("1485433560",2),("1485499798",2),("1485799205",3),("1485781057",3),("1485623782",4),("1485332601",4),("1485352246",3),("1485391179",3),("1485088297",2),("1485715532",2);
INSERT INTO `myTable` (`timestamp`,`userid`) VALUES ("1485679973",5),("1485880759",2),("1485919188",4),("1485310769",1),("1485334857",5),("1485874185",3),("1485737013",4),("1485626859",4),("1485711272",5),("1485615066",5);
INSERT INTO `myTable` (`timestamp`,`userid`) VALUES ("1485431634",2),("1485395131",4),("1485865892",3),("1485608615",2),("1485814718",5),("1485246275",3),("1485684463",2),("1485817191",1),("1485598599",1),("1485780025",2);
INSERT INTO `myTable` (`timestamp`,`userid`) VALUES ("1485335277",1),("1485889093",2),("1485877980",4),("1485491321",2),("1485589418",1),("1485178400",1),("1485093496",5),("1485551672",2),("1485137077",2),("1485418586",3);
INSERT INTO `myTable` (`timestamp`,`userid`) VALUES ("1485875911",2),("1485859088",5),("1485177508",3),("1485636084",4),("1485181669",5),("1485382618",5),("1485871330",4),("1485312836",3),("1485855890",1),("1485272206",2);
INSERT INTO `myTable` (`timestamp`,`userid`) VALUES ("1485595984",5),("1485696218",1),("1485292388",3),("1485442982",4),("1485680209",4),("1485591351",1),("1485409914",5),("1485124892",5),("1485836867",3),("1485607163",1);
INSERT INTO `myTable` (`timestamp`,`userid`) VALUES ("1485852764",4),("1485815496",3),("1485254943",1),("1485216648",4),("1485389964",2),("1485876777",5),("1485265749",1),("1485789682",5),("1485078698",2),("1485917921",5);
INSERT INTO `myTable` (`timestamp`,`userid`) VALUES ("1485621171",5),("1485083221",3),("1485473782",2),("1485315997",1),("1485745357",1),("1485266659",3),("1485605084",1),("1485148324",5),("1485783207",5),("1485246009",3);
INSERT INTO `myTable` (`timestamp`,`userid`) VALUES ("1485718788",1),("1485355391",5),("1485660212",3),("1485131000",3),("1485153001",2),("1485115212",4),("1485426966",3),("1485155245",2),("1485719262",3),("1485662864",1);

My aggregate query :

SELECT
  DATE_FORMAT(FROM_UNIXTIME(`timestamp`), '%b-%Y') AS 'month_year',
  userid,
  SUM(userid) AS 'sum'
FROM 
  myTable
GROUP BY
  DATE_FORMAT(FROM_UNIXTIME(`timestamp`), '%b-%Y'), userid
ORDER BY
  month_year, userid

The result :

month_year  userid  sum
Feb-2017    4   4
Feb-2017    5   5
Jan-2017    1   18
Jan-2017    2   42
Jan-2017    3   60
Jan-2017    4   64
Jan-2017    5   115

The SQL fiddle : http://sqlfiddle.com/#!9/ca60cd/1

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM