mysql - Select Query From SQL group by weekly reports -
i used following query select group records.
select yearweek(dtentered) week, sum(leftcount) lct, sum(rightcount) rct `dailycount` paid='1' , regid='sf00033200712' group yearweek(dtentered) "
but getting current week reports only. want show weekly reports. don't know how change this.
create table if not exists `dailycount` ( `countid` int(11) not null auto_increment, `regid` varchar(13) not null, `paid` int(11) not null, `dtentered` date not null, `leftcount` int(11) not null, `rightcount` int(11) not null, `carryleft` int(11) not null, `carryright` int(11) not null, `total_pairs` int(11) not null, `gross` int(11) not null, `service` decimal(19,2) not null default '0.00', `net` decimal(19,2) not null default '0.00', primary key (`countid`) ) engine=myisam default charset=latin1 auto_increment=33 ; insert `dailycount` (`countid`, `regid`, `paid`, `dtentered`, `leftcount`, `rightcount`, `carryleft`, `carryright`, `total_pairs`, `gross`, `service`, `net`) values (6, 'sf00033200712', 1, '2013-05-14', 0, 1, 0, 0, 0, 0, 0.00, 0.00), (5, 'sf00034140513', 1, '2013-05-14', 0, 1, 0, 0, 0, 0, 0.00, 0.00), (4, 'sf00033200712', 1, '2013-05-14', 0, 1, 0, 0, 0, 0, 0.00, 0.00), (7, 'sf00035140513', 1, '2013-05-14', 0, 1, 0, 0, 0, 0, 0.00, 0.00), (8, 'sf00034140513', 1, '2013-05-14', 0, 1, 0, 0, 0, 0, 0.00, 0.00), (9, 'sf00033200712', 1, '2013-05-14', 0, 1, 0, 0, 0, 0, 0.00, 0.00), (10, 'sf00033200712', 1, '2013-05-14', 1, 0, 0, 0, 0, 0, 0.00, 0.00), (11, 'sf00034140513', 1, '2013-05-14', 1, 0, 0, 0, 0, 0, 0.00, 0.00), (12, 'sf00033200712', 1, '2013-05-14', 0, 1, 0, 0, 0, 0, 0.00, 0.00), (13, 'sf00037140513', 1, '2013-05-14', 0, 1, 0, 0, 0, 0, 0.00, 0.00), (14, 'sf00033200712', 1, '2013-05-14', 1, 0, 0, 0, 0, 0, 0.00, 0.00), (15, 'sf00039140513', 1, '2013-05-14', 0, 1, 0, 0, 0, 0, 0.00, 0.00), (16, 'sf00037140513', 1, '2013-05-14', 0, 1, 0, 0, 0, 0, 0.00, 0.00), (17, 'sf00033200712', 1, '2013-05-14', 1, 0, 0, 0, 0, 0, 0.00, 0.00), (18, 'sf00037140513', 1, '2013-05-14', 1, 0, 0, 0, 0, 0, 0.00, 0.00), (19, 'sf00033200712', 1, '2013-05-14', 1, 0, 0, 0, 0, 0, 0.00, 0.00), (20, 'sf00041140513', 1, '2013-05-14', 0, 1, 0, 0, 0, 0, 0.00, 0.00), (21, 'sf00037140513', 1, '2013-05-14', 1, 0, 0, 0, 0, 0, 0.00, 0.00), (22, 'sf00033200712', 1, '2013-05-14', 1, 0, 0, 0, 0, 0, 0.00, 0.00), (23, 'sf00039140513', 1, '2013-05-14', 1, 0, 0, 0, 0, 0, 0.00, 0.00), (24, 'sf00037140513', 1, '2013-05-14', 0, 1, 0, 0, 0, 0, 0.00, 0.00), (25, 'sf00033200712', 1, '2013-05-14', 1, 0, 0, 0, 0, 0, 0.00, 0.00), (26, 'sf00041140513', 1, '2013-05-22', 1, 0, 0, 0, 0, 0, 0.00, 0.00), (27, 'sf00037140513', 1, '2013-05-22', 1, 0, 0, 0, 0, 0, 0.00, 0.00), (28, 'sf00033200712', 1, '2013-05-22', 1, 0, 0, 0, 0, 0, 0.00, 0.00), (29, 'sf00042140513', 1, '2013-05-15', 1, 0, 0, 0, 0, 0, 0.00, 0.00), (30, 'sf00041140513', 1, '2013-05-15', 0, 1, 0, 0, 0, 0, 0.00, 0.00), (31, 'sf00037140513', 1, '2013-05-15', 1, 0, 0, 0, 0, 0, 0.00, 0.00), (32, 'sf00033200712', 1, '2013-05-15', 1, 0, 0, 0, 0, 0, 0.00, 0.00);
query:
select yearweek(dtentered) week, sum(leftcount) lct, sum(rightcount) rct `dailycount` paid='1' , regid='sf00033200712' group yearweek(dtentered)
result:
| week | lct | rct | ---------------------- | 201319 | 7 | 4 | | 201320 | 1 | 0 |
query:
select week(dtentered) week, sum(leftcount) lct, sum(rightcount) rct, concat(date_format(date_add(dtentered, interval(1-dayofweek(dtentered)) day),'%y-%m-%e'), ' ', date_format(date_add(dtentered, interval(7-dayofweek(dtentered)) day),'%y-%m-%e')) daterange `dailycount` paid='1' , regid='sf00033200712' group yearweek(dtentered)
result:
| week | lct | rct | daterange | ----------------------------------------------- | 19 | 7 | 4 | 2013-05-12 2013-05-18 | | 20 | 1 | 0 | 2013-05-19 2013-05-25 |
Comments
Post a Comment