Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 4267678
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 21, 20262026-05-21T06:55:56+00:00 2026-05-21T06:55:56+00:00

I am running the script below. I was wondering if someone knows how to

  • 0

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
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-21T06:55:57+00:00Added an answer on May 21, 2026 at 6:55 am

    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

    DECLARE @MyResults (TABLE) (FY int, DAY varchar(10), STORE varchar(10),   DATES varchar(10), ITEM varchar(10),    NOTE varchar(10))
    
    DECLARE   @MyCalcs TABLE (ForYear int, Item int, Note varchar(100))
    
    --calc the avg, max and min
    
    INSERT INTO @MyCalcs (ForYear, Item, Note)
       SELECT  FY, AVG(ITEMS), 'Average'
       FROM @MyResults GROUP BY FY
    
    INSERT INTO @MyCalcs (ForYear, Item, Note)
       SELECT  FY, Max(ITEMS), 'Max'
       FROM @MyResults GROUP BY FY
    
    INSERT INTO @MyCalcs (ForYear, Item, Note)
       SELECT  FY, MIN(ITEMS), 'Min'
       FROM @MyResults GROUP BY FY
    
    --union the discrete data with the calcs
    SELECT FY,DAY,STORE,DATES,ITEM,NOTE
    FROM @MyResults
    UNION ALL
    SELECT ForYear AS FY, '' AS Day, '' AS Store, Item, Note
    FROM @MyCalcs
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm running the below script with cron, in /etc/cron.d/mycron I have the following: */10
i am very new to Ruby on rails . while running script/console , i
I have to following problem, after running script below it prints out an extra
I got an issue when running a Selenium Ruby Webdriver script as below: the
I'm running the below script to test something but it seems it's not working.
I'm getting the below error while running the below script. My goal is to
I have the below script running and the following logcat report as at the
I am having trouble running the script below (in Cygwin on win 7 mind
The following script below is not running in IE7 but works perfectly fine in
I am using below statement to return the directory name of the running script:

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.