2017-10-02 1 views
1

여기 내 오류가있는 곳을 알아 내는데 문제가 있습니다.SQL 오류 메시지 8120 수정

Column 'Shipments.ShipmentOrderDate' is invalid in the select list because it is not contained in either an aggregate function or the GROUP BY clause.

답변

1

당신은 당신 때문에 GROUP BY 필요 :이 질문은 :

Write a SELECT statement that returns the following columns 
a. ShipmentOrderDate 
b. Clients Email Address 
c. Column that calculates the Total Order called OrderTotal 
d. A Column that uses the Rank() function to return a column named OrderTotalRank that 
ranks the Order Total in Desc Order. 
e. A column that uses the DenseRank() function to return a column called DenseRank that 
ranks the Order Total in Desc Order. 

그리고 이것은 내가 코딩 한 것입니다 : 나는이 코드를 실행하면

SELECT ShipmentOrderDate, EmailAddress, 
    SUM(sh.ShipItemPrice * sh.Quantity) AS OrderTotalRank, 
    RANK() OVER (ORDER BY SUM(sh.ShipItemPrice * sh.Quantity) DESC) AS OrderTotalRank, 
    DENSE_RANK() OVER (ORDER BY SUM(sh.ShipItemPrice * sh.Quantity) DESC) AS DenseRank 
FROM Clients AS c JOIN Shipments AS s 
    ON c.ClientID = s.ClientID 
    JOIN ShipItems AS sh 
    ON s.ShipmentID = sh.ShipmentID 

나는이 오류 SELECT에 집계 함수가 있습니다. 이들은 또한 사용하고있는 창 함수와 별개입니다 :

SELECT ShipmentOrderDate, EmailAddress, 
     SUM(sh.ShipItemPrice * sh.Quantity) AS OrderTotalRank, 
     RANK() OVER (ORDER BY SUM(sh.ShipItemPrice * sh.Quantity) DESC) AS OrderTotalRank, 
     DENSE_RANK() OVER (ORDER BY SUM(sh.ShipItemPrice * sh.Quantity) DESC) AS DenseRank 
FROM Clients c JOIN 
    Shipments s 
    ON c.ClientID = s.ClientID JOIN 
    ShipItems sh 
    ON s.ShipmentID = sh.ShipmentID 
GROUP BY ShipmentOrderDate, EmailAddress; 
+0

여기서 GROUP BY가 필요한 이유는 무엇입니까? 그게 문제라고 생각했지만 그 중 하나가 필요하다는 것을 확신하지 못했습니다. –