Showing a cumlative total by month
От | David Nelson |
---|---|
Тема | Showing a cumlative total by month |
Дата | |
Msg-id | CANxyCUGMBeJqKo77AGNEvYsSSHZtYGE8RsVQBVJHMx4H2p-Sbg@mail.gmail.com обсуждение исходный текст |
Ответы |
Re: Showing a cumlative total by month
|
Список | pgsql-sql |
<div dir="ltr">Good morning all,<br /><br />We have a system that maintains information about files that have been uploadedto our system. I have a view that shows for each month the total size of all files submitted tht month. I would liketo add a running total column to the view, but can't quite get there. I figured out how to show the information I wantusing a CTE, but I can not figure out how to translate that to a single query to define a view with. So I'm hoping someonecan point me in the right direction.<br /><br />SELECT VERSION();<br /> version <br />----------------------------------------------------------------------------------------------------------------<br /> PostgreSQL9.1.18 on x86_64-unknown-linux-gnu, compiled by gcc (GCC) 4.4.7 20120313 (Red Hat 4.4.7-11), 64-bit<br /><br/>DROP TABLE uploaded_files;<br /><br />CREATE TABLE uploaded_files<br />(<br /> file_id SERIAL NOT NULL,<br /> upload_date TIMESTAMP WITH TIME ZONE NOT NULL,<br /> upload_size BIGINT NOT NULL,<br /> PRIMARY KEY (file_id)<br />);<br /><br />This test case is populatedwith a handful of random records (659), and the following CTE seems to extract the information I am after (stillnot positive I have the date down exactly, but I can deal with that):<br /><br />WITH upload_summary AS<br />(<br /> SELECT EXTRACT(YEAR FROM upload_date) AS up_year,<br /> LPAD(CAST(EXTRACT(MONTH FROM upload_date) AS TEXT),2, '0') AS up_month,<br /> SUM(upload_size) AS monthly_total<br /> FROM uploaded_files<br /> GROUP BYup_month, up_year<br />)<br />SELECT u.up_year,<br /> u.up_month,<br /> u.monthly_total,<br /> (SELECTSUM(monthly_total)<br /> FROM upload_summary<br /> WHERE CAST(CONCAT(up_year,<br /> '-',<br /> up_month,<br /> '-01 00:00:00-05')<br /> AS TIMESTAMP) <=<br /> CAST(CONCAT(u.up_year,<br /> '-',<br /> u.up_month,<br /> '-01 00:00:00-05')<br /> AS TIMESTAMP)<br/> ) AS cumulative<br />FROM upload_summary u<br />ORDER BY up_year, up_month;<br /><br /> up_year | up_month| monthly_total | cumulative <br />---------+----------+----------------+----------------<br /> 2014 | 01 | 3179135699 | 3179135699<br /> 2014 | 02 | 1634499060 | 4813634759<br /> 2014 | 03 | 4278982588 | 9092617347<br /> 2014 | 04 | 142238544857 | 151331162204<br /> 2014 | 05 | 357240707209 | 508571869413<br /> 2014 | 06 | 64339859968 | 572911729381<br /> 2014 | 07 | 25513017728 | 598424747109<br /> 2014 | 08 | 11020669492 | 609445416601<br /> 2014 | 09 | 1018774598 | 610464191199<br /> 2014 | 10 | 76752535951 | 687216727150<br /> 2014 | 11 | 4611404964 | 691828132114<br /> 2014 | 12 | 69607199452 | 761435331566<br /> 2015 | 01 | 15700525 | 761451032091<br /> 2015 | 02 | 34234715981 | 795685748072<br /> 2015 | 03 | 1484150449194| 2279836197266<br /> 2015 | 04 | 13096208914706 | 15376045111972<br /> 2015 | 05 | 814385166 | 15376859497138<br /> 2015 | 06 | 563829413 | 15377423326551<br /> 2015 | 07 | 656007272| 15378079333823<br /> 2015 | 08 | 1828956674258 | 17207036008081<br /> 2015 | 09 | 822601322| 17207858609403<br /><br /><br /><br />The basic view definition gives the first three columns (date fields concatenatedinto one):<br />SELECT CONCAT(EXTRACT(YEAR FROM upload_date),<br /> '-',<br /> LPAD(CAST(EXTRACT(MONTHFROM upload_date) AS TEXT), 2, '0')) AS mon,<br /> SUM(upload_size) AS monthly_total<br />FROMuploaded_files u<br />GROUP BY EXTRACT(MONTH FROM upload_date), EXTRACT(YEAR FROM upload_date)<br />ORDER BY EXTRACT(YEARFROM upload_date), EXTRACT(MONTH FROM upload_date);<br /><br /> mon | monthly_total<br />---------+----------------<br/> 2014-01 | 3179135699<br /> 2014-02 | 1634499060<br /> 2014-03 | 4278982588<br/> 2014-04 | 142238544857<br /> 2014-05 | 357240707209<br /> 2014-06 | 64339859968<br /> 2014-07 | 25513017728<br /> 2014-08 | 11020669492<br /> 2014-09 | 1018774598<br /> 2014-10 | 76752535951<br /> 2014-11| 4611404964<br /> 2014-12 | 69607199452<br /> 2015-01 | 15700525<br /> 2015-02 | 34234715981<br/> 2015-03 | 1484150449194<br /> 2015-04 | 13096208914706<br /> 2015-05 | 814385166<br /> 2015-06 | 563829413<br /> 2015-07 | 656007272<br /> 2015-08 | 1828956674258<br /> 2015-09 | 822601322<br /><br /><br/>I just can't figure out how to specify the summation of the file sizes through each month in a static query. Any suggestions?<br/><br />Thanks,<br />David</div>
В списке pgsql-sql по дате отправления: