当前位置导航:炫浪网>>网络学院>>编程开发>>MsSQL教程

SQL中UNION 与 UNION ALL的区别分析


  UNION 运算符:
  
  将两个或更多查询的结果组合为单个结果集,该结果集包含联合查询中的所有查询的全部行。这与使用联接组合两个表中的列不同。
  
  使用 UNION 组合两个查询的结果集的两个基本规则是:
  
  所有查询中的列数和列的顺序必须相同。
  
  数据类型必须兼容。
  
    这种多结果的查询组合为单一结果集在实际中应用的非常方便。但在应用中也有有着问题,如下实例可以进一步说明问题。
  
  问题描述:
  
    为了进一步分析与统计企业中关键部件的生产进度情况,采用了一个表Key_Item_Cal,其结构如图一:
  
 

  通过联合查询,将查询分为三个方面:
  
  1、提出数据的基础明细,其代码如下:
  SELECT dbo.Key_item.Key_item_name AS Item_Name,
  
   dbo.H_MORVE.QTY_RECVD1 AS Quantity, dbo.Key_item.Style AS Style
  
  FROM dbo.Key_item LEFT OUTER JOIN
  
   dbo.H_MORVE ON dbo.Key_item.Key_item = dbo.H_MORVE.ITEM
  
  WHERE (dbo.Key_item.Key_item <> 1)
  
  Order by Style asc
  
  
 

  2、提出数据的一级汇总明细,其代码如下:
  SELECT dbo.Key_item.Key_item_name AS Item_Name,
  
   SUM(dbo.H_MORVE.QTY_RECVD1) AS Quantity,MAX(dbo.Key_item.Style) AS Style
  
  FROM dbo.Key_item LEFT OUTER JOIN
  
   dbo.H_MORVE ON dbo.Key_item.Key_item = dbo.H_MORVE.ITEM
  
  WHERE (dbo.Key_item.Key_item <> 1)
  
  GROUP BY dbo.Key_item.Style,dbo.Key_item.Key_item_name
  
  order by Style
  
  其结果如图如示:
  
  
 

  3、提出数据的二级汇总明细,其代码如下:
  SELECT MAX(dbo.Key_item.Key_item_name) as Item_Name,
  
  SUM(dbo.H_MORVE.QTY_RECVD1) as Quantity,
  
   dbo.Key_item.Style as Style
  
  FROM dbo.Key_item LEFT OUTER JOIN
  
   dbo.H_MORVE ON dbo.Key_item.Key_item = dbo.H_MORVE.ITEM
  
  GROUP BY dbo.Key_item.Style
  
  ORDER BY dbo.Key_item.Style asc
  
  其结果如图所示:
  
  
 

  总体的设计就如以上所示,但通过UNION联接的时候出现了新的问题。通过以下例子,就可以看出UNION与UNION ALL的区别
  
  方案一,其代码如下:
  
  SELECT dbo.Key_item.Key_item_name AS Item_Name,
  
   dbo.H_MORVE.QTY_RECVD1 AS Quantity, dbo.Key_item.Style AS Style
  
  FROM dbo.Key_item LEFT OUTER JOIN
  
   dbo.H_MORVE ON dbo.Key_item.Key_item = dbo.H_MORVE.ITEM
  
  WHERE (dbo.Key_item.Key_item <> 1)
  
  UNION (
  
  SELECT dbo.Key_item.Key_item_name AS Item_Name,
  
   SUM(dbo.H_MORVE.QTY_RECVD1) AS Quantity,MAX(dbo.Key_item.Style) AS Style
  
  FROM dbo.Key_item LEFT OUTER JOIN
  
   dbo.H_MORVE ON dbo.Key_item.Key_item = dbo.H_MORVE.ITEM
  
  WHERE (dbo.Key_item.Key_item <> 1)
  
  GROUP BY dbo.Key_item.Style,dbo.Key_item.Key_item_name
  
  UNION
  
  SELECT MAX(dbo.Key_item.Key_item_name) as Item_Name, SUM(dbo.H_MORVE.QTY_RECVD1) as Quantity,
  
   dbo.Key_item.Style as Style
  
  FROM dbo.Key_item LEFT OUTER JOIN
  
   dbo.H_MORVE ON dbo.Key_item.Key_item = dbo.H_MORVE.ITEM
  
  GROUP BY dbo.Key_item.Style)
  
  ORDER BY dbo.Key_item.Style asc
  
  GO
  
  其结果如下:
  
  
 

  问题:
  
    通过以上结果,可以发现GR180.14.2.1后机架、PY165G.14.2.1B后机架和PY165K.14.2.1后机架的明细与其二级汇总值明显不符。而二级汇总的值是正确,为什么明细与汇总值不符?
  
  针对这个问题,我采用了第二种方案。
  
  方案二,其代码如下:
  
  SELECT dbo.Key_item.Key_item_name AS Item_Name,
  
   dbo.H_MORVE.QTY_RECVD1 AS Quantity, dbo.Key_item.Style AS Style
  
  FROM dbo.Key_item LEFT OUTER JOIN
  
   dbo.H_MORVE ON dbo.Key_item.Key_item = dbo.H_MORVE.ITEM
  
  WHERE (dbo.Key_item.Key_item <> 1)
  
  UNION ALL(
  
  SELECT dbo.Key_item.Key_item_name AS Item_Name,
  
   SUM(dbo.H_MORVE.QTY_RECVD1) AS Quantity,MAX(dbo.Key_item.Style) AS Style
  
  FROM dbo.Key_item LEFT OUTER JOIN
  
   dbo.H_MORVE ON dbo.Key_item.Key_item = dbo.H_MORVE.ITEM
  
  WHERE (dbo.Key_item.Key_item <> 1)
  
  GROUP BY dbo.Key_item.Style,dbo.Key_item.Key_item_name
  
  UNION
  
  SELECT MAX(dbo.Key_item.Key_item_name) as Item_Name,
  
  SUM(dbo.H_MORVE.QTY_RECVD1) as Quantity,
  
   dbo.Key_item.Style as Style
  
  FROM dbo.Key_item LEFT OUTER JOIN
  
   dbo.H_MORVE ON dbo.Key_item.Key_item = dbo.H_MORVE.ITEM
  
  GROUP BY dbo.Key_item.Style)
  
  ORDER BY dbo.Key_item.Style asc
  
  GO
  
  其结果如图:
  
  

  通过以上的例子,大家可以看出在UNION与UNION ALL在应用上区别
相关内容
赞助商链接