程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 數據庫知識 >> SqlServer數據庫 >> 關於SqlServer >> SQLServer2000中UNION與UNION ALL的區別

SQLServer2000中UNION與UNION ALL的區別

編輯:關於SqlServer
 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,&


您正在看的SQLserver教程是:SQLServer2000中UNION與UNION ALL的區別。nbsp;
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在應用上區別。
  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved