I'd like to combine all like Locations (name) and SUM the Volume (vol) for that location and order the results by Day of Month here. I attempted to Select a subset and inner join the values like here but kept getting group by not in aggregate clause errors, thank for the hints!! Results should be ordered by day of month and lines with the same location on same day should be summed on one line instead of multiple lines. Here is my Select Statement without grouping or summing:
SELECT
day_of_month AS 'Day of Month',
run_ticket AS 'Ticket number',
ticketdate AS 'Ticket Date',
id AS 'location id',
name AS 'location',
vol AS 'Volume',
ord_rev AS 'OrderHeader'
FROM
#RECORDS R
ORDER BY
day_of_month
Current Results:
Ordered by day of month, same locations for that day are not summed on one line.
Desired Result:
Ordered by day of month, same locations for that day are summed on one line.
I am also summing total vol per day and for the dates run but doing that in SSRS.
I was trying this solution as such
SELECT day_of_month AS 'Day of Month'
,run_ticket AS 'Ticket Number'
,ticketdate AS 'Ticket Date'
,r2.cmp_id AS 'Location ID'
,cmp_name AS 'location'
,SUM(vol) AS 'Volume'
,ord_rev AS 'OrderHeader'
FROM #RECORDS as r2
JOIN
(SELECT cmp_id, SUM(vol) AS 'Volume'
FROM #RECORDS
GROUP BY cmp_name
) AS s ON s.cmp_id = r2.cmp_id
GROUP BY r2.cmp_name
ORDER BY day_of_month
When I run the proc I get
Msg 8120, Level 16, State 1, Procedure DailyLoadReportMTD, Line 78 [Batch Start Line 109] Column '#RECORDS.cmp_id' is invalid in the select list because it is not contained in either an aggregate function or the GROUP BY clause.
ok, Erics answer got me this close! just need to eliminate dupes If I GROUP BY r.cmp_name, r.ord_revtype2, r.day_of_month I had to add r.ofr_BSWheight which forces a duplicate line... or throws the invalid select error w/o it