0

SQL newbie.

I'm trying to retrieve data for a product record in a databse using an SQL statement.

However, I get an error that says

Msg 8120, Level 16, State 1, Line 11 Column 'AggregatedSalesHistory.PriceZoneID' is invalid in the select list because it is not contained in either an aggregate function or the GROUP BY clause."

I'm using Microsoft SQL Server Management Studio 2014 and this is the statement I ran-

select  
    PriceZoneID,
    Date, 
    CAST(ash.Date as date) as Date, 
    SUM(ash.Sales) as Units, 
    SUM(ash.Sales * ash.Price) AS CashSales
from AggregatedSalesHistory AS ASH 
    INNER JOIN ForecastProductScenario AS LRF
                ON ash.ProductSID = lrf.ProductSID AND ash.date = lrf.ForecastDate AND ash.PriceZoneID = lrf.ZoneID AND ash.SegmentID = lrf.SegmentID 
                WHERE ash.PriceZoneID = 2 
                AND ash.ProductSID = '56358E18-7B49-4346-8B70-DC5DE122DB59'
                AND DATE = 12/10/2014 
DhruvJoshi
  • 17,041
  • 6
  • 41
  • 60
Mr 2017
  • 103
  • 2
  • 6
  • 15
  • 1
    You need to add `PriceZoneId` and `Date` to a `group by` clause. When aggregating data (such as `sum`), any column that is not included in an aggregation needs to be included in `group by`. – sgeddes Jun 13 '18 at 17:08
  • Help up help you - please share the table's structure, some sample data, and the result you're trying to get for it. – Mureinik Jun 13 '18 at 17:13
  • There is no GROUP BY clause – DhruvJoshi Jun 13 '18 at 17:20

0 Answers0