我有一个查询,根据位置名称显示空位置的数量,我想要添加的是我在空位置旁边创建的总位置列,然后我打算做数学运算显示可用库存空间的百分比。我对子查询等相当新,所以我不确定我哪里出错了
完整查询:
select bin_type_name, floor, empty_locations, total day, time from(
(select bin_type_name,
bin_level as floor,
sum(bin) as empty_locations,
to_char(sysdate, 'MM-DD-YYYY ') as day,
to_char(sysdate,'hh:mi:ssam') as time
from
(select count(bins.bin_id) as bin, bin_type_name, bin_level
from bins
left join bin_items bi on bi.bin_id = bins.bin_id
where bi.quantity is null
and usage = '1024'
group by bins.bin_id, bin_level, bin_type_name)
group by bin_type_name, bin_level, to_char(sysdate, 'MM-DD-YYYY '), to_char(sysdate,'hh:mi:ssam')
)order by bin_level) a
inner join a on a.bin_type_name = b.bin_type_name
union
(select bin_type_name, count(bin_type_name) as total, bin_level from bins
where usage = '1024'
group by bin_type_name, bin_level
)order by floor) b;
上半部分:
select bin_type_name, count(b.bin_id) empty_locations, bin_level from bins b
left join bin_items bi on bi.bin_id = b.bin_id
where quantity is null
and usage = '1024'
group by bin_type_name, bin_level, quantity
order by bin_level;
最佳结果:
14-KIVA-DEEP 1 645 12-30-2013 04:35:47pm
18-KIVA-DEEP 1 235 12-30-2013 04:35:47pm
24-KIVA-DEEP 1 74 12-30-2013 04:35:47pm
30-KIVA-DEEP 1 35 12-30-2013 04:35:47pm
34-KIVA-DEEP 1 116 12-30-2013 04:35:47pm
48-KIVA-DEEP 1 17 12-30-2013 04:35:47pm
78-KIVA-TALL 1 154 12-30-2013 04:35:47pm
PALLET-SINGLE 1 654 12-30-2013 04:35:47pm
14-KIVA-DEEP 2 783 12-30-2013 04:35:47pm
18-KIVA-DEEP 2 847 12-30-2013 04:35:47pm
24-KIVA-DEEP 2 120 12-30-2013 04:35:47pm
30-KIVA-DEEP 2 276 12-30-2013 04:35:47pm
34-KIVA-DEEP 2 32 12-30-2013 04:35:47pm
PALLET-SINGLE 2 526 12-30-2013 04:35:47pm
底部部分
select bin_type_name, bin_level, count(bin_type_name) as total from bins
其中usage ='1024' group by bin_type_name,bin_level 按bin_level排序;
底部结果
14-KIVA-DEEP 1 8381
18-KIVA-DEEP 1 6004
24-KIVA-DEEP 1 12331
30-KIVA-DEEP 1 4607
34-KIVA-DEEP 1 4771
48-KIVA-DEEP 1 4800
48-KIVA-XL 1 2698
78-KIVA-TALL 1 3200
PALLET-SINGLE 1 3202
14-KIVA-DEEP 2 4219
18-KIVA-DEEP 2 8502
24-KIVA-DEEP 2 8773
30-KIVA-DEEP 2 6997
34-KIVA-DEEP 2 829
PALLET-SINGLE 2 2848
期望的结果:
bin_type_name floor empty % available total day time
14-KIVA-DEEP 1 645 7.70% 8381 12/30/2013 04:38:12pm
18-KIVA-DEEP 1 234 3.90% 6004 12/30/2013 04:38:12pm
24-KIVA-DEEP 1 74 0.60% 12331 12/30/2013 04:38:12pm
30-KIVA-DEEP 1 35 0.76% 4607 12/30/2013 04:38:12pm
34-KIVA-DEEP 1 116 2.43% 4771 12/30/2013 04:38:12pm
48-KIVA-DEEP 1 17 0.35% 4800 12/30/2013 04:38:12pm
48-KIVA-XL 1 0 0.00% 2698 12/30/2013 04:38:12pm
78-KIVA-TALL 1 154 4.81% 3200 12/30/2013 04:38:12pm
PALLET-SINGLE 1 654 20.42% 3202 12/30/2013 04:38:12pm
14-KIVA-DEEP 2 783 18.56% 4219 12/30/2013 04:38:12pm
18-KIVA-DEEP 2 847 9.96% 8502 12/30/2013 04:38:12pm
24-KIVA-DEEP 2 120 1.37% 8773 12/30/2013 04:38:12pm
30-KIVA-DEEP 2 276 3.94% 6997 12/30/2013 04:38:12pm
34-KIVA-DEEP 2 32 3.86% 829 12/30/2013 04:38:12pm
PALLET-SINGLE 2 524 18.40% 2848 12/30/2013 04:38:12pm
答案 0 :(得分:1)
SELECT x.bin_type_name,
x.bin_level floor,
x.empty_locations EMPTY,
( ( b.total total - x.empty_locations ) / y.total total ) * 100 perc_available,
y.total total,
TO_CHAR ( sysdate, 'MM-DD-YYYY ' ) AS DAY,
TO_CHAR ( sysdate, 'hh:mi:ssam' ) AS TIME
FROM
(
SELECT bin_type_name,
COUNT ( b.bin_id ) empty_locations,
bin_level
FROM bins b
LEFT JOIN bin_items bi
ON bi.bin_id = b.bin_id
WHERE quantity IS NULL
AND USAGE = '1024'
GROUP BY bin_type_name,
bin_level,
quantity
)
x
INNER JOIN
(
SELECT bin_type_name,
bin_level,
COUNT ( bin_type_name ) AS total
FROM bins
WHERE USAGE = '1024'
GROUP BY bin_type_name,
bin_level
)
y
ON x.bin_type_name = y.bin_type_name
AND x.bin_level = y.bin_level
order by 2 ;