为什么? UNION ALL正在进行计算

时间:2015-01-30 20:50:06

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

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

4 个答案:

答案 0 :(得分: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

你不应该使用float - 使用十进制

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

答案 1 :(得分: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.

答案 2 :(得分:0)

在您运行第一个查询和第二个查询之间或在一台服务器上运行一个而另一台服务器上运行另一个服务器时,数据会发生变化。

答案 3 :(得分:0)

舍入问题会让你陷入困境。

旧表结构:

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