交叉表功能和日期PostgreSQL

时间:2016-12-06 08:54:55

标签: postgresql crosstab

我必须从Query创建一个交叉表,其中日期将更改为列名。这些订单日期可以根据查询中传递的日期增加或减少。订单日期采用Unix格式,并改为普通格式。

查询如下:

Select cd.cust_id
     , od.order_id
     , od.order_size
     , (TIMESTAMP 'epoch' + od.order_date * INTERVAL '1 second')::Date As order_date
From consumer_details cd,
     consumer_order od,
Where cd.cust_id = od.cust_id
  And od.order_date Between 1469212200 And 1469212600
Order By od.order_id, od.order_date

表如下:

 cust_id   | order_id       |    order_size | order_date   
-----------|----------------|---------------|--------------
 210721008 | 0437756        |          4323 | 2016-07-22   
 210721008 | 0437756        |          4586 | 2016-09-24   
 210721019 | 10749881       |             0 | 2016-07-28   
 210721019 | 10749881       |             0 | 2016-07-28   
 210721033 | 13639          |       2286145 | 2016-09-06   
 210721033 | 13639          |       2300040 | 2016-10-03   

结果将是:

 cust_id   | order_id       |    2016-07-22 |    2016-09-24 |    2016-07-28 |    2016-09-06 |    2016-10-03 
-----------|----------------|---------------|---------------|---------------|---------------|---------------
 210721008 | 0437756        |          4323 |          4586 |               |               |               
 210721019 | 10749881       |               |               |             0 |               |               
 210721033 | 13639          |               |               |               |       2286145 |       2300040 

0 个答案:

没有答案