我怎样才能在几个月内分组日期(一月,二月,三月等)(How can i group dates in months (Jan, Feb, March, etc.))

编程入门 行业动态 更新时间:2024-10-27 15:29:29
我怎样才能在几个月内分组日期(一月,二月,三月等)(How can i group dates in months (Jan, Feb, March, etc.))

你好,我有一个这样的桌子

+-------+------------+----------+---------+ | name | date | quantity | price | +-------+------------+----------+---------+ | art1 | 2017-01-05 | 10 | 5543.32 | +-------+------------+----------+---------+ | art2 | 2017-01-15 | 16 | 12.56 | +-------+------------+----------+---------+ | art3 | 2017-02-08 | 5 | 853.65 | +-------+------------+----------+---------+ | art4 | 2017-03-06 | 32 | 98.65 | +-------+------------+----------+---------+

我想展示这些信息

+-------+------------+----------+---------+ | name | January | February | March | +-------+------------+----------+---------+ | art1 | 5543.32 | 0 | 0 | +-------+------------+----------+---------+ | art2 | 12.56 | 0 | 0 | +-------+------------+----------+---------+ | art3 | 0 | 853.65 | 0 | +-------+------------+----------+---------+ | art4 | 0 | 0 | 98.65 | +-------+------------+----------+---------+

我怎样才能做到这一点? 我在sql中有点生疏。 谢谢

Hi i got 1 table like this

+-------+------------+----------+---------+ | name | date | quantity | price | +-------+------------+----------+---------+ | art1 | 2017-01-05 | 10 | 5543.32 | +-------+------------+----------+---------+ | art2 | 2017-01-15 | 16 | 12.56 | +-------+------------+----------+---------+ | art3 | 2017-02-08 | 5 | 853.65 | +-------+------------+----------+---------+ | art4 | 2017-03-06 | 32 | 98.65 | +-------+------------+----------+---------+

And i want to show this information

+-------+------------+----------+---------+ | name | January | February | March | +-------+------------+----------+---------+ | art1 | 5543.32 | 0 | 0 | +-------+------------+----------+---------+ | art2 | 12.56 | 0 | 0 | +-------+------------+----------+---------+ | art3 | 0 | 853.65 | 0 | +-------+------------+----------+---------+ | art4 | 0 | 0 | 98.65 | +-------+------------+----------+---------+

How can i do that? I'm a bit rusty in sql. Thanks

最满意答案

我会使用条件聚合:

select name, sum(case when date >= '2017-01-01' and date < '2017-02-01' then price else 0 end) as january, sum(case when date >= '2017-02-01' and date < '2017-03-01' then price else 0 end) as february, sum(case when date >= '2017-03-01' and date < '2017-04-01' then price else 0 end) as march from t group by name;

I would use conditional aggregation:

select name, sum(case when date >= '2017-01-01' and date < '2017-02-01' then price else 0 end) as january, sum(case when date >= '2017-02-01' and date < '2017-03-01' then price else 0 end) as february, sum(case when date >= '2017-03-01' and date < '2017-04-01' then price else 0 end) as march from t group by name;

更多推荐

本文发布于:2023-08-04 15:51:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1417843.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:几个   月内   日期   group   dates

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!