你可以做:
SELECT
SUM(a.GQTY) AS GrossQty,
SUM(a.NQTY) AS NQTY
FROM
(
SELECT SUM(GrossQty) AS GQTY, SUM(NetQty) AS NQTY
FROM NSales
WHERE IMonth = 5 AND IYear = 2012 AND IDay > 15
UNION ALL
SELECT SUM(GrossQty), SUM(NetQty)
FROM NSales
WHERE IMonth = 6 AND IYear = 2012 AND IDay < 10
) a
或者:
SELECT
a.GQTY + b.GQTY AS GQTY,
a.NQTY + b.NQTY AS NQTY
FROM
(
SELECT SUM(GrossQty) AS GQTY, SUM(NetQty) AS NQTY
FROM NSales
WHERE IMonth = 5 AND IYear = 2012 AND IDay > 15
) a
CROSS JOIN
(
SELECT SUM(GrossQty) AS GQTY, SUM(NetQty) NQTY
FROM NSales
WHERE IMonth = 6 AND IYear = 2012 AND IDay < 10
) b
或者您可以使用CASE
表达式并避免发出两个单独的子选择(更难阅读):
SELECT
SUM(
CASE WHEN (IMonth = 5 AND IYear = 2012 AND IDay > 15) OR
(IMonth = 6 AND IYear = 2012 AND IDay < 10) THEN
GrossQty
ELSE 0
END) AS GQTY,
SUM(
CASE WHEN (IMonth = 5 AND IYear = 2012 AND IDay > 15) OR
(IMonth = 6 AND IYear = 2012 AND IDay < 10) THEN
NetQty
ELSE 0
END) AS NQTY
FROM NSales