【问题标题】:WHY? UNION ALL is doing a Calculation为什么? UNION ALL 正在计算
【发布时间】:2015-03-30 10:18:45
【问题描述】:

我有两个数据集:DS_ADS_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

【问题讨论】:

  • 这不可能发生。 UNION/UNION ALL 在任何情况下都不会更改数据。您的查询中有一些您没有向我们展示的内容导致了这种情况,或者您错误地读取了您的数据并且您完全弄错了。
  • 你确定这是整个查询吗?
  • 我在两个查询中都有一个总和(数量),但这会返回一个二
  • DS_A 和 DS_B 都包含多个连接和业务逻辑
  • 问题可能是查询和已完成的任何舍入(尤其是整数数学会导致这些问题),或者它可能出现在某个其他表具有 1 多或多对多的连接中关系。如果没有数据模型、所有表中的样本数据以及 union all 和原始查询的整个查询,我们如何为您提供帮助?

标签: sql sql-server tsql sql-server-2005


【解决方案1】:

我怀疑它被强制转换为 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

【讨论】:

    【解决方案2】:

    不确定...在 Oracle 11 上是否适合我...您使用的是什么 DBMS,什么版本?

    似乎您可能有错字,或者当您认为它是 3 时它是 2 ?如果UNION之前是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.
    

    【讨论】:

      【解决方案3】:

      在您运行第一个查询和第二个查询或您在一个服务器上运行一个查询和另一个在不同服务器上运行另一个查询之间,数据发生了变化。

      【讨论】:

      • 我在同一台服务器上
      【解决方案4】:

      四舍五入的问题会给你带来麻烦。

      旧表结构:

      DECLARE Sales_Table     TABLE(
               loc_id          Int,    
               pro_id                 Int,     
               quantity               int, --> on insert caused rounding issues       
               price                  Int )
      

      新表结构:

      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 插入 int 不会导致舍入错误。浮点数不是正确的数据类型。使用小数。
      猜你喜欢
      • 1970-01-01
      • 2021-05-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-09-12
      • 2018-06-08
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多