2013-05-15 60 views
2

我使用以下查詢來按記錄選擇組。每週報告選擇來自SQL組的查詢

SELECT YEARWEEK(dtentered) week, sum(leftcount) as lct, sum(rightcount) as rct FROM `dailycount` WHERE paid='1' and regid='SF00033200712' GROUP BY YEARWEEK(dtentered) " 

但我只收到本週的報告。我想顯示所有的每週報告。我不知道如何改變這一點。

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 INTO `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); 
+0

我想你可以刪除[PHP]和[phpMyAdmin的]標籤,問題是直接有關SQL。如下 – Voitcus

+0

更改SQL:SELECT SUM(leftcount)作爲YEARWEEK(dtentered),LCT,總和(rightcount)作爲RCT FROM'dailycount' WHERE和付費= '1' 和REGID = 'SF00033200712' 基團由YEARWEEK(dtentered); –

回答

5

查詢:

SQLFIDDLEExample

SELECT YEARWEEK(dtentered) week, 
     sum(leftcount) as lct, 
     sum(rightcount) as rct 
FROM `dailycount` 
WHERE paid='1' and regid='SF00033200712' 
GROUP BY YEARWEEK(dtentered) 

結果:

| WEEK | LCT | RCT | 
---------------------- 
| 201319 | 7 | 4 | 
| 201320 | 1 | 0 | 

查詢:

SQLFIDDLEExample

SELECT WEEK(dtentered) week, 
     sum(leftcount) as lct, 
     sum(rightcount) as rct, 
CONCAT(DATE_FORMAT(DATE_ADD(dtentered, INTERVAL(1-DAYOFWEEK(dtentered)) DAY),'%Y-%m-%e'), ' TO ',  
DATE_FORMAT(DATE_ADD(dtentered, INTERVAL(7-DAYOFWEEK(dtentered)) DAY),'%Y-%m-%e')) AS DateRange 
FROM `dailycount` 
WHERE paid='1' and regid='SF00033200712' 
GROUP BY YEARWEEK(dtentered) 

結果:

| WEEK | LCT | RCT |    DATERANGE | 
----------------------------------------------- 
| 19 | 7 | 4 | 2013-05-12 TO 2013-05-18 | 
| 20 | 1 | 0 | 2013-05-19 TO 2013-05-25 | 
+0

謝謝,它運作良好。我只想每週計算一次。例如第一列中的19,20。我需要更多的一列來顯示日期範圍。 12-05-2013至18-05-2013,19-05-2013至25-05-2013。請幫幫我。提前致謝。 – Velmurugan

+0

我編輯答案 – Justin

+0

感謝朋友。我的問題解決了。謝謝。 – Velmurugan