mysql - how to select and sum directly preceded related rows -


i have table of bills payable ..

i select data (if sd_doc null) retrieves invoice price it

however, if sd_doc not null retrieves previous null sd_doc, , sum prices of rows together

note : want empty months directly preceded not null sd_doc

my table , sampledata

create table if not exists `d_statistics_docs` (   `sd_id` int(11) not null auto_increment,   `sd_pub` int(11) not null,   `sd_date` date not null,   `sd_doc` text not null,   `price` int(11) not null,   primary key (`sd_id`) ) engine=myisam  default charset=utf8 auto_increment=16 ;  -- -- dumping data table `d_statistics_docs` --  insert `d_statistics_docs` (`sd_id`, `sd_pub`, `sd_date`, `sd_doc`, `price`) values (9, 256, '2013-03-01', 'scifiwallpaper1.jpg', 5), (10, 256, '2013-04-01', 'scifiwallpaper1.jpg', 15), (11, 256, '2013-05-01', '', 3), (12, 256, '2013-06-01', 'scifiwallpaper1.jpg', 7), (13, 256, '2013-07-01', '', 9), (14, 256, '2013-08-01', '', 11), (15, 256, '2013-09-01', 'scifiwallpaper1.jpg', 25); 

the result need that

9   256     2013-03-01  scifiwallpaper1.jpg 5  10  256     2013-04-01  scifiwallpaper1.jpg 15  11  256     2013-05-01                       3  12  256    2013-06-01   scifiwallpaper1.jpg 10  // total 7 + 3   13  256     2013-07-01                      9  14  256     2013-08-01                      11  15  256     2013-09-01  scifiwallpaper1.jpg 45  // total of 25+11+9 

you had said null, you're setting sd_doc spaces programmed that

select    *  , case    when d.sd_doc <> ''    ifnull   (     ( select sum(d2.price)       d_statistics_docs d2       d2.sd_id >             ( select max(sd_id)               d_statistics_docs d3               sd_doc <> ''                 , d3.sd_id < d.sd_id              )         , d2.sd_id <= d.sd_id     )   , d.price     )   else d.price   end  totprev d_statistics_docs d 

Comments

Popular posts from this blog

c++ - CryptStringToBinary API behavior -

c++ - Correct method for redrawing a layered window -

java.util.scanner - How to read and add only numbers to array from a text file -