我有一个带有CASE
语句的查询,我需要对GROUP BY
别名做一个。我知道这做不到,所以我尝试使用子查询,但它不起作用。
这是我正在处理的查询:
SELECT
a.Vendor,
a.Month_Sold
--SUM(sd.SBQSHP) AS Sales_Qty,
--SUM(sd.SBEPRC) AS Sales_Dlr
FROM
(SELECT
sd.IFPRVN AS Vendor,
vn.ACNAME AS Vendor_Name,
CASE
WHEN sd.SBINDT BETWEEN '2012-07-30' AND '2012-08-26' THEN 'August 2012'
WHEN sd.SBINDT BETWEEN '2012-08-27' AND '2012-09-30' THEN 'September 2012'
WHEN sd.SBINDT BETWEEN '2012-10-01' AND '2012-10-28' THEN 'October 2012'
WHEN sd.SBINDT BETWEEN '2012-10-29' AND '2012-11-25' THEN 'November 2012'
WHEN sd.SBINDT BETWEEN '2012-11-26' AND '2012-12-31' THEN 'December 2012'
WHEN sd.SBINDT BETWEEN '2013-01-01' AND '2013-01-27' THEN 'January 2013'
WHEN sd.SBINDT BETWEEN '2013-01-28' AND '2013-02-24' THEN 'Febuary 2013'
WHEN sd.SBINDT BETWEEN '2013-02-25' AND '2013-03-31' THEN 'March 2013'
END AS Month_Sold
FROM
dbo.SalesData sd
INNER JOIN dbo.S2K_VEND vn ON vn.ACVEND = sd.IFPRVN
WHERE
sd.SBINDT > '2012-07-29'
AND
sd.SBCLS IN ('1500')
AND
sd.SBDIV NOT IN ('4000')
)a
GROUP BY
a.Vendor,
a.Month_Sold
ORDER BY
a.Vendor,
a.Month_Sold
被注释掉的两列需要以某种方式包含在内。有什么建议么?