如何使用交叉表在适当的列中获取值?
How to use crosstab to get values in proper columns?
我有一个交叉表 SQL,但我没有得到预期的输出结果。我得到了第一列中的所有值。这是我的 sql:
select * from crosstab
('select p.name::text as product,
pc.name as prod_cat,
sum( ms.qtyonhand) as total_stock from adempiere.m_product p
join adempiere.m_product_category pc on p.m_product_category_id=pc.m_product_category_id
join adempiere.m_storage ms on ms.m_product_id=p.m_product_id
group by prod_cat,product order by 3 desc') as ct
(product text,
"ELECTRICAL & ELECTRONIC ITEMS" numeric,
"ACADEMICS BOOKS" numeric,
"Standard" numeric,
"FOOD AND BEVERAGES" numeric,
"Possibly Product Category" numeric,
"Pharmacy Medicine" numeric,
"COMPUTER & ACCESSORIES" numeric)
limit 10
这是我得到的输出:http://i.stack.imgur.com/w6gc5.png
这是我正在寻找的输出:
product | Electricals | Electronics | Food & Beverages | Cosmetics | Hardwares
---------------------|-------------|---------------|-------------------|------------|-------------
Samsung-WM | | 4552 | | |
Videocon-Refridge | | 1254 | | |
Philips-CFL Bulbs | 5677 | | | |
Head&shoulder Shampoo| | | | 4567 |
Candysweet | | | 5678 | |
Icecreams | | | 6785 | |
Paints | | | | | 9876
Taps | | | | | 10987
Electrical wires | 18796 | | | |
如何修改我的查询以获得正确的结果?
您应该将函数与两个查询一起使用:crosstab(text source_sql, text category_sql)
。
在第二个查询中 select 所有可能的类别值按所需顺序排列。
简单示例 - 我的家庭预算:
create table expenses (subject text, person text, cost int);
insert into expenses values
('dress', 'wife', 200),
('toy', 'son', 100),
('beer', 'me', 50);
总结:
select *
from crosstab (
$ct$
select * from expenses
order by 1, 2
$ct$
) as ct (subject text, wife int, son int, me int);
subject | wife | son | me
---------+------+-----+----
beer | 50 | |
dress | 200 | |
toy | 100 | |
(3 rows)
嗯,这不是我能相信的。
当查询产生有漏洞的数据时,你应该告诉crosstab()
它如何识别哪个value
属于哪个category
:
select *
from crosstab (
$ct$
select * from expenses
order by 1, 2
$ct$,
$ct$
select person from expenses
order by 1 desc
$ct$
) as ct (subject text, wife int, son int, me int);
subject | wife | son | me
---------+------+-----+----
beer | | | 50
dress | 200 | |
toy | | 100 |
(3 rows)
第二个查询只是 returns 类别列表:'wife', 'son', 'me'
(顺序很重要)。
您也可以使用 values ('wife'), ('son'), ('me')
。
我有一个交叉表 SQL,但我没有得到预期的输出结果。我得到了第一列中的所有值。这是我的 sql:
select * from crosstab
('select p.name::text as product,
pc.name as prod_cat,
sum( ms.qtyonhand) as total_stock from adempiere.m_product p
join adempiere.m_product_category pc on p.m_product_category_id=pc.m_product_category_id
join adempiere.m_storage ms on ms.m_product_id=p.m_product_id
group by prod_cat,product order by 3 desc') as ct
(product text,
"ELECTRICAL & ELECTRONIC ITEMS" numeric,
"ACADEMICS BOOKS" numeric,
"Standard" numeric,
"FOOD AND BEVERAGES" numeric,
"Possibly Product Category" numeric,
"Pharmacy Medicine" numeric,
"COMPUTER & ACCESSORIES" numeric)
limit 10
这是我得到的输出:http://i.stack.imgur.com/w6gc5.png
这是我正在寻找的输出:
product | Electricals | Electronics | Food & Beverages | Cosmetics | Hardwares
---------------------|-------------|---------------|-------------------|------------|-------------
Samsung-WM | | 4552 | | |
Videocon-Refridge | | 1254 | | |
Philips-CFL Bulbs | 5677 | | | |
Head&shoulder Shampoo| | | | 4567 |
Candysweet | | | 5678 | |
Icecreams | | | 6785 | |
Paints | | | | | 9876
Taps | | | | | 10987
Electrical wires | 18796 | | | |
如何修改我的查询以获得正确的结果?
您应该将函数与两个查询一起使用:crosstab(text source_sql, text category_sql)
。
在第二个查询中 select 所有可能的类别值按所需顺序排列。
简单示例 - 我的家庭预算:
create table expenses (subject text, person text, cost int);
insert into expenses values
('dress', 'wife', 200),
('toy', 'son', 100),
('beer', 'me', 50);
总结:
select *
from crosstab (
$ct$
select * from expenses
order by 1, 2
$ct$
) as ct (subject text, wife int, son int, me int);
subject | wife | son | me
---------+------+-----+----
beer | 50 | |
dress | 200 | |
toy | 100 | |
(3 rows)
嗯,这不是我能相信的。
当查询产生有漏洞的数据时,你应该告诉crosstab()
它如何识别哪个value
属于哪个category
:
select *
from crosstab (
$ct$
select * from expenses
order by 1, 2
$ct$,
$ct$
select person from expenses
order by 1 desc
$ct$
) as ct (subject text, wife int, son int, me int);
subject | wife | son | me
---------+------+-----+----
beer | | | 50
dress | 200 | |
toy | | 100 |
(3 rows)
第二个查询只是 returns 类别列表:'wife', 'son', 'me'
(顺序很重要)。
您也可以使用 values ('wife'), ('son'), ('me')
。