在 hive/mysql 中实施 sum_reverse() [分解 table 结果]
Implement sum_reverse() [Explode table result] in either hive/mysql
我们要平均分配数量。
Table 销量如下
product category qty
123 A 3
345 B 2
输出
product category qty
123 A 1
123 A 1
123 A 1
345 B 1
345 B 1
如果您有 table 个号码,您可以:
select s.product, s.category, 1 as qty
from sales s join
numbers n
on n.n <= s.qty and n.n > 0;
在MySQL中,您可以使用变量生成这样的table。例如,如果 sales
足够大:
select s.product, s.category, 1 as qty
from sales s join
(select (@rn := @rn + 1) as n
from sales s cross join
(select @rn := 0) params
) n
on n.n <= s.qty and n.n > 0;
我自己想出答案(这是在配置单元中)
select category,main1.product,main2.qty from (select category,product,
split(substring(repeat(concat(1,','),int(qty)),0,int(qty*2)-1),",") as qty from
(select collect_set(product) product,category,
sum(qty) qty
from sales) main
lateral view explode(product) main1 as product
lateral view explode(qty) main2 AS qty;
蜂巢
select t.product
,t.category
,1 as qty
from mytable t
lateral view explode(split(space(t.qty - 1),' ')) e
;
+---------+----------+-----+
| product | category | qty |
+---------+----------+-----+
| 123 | A | 1 |
+---------+----------+-----+
| 123 | A | 1 |
+---------+----------+-----+
| 123 | A | 1 |
+---------+----------+-----+
| 345 | B | 1 |
+---------+----------+-----+
| 345 | B | 1 |
+---------+----------+-----+
我们要平均分配数量。 Table 销量如下
product category qty
123 A 3
345 B 2
输出
product category qty
123 A 1
123 A 1
123 A 1
345 B 1
345 B 1
如果您有 table 个号码,您可以:
select s.product, s.category, 1 as qty
from sales s join
numbers n
on n.n <= s.qty and n.n > 0;
在MySQL中,您可以使用变量生成这样的table。例如,如果 sales
足够大:
select s.product, s.category, 1 as qty
from sales s join
(select (@rn := @rn + 1) as n
from sales s cross join
(select @rn := 0) params
) n
on n.n <= s.qty and n.n > 0;
我自己想出答案(这是在配置单元中)
select category,main1.product,main2.qty from (select category,product,
split(substring(repeat(concat(1,','),int(qty)),0,int(qty*2)-1),",") as qty from
(select collect_set(product) product,category,
sum(qty) qty
from sales) main
lateral view explode(product) main1 as product
lateral view explode(qty) main2 AS qty;
蜂巢
select t.product
,t.category
,1 as qty
from mytable t
lateral view explode(split(space(t.qty - 1),' ')) e
;
+---------+----------+-----+
| product | category | qty |
+---------+----------+-----+
| 123 | A | 1 |
+---------+----------+-----+
| 123 | A | 1 |
+---------+----------+-----+
| 123 | A | 1 |
+---------+----------+-----+
| 345 | B | 1 |
+---------+----------+-----+
| 345 | B | 1 |
+---------+----------+-----+