|
我有一个column在SQL table。其中一个date交货单。! E& B9 g7 P: w' x( A
因此,可以重复相同的日期(我们一天下几个订单),如下所示:
. D# C1 p; O! Y3 }' A& V2 H05-01-1605-01-1605-01-1608-01-1608-01-1614-01-1622-01-1622-01-1604-02-1605-02-1605-02-16我想计算每篇文章的营业额AVG each 6 months,我将进一步解释:; l- {) p% Y/ D- f
From January to June ==> Turnover 1From Febrary to July ==> Turnover 2From March to August ==> Turnover 3From April to September ==> Turnover 4From May to Obtober ==> Turnover 5From June to November ==> Turnover 6From July to December ==> Turnover 7我已经在以下请求中提取了月份,但我无法动态计算上述示例所示的营业额(因为我的数据应该每月更改):
/ Q; L7 A4 r$ ]$ D7 bselect distinct extract (month from Article) as mt order by mt我试着用,cursor但是没有最好的解决方案。
' n" l) q- X( t g5 c) k" q- t我提出了计算以下内容的要求the first 6 months(我手动完成)每位客户的营业额:: @) H8 `5 m7 a, `, M9 x& x
select "LRU","Client",round(sum("Montant_fac_eur"))from "foundry"where "Nature"='Repair' and "Client"={{w_widget3.selectedValue}}and "annee"='2016'and extract (month from "date") between '1' and '6'group by "LRU","Client"结果如下:
P# l3 N8 b/ k+ Z8 j: p* l LRU Client round"article1" Turnover of article1 from January to June"article2" Turnover of article2 from January to June"article3" Turnover of article3 from January to June"article4" Turnover of article4 from January to June"article5" Turnover of article5 from January to June"article6" Turnover of article6 from January to June"article7" Turnover of article7 from January to June"article8" Turnover of article8 from January to June"article9" Turnover of article9 from January to June"article10" 4001 41612 Turnover of article10 from January to June我想做一个循环来计算每六个月的营业额,如果可能的话不手动写吗?有人可以帮我,给我一个解决方案或建议。我该怎么办?非常感谢。
k- T' ~5 x; R7 K. E - Q" Y7 M% o* }
解决方案: 2 ]/ {/ [" T2 y; `
在这里,您可以查看问题的简化定义和解决方案(如果我理解正确):http :% }( S0 \4 D8 T2 Q
//sqlfiddle.com/#!9/48a2e1/1. ~- n" ^8 a# W5 q
CREATE TABLE foundry( lru varchar(50) NOT NULL, client int NOT NULL, purchase_date date, price int NOT NULL);INSERT INTO foundry (lru,client,purchase_date,price) VALUES("article1",4001,"01-01-16",100),("article1",4001,"01-01-17",200),("article1",4001,"01-02-16",300),("article1",4001,"01-04-16",400),("article1",4001,"01-06-16",500),("article1",4001,"01-08-16",600),("article1",4001,"01-10-16",700),("article1",4001,"01-11-16",800),("article1",4002,"01-01-16",900),("article1",4002,"01-07-16",1000),("article1",4002,"01-12-16",1100);基本上,我们有一个包含四列的表:lru(商品名称)、客户、购买日期和一些价格。
( N/ H: T: _2 a2 e0 b0 s解决方案如下:
" Z' ^9 c; [' T: FSELECT lru,client,avg(price),COUNT(*) as total_items,MONTHNAME(STR_TO_DATE(L,'%m')) as start_month,MONTHNAME(STR_TO_DATE(R,'%m')) as end_month FROM foundry,( SELECT 1 as L,6 as R UNION ALL SELECT 2,7 UNION ALL SELECT 3、8 UNION ALL SELECT 4、9 UNION ALL SELECT 5,10 UNION ALL SELECT 6,11 UNION ALL SELECT 7,12) monthsWHERE month(purchase_date) >= L AND month(purchase_date) 这个想法是:% i% R+ F; X( @- Y
[ol]生成月的所有可能组合:1-6、2-7,…,7.126 B, L9 z+ t8 p8 H1 s+ {
产生的月组合并源数据( [8 Z4 R) d9 K1 X# }
将AVG与GROUP BY一起使用[/ol]结果是:
; {& ]9 l- P6 d6 Ilru client avg(price) total_items start_month end_montharticle 1 4001 3000 55 3001January Junearticle1 4001 4000 3 4001February Julyarticle 1 4001 5000 3 50001March Augustarticle 1 4001 5000 3 50001April Septemberarticle 1 4001 600 3 May Octoberarticle1 4001 650 4 45001 June Novemberarticle 1 4001 7000 3 July Decemberarticle1 4002 900 1 January Junearticle1 4002 1000 1 February Julyarticle1 4002 1000 1 March Augustarticle1 4002 1000 1 April Septemberarticle1 4002 1000 1 May Octoberarticle1 4002 1000 1 June Novemberarticle 1 4002 1050 2 10502July December |
|