Informix Select 并将结果输出为矩阵
Informix Select and output result as a matrix
我有一个名为 "sales_pos" 的 table,其中包含以下列:
cust_nr ; cust_name ; cust_ans ; date ; month_year ; value_goods
示例:
1234;Jon Doe;New York;31/01/2015;1/2015;250,00
4711;Max Muster;New York;22/03/2015;01/2015;900,00
0812;Will Smith;New York;22/02/2015;01/2015;300,00
1234;Jon Doe;New York;11/01/2015;1/2015;150,00
我想要一个结果如下的选择:
Customer |1/2015|2/2015|3/2015|4/2015| .. |12/2015|
0812 Will Smith New York |300,00|.. |.. |.. | .. |.. |
1234 Jon Doe New York |400,00|.. |.. |.. | .. |.. |
4711 Max Muster New York |.. |.. |900,00|..
..
Select
cust_nr, cust_name, cust_ans, month_year, sum(value_goods)
from sales_pos
group by
cust_nr, cust_name, cust_ans, month_year
这个 select-语句有我需要的所有信息,但我不知道如何将这个结果转换成上面的矩阵。
我也试过:
select
cust_nr, cust_name, cust_ans, month_year, sum(value_goods)
from sales_pos
where
month_year = '1/2015'
group by cust_nr, cust_name, cust_ans
UNION ALL
select
cust_nr, cust_name, cust_ans, month_year, sum(value_goods)
from sales_pos
where
month_year = '2/2015'
group by cust_nr, cust_name, cust_ans
UNION ALL
select
cust_nr, cust_name, cust_ans, month_year, sum(value_goods)
from sales_pos
where
month_year = '3/2015'
group by cust_nr, cust_name, cust_ans
UNION ALL
...
但这也不起作用。
我希望有人能帮忙。谢谢
如果您知道 headers 是什么,您可以使用条件聚合:
Select cust_nr, cust_name, cust_ans,
sum(case when month_year = '1/2015' then value_goods end) as MY_012015,
sum(case when month_year = '2/2015' then value_goods end) as MY_022015,
. . .
sum(case when month_year = '12/2015' then value_goods end) as MY_122015
from sales_pos
group by cust_nr, cust_name, cust_ans;
我终于找到了适用于 informix 7.5 的有效解决方案
select DISTINCT
CUST.cust_nr, CUST.cust_name, CUST.cust_ans,
(select sum(M01.value_goods) from $table as M01 where M01.month_year = '1/2015' and M01.cust_nr = CUST.cust_nr),
(select sum(M02.value_goods) from $table as M02 where M02.month_year = '2/2015' and M02.cust_nr = CUST.cust_nr),
...
(select sum(M12.value_goods) from $table as M12 where M12.month_year = '12/2015' and M12.cust_nr = CUST.cust_nr)
from sales_pos as CUST order by CUST.cust_nr;
我不喜欢这个解决方案,但至少它有效:-(
我有一个名为 "sales_pos" 的 table,其中包含以下列:
cust_nr ; cust_name ; cust_ans ; date ; month_year ; value_goods
示例:
1234;Jon Doe;New York;31/01/2015;1/2015;250,00
4711;Max Muster;New York;22/03/2015;01/2015;900,00
0812;Will Smith;New York;22/02/2015;01/2015;300,00
1234;Jon Doe;New York;11/01/2015;1/2015;150,00
我想要一个结果如下的选择:
Customer |1/2015|2/2015|3/2015|4/2015| .. |12/2015|
0812 Will Smith New York |300,00|.. |.. |.. | .. |.. |
1234 Jon Doe New York |400,00|.. |.. |.. | .. |.. |
4711 Max Muster New York |.. |.. |900,00|..
..
Select
cust_nr, cust_name, cust_ans, month_year, sum(value_goods)
from sales_pos
group by
cust_nr, cust_name, cust_ans, month_year
这个 select-语句有我需要的所有信息,但我不知道如何将这个结果转换成上面的矩阵。
我也试过:
select
cust_nr, cust_name, cust_ans, month_year, sum(value_goods)
from sales_pos
where
month_year = '1/2015'
group by cust_nr, cust_name, cust_ans
UNION ALL
select
cust_nr, cust_name, cust_ans, month_year, sum(value_goods)
from sales_pos
where
month_year = '2/2015'
group by cust_nr, cust_name, cust_ans
UNION ALL
select
cust_nr, cust_name, cust_ans, month_year, sum(value_goods)
from sales_pos
where
month_year = '3/2015'
group by cust_nr, cust_name, cust_ans
UNION ALL
...
但这也不起作用。 我希望有人能帮忙。谢谢
如果您知道 headers 是什么,您可以使用条件聚合:
Select cust_nr, cust_name, cust_ans,
sum(case when month_year = '1/2015' then value_goods end) as MY_012015,
sum(case when month_year = '2/2015' then value_goods end) as MY_022015,
. . .
sum(case when month_year = '12/2015' then value_goods end) as MY_122015
from sales_pos
group by cust_nr, cust_name, cust_ans;
我终于找到了适用于 informix 7.5 的有效解决方案
select DISTINCT
CUST.cust_nr, CUST.cust_name, CUST.cust_ans,
(select sum(M01.value_goods) from $table as M01 where M01.month_year = '1/2015' and M01.cust_nr = CUST.cust_nr),
(select sum(M02.value_goods) from $table as M02 where M02.month_year = '2/2015' and M02.cust_nr = CUST.cust_nr),
...
(select sum(M12.value_goods) from $table as M12 where M12.month_year = '12/2015' and M12.cust_nr = CUST.cust_nr)
from sales_pos as CUST order by CUST.cust_nr;
我不喜欢这个解决方案,但至少它有效:-(