如何通过对 db2 或 sql 中的数据进行分组来生成一系列数字
how generate a series of numbers by grouping the data in db2 or sql
我有如下数据
Manager
City
Building
Floor
Number of Seats
xxx
BLR
SA2
2F
2
xyz
BLR
SA2
2F
3
xya
BLR
SA2
3F
2
xjk
BLR
SA2
3F
1
结果数据应该如下,按经理或建筑物和楼层分组,需要根据座位数生成一系列数字。
Manager
City
Building
Floor
Number of Seats
xxx
BLR
SA2
2F
1
xxx
BLR
SA2
2F
2
xyz
BLR
SA2
2F
3
xyz
BLR
SA2
2F
4
xyz
BLR
SA2
2F
5
xya
BLR
SA2
3F
1
xya
BLR
SA2
3F
2
xjk
BLR
SA2
3F
3
您可以通过 no_of_seats 销售产品,然后通过经理 ID 订购。
这里有一个方法可以做到这一点
with row_gen(rn) as (
select 1 from SYSIBM.SYSDUMMY1
union all
select rn+ 1 from dummy where id < 10000 /*assumption that the number of seats isnt more than 10000*/
)
,data
as (
select tb.mgr,tb.city,tb.bldg,tb.floor,rg.rn
from row_gen rg
join table tb
on tb.no_of_seats<=rg.rn
)
select d.mgr,d.city,d.bldg,d.floor
,row_number() over(partition by d.city,d.bldg,d.floor order by d.mgr) as no_of_seats
from data d
with row_gen(rn) as (
SELECT 1 AS rn FROM SYSIBM.SYSDUMMY1
UNION ALL
SELECT rn+1 FROM row_gen WHERE rn+ 1 <=50
)
,data
as (
select tb.manager,tb.location,tb.building,tb.floor,rg.rn
from row_gen rg
join db2inst1.tmp_cio_data tb
on rg.rn <= tb.seat_range_to
)
select d.manager,d.location,d.building,d.floor,
row_number() over(partition by d.location,d.building,d.floor order by d.manager) as no_of_seats
from data d WHERE d.floor = 11 AND d.building = '12D'
下面的解决方案不依赖于最大座位数。
WITH
/*
MYTAB (Manager, City, Building, Floor, Number_of_Seats) AS
(
VALUES
('xxx', 'BLR', 'SA2', '2F', 2)
, ('xyz', 'BLR', 'SA2', '2F', 3)
, ('xya', 'BLR', 'SA2', '3F', 2)
, ('xjk', 'BLR', 'SA2', '3F', 1)
)
,
*/
T (Manager, City, Building, Floor, Number_of_Seats) AS
(
SELECT Manager, City, Building, Floor, Number_of_Seats
FROM MYTAB
UNION ALL
SELECT Manager, City, Building, Floor, Number_of_Seats - 1
FROM T
WHERE Number_of_Seats > 1
)
SELECT
Manager, City, Building, Floor
, ROW_NUMBER () OVER (PARTITION BY City, Building, Floor ORDER BY Manager) AS Number_of_Seats
FROM T
ORDER BY City, Building, Floor, Manager
如果您取消注释推荐的 out 块和 运行 原样的语句,您将得到以下输出。
MANAGER
CITY
BUILDING
FLOOR
NUMBER_OF_SEATS
xxx
BLR
SA2
2F
1
xxx
BLR
SA2
2F
2
xyz
BLR
SA2
2F
3
xyz
BLR
SA2
2F
4
xyz
BLR
SA2
2F
5
xjk
BLR
SA2
3F
1
xya
BLR
SA2
3F
2
xya
BLR
SA2
3F
3
我有如下数据
Manager | City | Building | Floor | Number of Seats |
---|---|---|---|---|
xxx | BLR | SA2 | 2F | 2 |
xyz | BLR | SA2 | 2F | 3 |
xya | BLR | SA2 | 3F | 2 |
xjk | BLR | SA2 | 3F | 1 |
结果数据应该如下,按经理或建筑物和楼层分组,需要根据座位数生成一系列数字。
Manager | City | Building | Floor | Number of Seats |
---|---|---|---|---|
xxx | BLR | SA2 | 2F | 1 |
xxx | BLR | SA2 | 2F | 2 |
xyz | BLR | SA2 | 2F | 3 |
xyz | BLR | SA2 | 2F | 4 |
xyz | BLR | SA2 | 2F | 5 |
xya | BLR | SA2 | 3F | 1 |
xya | BLR | SA2 | 3F | 2 |
xjk | BLR | SA2 | 3F | 3 |
您可以通过 no_of_seats 销售产品,然后通过经理 ID 订购。
这里有一个方法可以做到这一点
with row_gen(rn) as (
select 1 from SYSIBM.SYSDUMMY1
union all
select rn+ 1 from dummy where id < 10000 /*assumption that the number of seats isnt more than 10000*/
)
,data
as (
select tb.mgr,tb.city,tb.bldg,tb.floor,rg.rn
from row_gen rg
join table tb
on tb.no_of_seats<=rg.rn
)
select d.mgr,d.city,d.bldg,d.floor
,row_number() over(partition by d.city,d.bldg,d.floor order by d.mgr) as no_of_seats
from data d
with row_gen(rn) as (
SELECT 1 AS rn FROM SYSIBM.SYSDUMMY1
UNION ALL
SELECT rn+1 FROM row_gen WHERE rn+ 1 <=50
)
,data
as (
select tb.manager,tb.location,tb.building,tb.floor,rg.rn
from row_gen rg
join db2inst1.tmp_cio_data tb
on rg.rn <= tb.seat_range_to
)
select d.manager,d.location,d.building,d.floor,
row_number() over(partition by d.location,d.building,d.floor order by d.manager) as no_of_seats
from data d WHERE d.floor = 11 AND d.building = '12D'
下面的解决方案不依赖于最大座位数。
WITH
/*
MYTAB (Manager, City, Building, Floor, Number_of_Seats) AS
(
VALUES
('xxx', 'BLR', 'SA2', '2F', 2)
, ('xyz', 'BLR', 'SA2', '2F', 3)
, ('xya', 'BLR', 'SA2', '3F', 2)
, ('xjk', 'BLR', 'SA2', '3F', 1)
)
,
*/
T (Manager, City, Building, Floor, Number_of_Seats) AS
(
SELECT Manager, City, Building, Floor, Number_of_Seats
FROM MYTAB
UNION ALL
SELECT Manager, City, Building, Floor, Number_of_Seats - 1
FROM T
WHERE Number_of_Seats > 1
)
SELECT
Manager, City, Building, Floor
, ROW_NUMBER () OVER (PARTITION BY City, Building, Floor ORDER BY Manager) AS Number_of_Seats
FROM T
ORDER BY City, Building, Floor, Manager
如果您取消注释推荐的 out 块和 运行 原样的语句,您将得到以下输出。
MANAGER | CITY | BUILDING | FLOOR | NUMBER_OF_SEATS |
---|---|---|---|---|
xxx | BLR | SA2 | 2F | 1 |
xxx | BLR | SA2 | 2F | 2 |
xyz | BLR | SA2 | 2F | 3 |
xyz | BLR | SA2 | 2F | 4 |
xyz | BLR | SA2 | 2F | 5 |
xjk | BLR | SA2 | 3F | 1 |
xya | BLR | SA2 | 3F | 2 |
xya | BLR | SA2 | 3F | 3 |