简体   繁体   中英

sql query to return display by month

I am trying to generate a query which returns results based on month.

For example: table1

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

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM