I am running the script below. I was wondering if someone knows how to add three additional rows to the result:
1.add a new row with the MAX value
2.add a new row with the MIN value.
3.add a new row with the average.
Here is my query which run on two full month of April and May of 2006 data.
SELECT
DATEPART(YYYY, DATEADD(MM, 3, SOLD_DATE)) AS FY,
DATENAME(WEEKDAY, SOLD_DATE) AS DAY,
STORE_NAME AS STORE,
CONVERT (VARCHAR, SOLD_DATE, 10) DATES,
SUM(ITEMS) AS ITEM,
'NUMBER' AS NOTE
FROM MYTABLE
WHERE SOLD_DATE >='04/1/2006'
AND SOLD_DATE <'06/1/2006'
AND STORE_NAME ='ELEVEN'
GROUP BY DATEPART(YYYY, DATEADD(MM, 3, SOLD_DATE)),
DATENAME(WEEKDAY, SOLD_DATE), STORE_NAME, SOLD_DATE
The query yields the following result(partial), with the desired additional rows at the bottom of the result.
FY DAY STORE DATES ITEM NOTE
2006 Saturday ELEVEN 4/1/2006 14 NUMBER
2006 Sunday ELEVEN 4/2/2006 21 NUMBER
2006 Monday ELEVEN 4/3/2006 24 NUMBER
--------------------------------------------------
--------------------------------------------------
--------------------------------------------------
2006 Monday ELEVEN 5/29/2006 37 NUMBER
2006 Tuesday ELEVEN 5/30/2006 20 NUMBER
2006 Wednesday ELEVEN 5/31/2006 25 NUMBER
2006 Saturday ELEVEN 5/13/2006 5 MINIMUM
2006 Tuesday ELEVEN 5/16/2006 61 MAXIMUM
2006 ELEVEN 25 AVERAGE
Suggest you load your query into a table variable first. Then you can perform your meta operations. Then
UNIONing the 3 rows onto the resultset.The benefit of this approach:
you’re calculating from the SAME resultset. If you copy/pasted the query for the 3 (min,max,avg), you potentially could be reading different data, if there’s another process who’d updated the rows while you were calculating. If/when this happens, your calculations would appear to be incorrect, compared to the raw data in the resultset.
much less code to read and maintain