Notice
Recent Posts
Recent Comments
Link
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | 2 | 3 | 4 | 5 | 6 | 7 |
8 | 9 | 10 | 11 | 12 | 13 | 14 |
15 | 16 | 17 | 18 | 19 | 20 | 21 |
22 | 23 | 24 | 25 | 26 | 27 | 28 |
29 | 30 |
Tags
- left join
- CSV
- json
- stratascratch
- R/3
- ERP
- SQL
- order by
- between
- SAP HANA
- asc
- desc
- %_%
- 티스토리챌린지
- SERVICE_KEY_IS_NOT_REGISTERED_ERROR
- join
- over()
- HAVING
- 오블완
- spa
- 공공데이터
- 중복 컬럼
- r/2
- group by
- dense_rank()
- DateDiff
- union
- limit
- Where
- S/4 HANA
Archives
- Today
- Total
RE:cord
[SQL] Why UNION? 본문
-----If not using union...
SELECT ProductID,
SUM(OrderQty) AS TotalQty,
COUNT(OrderQty) NoOfSales,
CASE
WHEN AVG(OrderQty) < 3 THEN 'Low quantity'
WHEN AVG(OrderQty) BETWEEN 3 AND 6 THEN 'Reasonable quantity'
WHEN AVG(OrderQty) > 6 THEN 'High quantity'
END AS AvgQtyDescribe
FROM Sales.SalesOrderDetail AS SOD
JOIN Sales.SalesOrderHeader AS SOH
ON SOD.SalesOrderID = SOH.SalesOrderID
WHERE SOH.OrderDate BETWEEN '2012-05-01' AND '2012-05-31'
GROUP BY SOD.ProductID
ORDER BY AVG(OrderQty) DESC
-----If using union...
-- Query 1: Calculate Total Quantity and Number of Sales
SELECT
SOD.ProductID,
SUM(OrderQty) AS TotalQty,
COUNT(OrderQty) AS NoOfSales,
NULL AS AvgQtyDescribe
FROM
Sales.SalesOrderDetail AS SOD
JOIN Sales.SalesOrderHeader AS SOH
ON SOD.SalesOrderID = SOH.SalesOrderID
WHERE
SOH.OrderDate BETWEEN '2012-05-01' AND '2012-05-31'
GROUP BY
SOD.ProductID
UNION ALL
-- Query 2: Calculate Average Order Quantity and Description
SELECT
SOD.ProductID,
NULL AS TotalQty,
NULL AS NoOfSales,
CASE
WHEN AVG(OrderQty) < 3 THEN 'Low quantity'
WHEN AVG(OrderQty) BETWEEN 3 AND 6 THEN 'Reasonable quantity'
ELSE 'High quantity'
END AS AvgQtyDescribe
FROM
Sales.SalesOrderDetail AS SOD
JOIN Sales.SalesOrderHeader AS SOH
ON SOD.SalesOrderID = SOH.SalesOrderID
WHERE
SOH.OrderDate BETWEEN '2012-05-01' AND '2012-05-31'
GROUP BY
SOD.ProductID
ORDER BY
AvgQtyDescribe DESC;
< Why Use Union? >
Efficiency of UNION vs. Not Using UNION
You're correct in thinking that using UNION can be less efficient than not using it in certain situations. Here’s why:
- Duplicate Removal:
- When you use UNION, SQL checks for duplicates between the two queries and removes them, which adds an extra processing step. This is especially true if the datasets being combined have a lot of rows or if the DISTINCT operation is performed on large sets of data.
- In contrast, if you don’t need to combine different result sets but instead can accomplish everything in one query (without needing to combine results), you avoid that unnecessary duplicate-checking overhead.
- Complexity and Readability:
- Using UNION when it's not needed adds complexity to the query. A single query that calculates everything in one go (with proper JOIN, GROUP BY, and CASE statements) is more straightforward and efficient to execute.
- Performance:
- When combining results using UNION, the SQL engine has to do more work, especially if large datasets are involved. This results in a longer execution time compared to a query that processes the data in one pass.
Conclusion:
In your case, if you can achieve the same results with a single query (as you did with the original approach using JOIN, GROUP BY, and CASE), then avoiding UNION is more efficient both in terms of performance and query readability.
Using UNION is more suitable when you need to combine two distinct sets of data, but if both parts of your query are related and can be calculated together, not using UNION will be more efficient.
'SQL' 카테고리의 다른 글
[SQL] Daily Active Users (0) | 2024.12.17 |
---|---|
[SQL] GROUP BY vs PARTITION BY (0) | 2024.12.16 |
[SQL] Finding Unsold Products (한 번도 팔리지 않은 제품 찾기) (IS NULL / NOT EXIST) (0) | 2024.12.14 |
[SQL] Analyzing User Retention (유저 리텐션 분석) (DATEDIFF / BETWEEN AND / HAVING) (0) | 2024.12.04 |
[SQL] Search for Data Containing Keywords (WHERE LIKE '%A%') (0) | 2024.12.03 |