为什么? UNION ALL 正在做一个计算
WHY? UNION ALL is doing a Calculation
我有两个数据集:DS_A
和DS_B
。
我的问题是为什么 pro_id
71549 有两个数量而不是三个?
ALL
将所有行合并到结果中。这包括重复项。如果未指定,将删除重复的行。
loc_id pro_id quantity price
------------- ----------- ----------- -----------
2310 5052 1 0
2365 5433 1 0
2310 7694 1 0
2310 9480 1 0
2310 9502 1 0
2310 14413 1 0
2310 31277 1 0
2310 46180 1 0
2310 65233 1 0
2310 68369 1 0
2310 68372 1 0
2310 77396 1 0
loc_id pro_id quantity price
------------- ----------- ----------- -----------
2310 71549 0.15 0
当我做我的 UNION ALL
:
declare @tax float
set @tax = 0.05
select loc_id
, pro_id
, sum(quantity)
, price
from DS_A
group by loc_id, pro_id
UNION ALL
select 2310
, 71549
, case when sum(quantity)<>0 Then sum(quantity/ @tax) Else 0 End
, price
from DS_B
group by pro_id, loc_id
结果:
loc_id pro_id quantity price
------------- ----------- ----------- -----------
2310 5052 1 0
2365 5433 1 0
2310 7694 1 0
2310 9480 1 0
2310 9502 1 0
2310 14413 1 0
2310 31277 1 0
2310 46180 1 0
2310 65233 1 0
2310 68369 1 0
2310 68372 1 0
2310 77396 1 0
2310 71549 2 0
不确定...适合我,在 Oracle 11 上...您使用的是什么 DBMS,什么版本?
你好像打错了,或者当你认为是 3 时却是 2?如果它之前是3,它肯定应该是UNION之后的3。
SQL> l
1 with w_d1 as (
2 select 2310 as loc_id, 5052 as pro_id, 1 as quantity, 0 as price from dual union all
3 select 2365 as loc_id, 5433 as pro_id, 1 as quantity, 0 as price from dual union all
4 select 2310 as loc_id, 7694 as pro_id, 1 as quantity, 0 as price from dual union all
5 select 2310 as loc_id, 9480 as pro_id, 1 as quantity, 0 as price from dual union all
6 select 2310 as loc_id, 9502 as pro_id, 1 as quantity, 0 as price from dual union all
7 select 2310 as loc_id, 14413 as pro_id, 1 as quantity, 0 as price from dual union all
8 select 2310 as loc_id, 31277 as pro_id, 1 as quantity, 0 as price from dual union all
9 select 2310 as loc_id, 46180 as pro_id, 1 as quantity, 0 as price from dual union all
10 select 2310 as loc_id, 65233 as pro_id, 1 as quantity, 0 as price from dual union all
11 select 2310 as loc_id, 68369 as pro_id, 1 as quantity, 0 as price from dual union all
12 select 2310 as loc_id, 68372 as pro_id, 1 as quantity, 0 as price from dual union all
13 select 2310 as loc_id, 77396 as pro_id, 1 as quantity, 0 as price from dual
14 ),
15 w_d2 as (
16 select 2310 as loc_id, 71549 as pro_id, 3 as quantity, 0 as price from dual
17 )
18 select loc_id, pro_id, quantity, price from w_d1
19 union all
20* select loc_id, pro_id, quantity, price from w_d2
SQL> /
LOC_ID PRO_ID QUANTITY PRICE
---------- ---------- ---------- ----------
2310 5052 1 0
2365 5433 1 0
2310 7694 1 0
2310 9480 1 0
2310 9502 1 0
2310 14413 1 0
2310 31277 1 0
2310 46180 1 0
2310 65233 1 0
2310 68369 1 0
2310 68372 1 0
2310 77396 1 0
2310 71549 3 0
13 rows selected.
当您 运行 第一个查询和第二个查询或您 运行 一个服务器上的另一个服务器上的另一个查询之间的数据发生变化。
舍入问题会给您带来麻烦。
旧table结构:
DECLARE Sales_Table TABLE(
loc_id Int,
pro_id Int,
quantity int, --> on insert caused rounding issues
price Int )
新table结构:
DECLARE Sales_Table TABLE(
loc_id Int,
pro_id Int,
quantity decimal(10,2),
price Int)
我的修复测试:
declare @tax float
set @tax = 0.05
insert into Sales_table --> old table
select loc_id
, pro_id
, sum(quantity)
, price
from DS_A
group by loc_id, pro_id
UNION ALL
select 2310
, 71549
, cast(case when sum(0.15)<>0 Then sum(0.15/ @tax) Else 0 End AS float) --> rtns 3
, price
from DS_B
group by pro_id, loc_id
我怀疑它正在转换为 int 并向下舍入
declare @tax float
set @tax = 0.05
declare @quantityF float
set @quantityF = 0.15
select @quantityF / @tax -- 3
select cast((@quantityF / @tax) as int) -- 2
您不应该使用浮点数 - 使用小数点
declare @taxD decimal(9,2)
set @taxD = 0.05
declare @quantityD decimal(9,2)
set @quantityD = 0.15
select @quantityD / @taxD -- 3.0000000
select cast((@quantityD / @taxD) as int) -- 3
我有两个数据集:DS_A
和DS_B
。
我的问题是为什么 pro_id
71549 有两个数量而不是三个?
ALL 将所有行合并到结果中。这包括重复项。如果未指定,将删除重复的行。
loc_id pro_id quantity price
------------- ----------- ----------- -----------
2310 5052 1 0
2365 5433 1 0
2310 7694 1 0
2310 9480 1 0
2310 9502 1 0
2310 14413 1 0
2310 31277 1 0
2310 46180 1 0
2310 65233 1 0
2310 68369 1 0
2310 68372 1 0
2310 77396 1 0
loc_id pro_id quantity price
------------- ----------- ----------- -----------
2310 71549 0.15 0
当我做我的 UNION ALL
:
declare @tax float
set @tax = 0.05
select loc_id
, pro_id
, sum(quantity)
, price
from DS_A
group by loc_id, pro_id
UNION ALL
select 2310
, 71549
, case when sum(quantity)<>0 Then sum(quantity/ @tax) Else 0 End
, price
from DS_B
group by pro_id, loc_id
结果:
loc_id pro_id quantity price
------------- ----------- ----------- -----------
2310 5052 1 0
2365 5433 1 0
2310 7694 1 0
2310 9480 1 0
2310 9502 1 0
2310 14413 1 0
2310 31277 1 0
2310 46180 1 0
2310 65233 1 0
2310 68369 1 0
2310 68372 1 0
2310 77396 1 0
2310 71549 2 0
不确定...适合我,在 Oracle 11 上...您使用的是什么 DBMS,什么版本?
你好像打错了,或者当你认为是 3 时却是 2?如果它之前是3,它肯定应该是UNION之后的3。
SQL> l
1 with w_d1 as (
2 select 2310 as loc_id, 5052 as pro_id, 1 as quantity, 0 as price from dual union all
3 select 2365 as loc_id, 5433 as pro_id, 1 as quantity, 0 as price from dual union all
4 select 2310 as loc_id, 7694 as pro_id, 1 as quantity, 0 as price from dual union all
5 select 2310 as loc_id, 9480 as pro_id, 1 as quantity, 0 as price from dual union all
6 select 2310 as loc_id, 9502 as pro_id, 1 as quantity, 0 as price from dual union all
7 select 2310 as loc_id, 14413 as pro_id, 1 as quantity, 0 as price from dual union all
8 select 2310 as loc_id, 31277 as pro_id, 1 as quantity, 0 as price from dual union all
9 select 2310 as loc_id, 46180 as pro_id, 1 as quantity, 0 as price from dual union all
10 select 2310 as loc_id, 65233 as pro_id, 1 as quantity, 0 as price from dual union all
11 select 2310 as loc_id, 68369 as pro_id, 1 as quantity, 0 as price from dual union all
12 select 2310 as loc_id, 68372 as pro_id, 1 as quantity, 0 as price from dual union all
13 select 2310 as loc_id, 77396 as pro_id, 1 as quantity, 0 as price from dual
14 ),
15 w_d2 as (
16 select 2310 as loc_id, 71549 as pro_id, 3 as quantity, 0 as price from dual
17 )
18 select loc_id, pro_id, quantity, price from w_d1
19 union all
20* select loc_id, pro_id, quantity, price from w_d2
SQL> /
LOC_ID PRO_ID QUANTITY PRICE
---------- ---------- ---------- ----------
2310 5052 1 0
2365 5433 1 0
2310 7694 1 0
2310 9480 1 0
2310 9502 1 0
2310 14413 1 0
2310 31277 1 0
2310 46180 1 0
2310 65233 1 0
2310 68369 1 0
2310 68372 1 0
2310 77396 1 0
2310 71549 3 0
13 rows selected.
当您 运行 第一个查询和第二个查询或您 运行 一个服务器上的另一个服务器上的另一个查询之间的数据发生变化。
舍入问题会给您带来麻烦。
旧table结构:
DECLARE Sales_Table TABLE(
loc_id Int,
pro_id Int,
quantity int, --> on insert caused rounding issues
price Int )
新table结构:
DECLARE Sales_Table TABLE(
loc_id Int,
pro_id Int,
quantity decimal(10,2),
price Int)
我的修复测试:
declare @tax float
set @tax = 0.05
insert into Sales_table --> old table
select loc_id
, pro_id
, sum(quantity)
, price
from DS_A
group by loc_id, pro_id
UNION ALL
select 2310
, 71549
, cast(case when sum(0.15)<>0 Then sum(0.15/ @tax) Else 0 End AS float) --> rtns 3
, price
from DS_B
group by pro_id, loc_id
我怀疑它正在转换为 int 并向下舍入
declare @tax float
set @tax = 0.05
declare @quantityF float
set @quantityF = 0.15
select @quantityF / @tax -- 3
select cast((@quantityF / @tax) as int) -- 2
您不应该使用浮点数 - 使用小数点
declare @taxD decimal(9,2)
set @taxD = 0.05
declare @quantityD decimal(9,2)
set @quantityD = 0.15
select @quantityD / @taxD -- 3.0000000
select cast((@quantityD / @taxD) as int) -- 3