[英]sql query to return display by month
I am trying to generate a query which returns results based on month.我正在尝试生成一个基于月份返回结果的查询。
For example: table1例如:表 1
Name Amount createDate
p1 100 01/01/2012
p1 50 02/01/2012
p1 200 03/01/2012
p3 100 04/01/2012
The query should return:查询应返回:
Name Jan Feb Mar April
p1 100 50 200 0
p2 0 0 0 100
Any suggestion or any idea of how to do it.关于如何做的任何建议或想法。
I am using db2 database.Sorry for missing the details我正在使用 db2 数据库。很抱歉遗漏了细节
Something like this might work...这样的事情可能会奏效......
select
name,
case when month(createDate) = '1' then amount end as Jan,
case when month(createDate) = '2' then amount end as Feb
--etc...
from
table1
group by
name
;
However, the date equality test may not pass muster.但是,日期相等性测试可能无法通过。 Like the others said, you'll probably have to look up date functions and find the one that suits your needs.
就像其他人所说的那样,您可能需要查找日期函数并找到适合您需要的函数。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.