无效的列名“销售季度”错误消息

时间:2019-01-30 04:56:34

标签: sql-server

我正在使用下面的SQL。

我的目标是:

1)连接名字和姓氏,并将它们放在名为“雇员姓名”的列中。

2)仅从InvoiceDate列中提取年份。

3)从InvoiceDate确定季度并将其放入名为Sales Quarter的列。

4)确定“最大”,“计数”和“总和”,并将它们放入称为“最高价格”,“销售数量”和“总销售”的列中。

5)按年份和销售季度分组。

6)按员工姓名,年份和销售季度排序。

执行下面的代码时,我收到以下消息。

  

“无效的列名“销售季度”。

我不知道我在做什么错...任何建议都会最有帮助。

SELECT 
    CONCAT(E.FirstName,' ', E.LastName) AS "Employee Name"
    , YEAR(I.InvoiceDate) AS "Fiscal Year"
    , CASE 
        WHEN MONTH(I.InvoiceDate) BETWEEN 1 AND 3 THEN 'First'
        WHEN MONTH(I.InvoiceDate) BETWEEN 4 AND 6 THEN 'Second'
        WHEN MONTH(I.InvoiceDate) BETWEEN 7 AND 9 THEN 'Third'
        WHEN MONTH(I.InvoiceDate) BETWEEN 10 AND 12 THEN 'Fourth'
        END "Sales Quarter"
    , MAX(I.Total) AS "Highest Price"
    , COUNT(*) AS "Number of Sales"
    , SUM(IL.UnitPrice) AS "Total Sales"

FROM Employee E
LEFT JOIN Customer C
    ON E.EmployeeId = C.SupportRepId
JOIN Invoice I
    ON C.CustomerId = I.CustomerId
JOIN InvoiceLine IL
    ON I.InvoiceId = IL.InvoiceId 

WHERE I.InvoiceDate  BETWEEN '2010-01-01' AND '2012-06-30'

GROUP BY YEAR(I.InvoiceDate), "Sales Quarter"
ORDER BY CONCAT(E.FirstName,' ', E.LastName), YEAR(I.InvoiceDate), "Sales Quarter"

1 个答案:

答案 0 :(得分:2)

您可以在下面尝试-无法识别group by子句中的别名

SELECT 
    CONCAT(E.FirstName,' ', E.LastName) AS "Employee Name"
    , YEAR(I.InvoiceDate) AS "Fiscal Year"
    , CASE 
        WHEN MONTH(I.InvoiceDate) BETWEEN 1 AND 3 THEN 'First'
        WHEN MONTH(I.InvoiceDate) BETWEEN 4 AND 6 THEN 'Second'
        WHEN MONTH(I.InvoiceDate) BETWEEN 7 AND 9 THEN 'Third'
        WHEN MONTH(I.InvoiceDate) BETWEEN 10 AND 12 THEN 'Fourth'
        END "Sales Quarter"
    , MAX(I.Total) AS "Highest Price"
    , COUNT(*) AS "Number of Sales"
    , SUM(IL.UnitPrice) AS "Total Sales"

FROM Employee E
LEFT JOIN Customer C
    ON E.EmployeeId = C.SupportRepId
JOIN Invoice I
    ON C.CustomerId = I.CustomerId
JOIN InvoiceLine IL
    ON I.InvoiceId = IL.InvoiceId 

WHERE I.InvoiceDate  BETWEEN '2010-01-01' AND '2012-06-30'

GROUP BY YEAR(I.InvoiceDate), CONCAT(E.FirstName,' ', E.LastName),DATEPART ( qq,I.InvoiceDate )  ,CASE 
        WHEN MONTH(I.InvoiceDate) BETWEEN 1 AND 3 THEN 'First'
        WHEN MONTH(I.InvoiceDate) BETWEEN 4 AND 6 THEN 'Second'
        WHEN MONTH(I.InvoiceDate) BETWEEN 7 AND 9 THEN 'Third'
        WHEN MONTH(I.InvoiceDate) BETWEEN 10 AND 12 THEN 'Fourth'
        END
ORDER BY CONCAT(E.FirstName,' ', E.LastName), YEAR(I.InvoiceDate), DATEPART ( qq,I.InvoiceDate )