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 645707
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T21:31:57+00:00 2026-05-13T21:31:57+00:00

I have the following SQL table – Date StoreNo Sales 23/4 34 4323.00 23/4

  • 0

I have the following SQL table –

Date       StoreNo       Sales
23/4            34     4323.00
23/4            23      564.00
24/4            34     2345.00
etc

I am running a query that returns average sales, max sales and min sales for a certain period –

select avg(Sales), max(sales), min(sales)
from tbl_sales
where date between etc

But there are some values coming through in the min and max that are really extreme – perhaps because the data entry was bad, perhaps because some anomoly had occurred on that date and store.

What I’d like is a query that returns average, max and min, but somehow excludes the extreme values. I am open to how this is done, but perhaps it would use standard deviations in some way (for example, only using data within x std devs of the true average).

Many thanks

  • 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-13T21:31:58+00:00Added an answer on May 13, 2026 at 9:31 pm

    In order to calculate the standard deviation, you need to iterate through all of the elements, so it would be impossible to do this in one query. The lazy way would be to just do it in two passes:

    DECLARE
        @Avg int,
        @StDev int
    
    SELECT @Avg = AVG(Sales), @StDev = STDEV(Sales)
    FROM tbl_sales
    WHERE ...
    
    SELECT AVG(Sales) AS AvgSales, MAX(Sales) AS MaxSales, MIN(Sales) AS MinSales
    FROM tbl_sales
    WHERE ...
    AND Sales >= @Avg - @StDev * 3
    AND Sales <= @Avg + @StDev * 3
    

    Another simple option that might work (fairly common in analysis of scientific data) would be to just drop the minimum and maximum x values, which works if you have a lot of data to process. You can use ROW_NUMBER to do this in one statement:

    WITH OrderedValues AS
    (
        SELECT
            Sales,
            ROW_NUMBER() OVER (ORDER BY Sales) AS RowNumAsc,
            ROW_NUMBER() OVER (ORDER BY Sales DESC) AS RowNumDesc
    )
    SELECT ...
    FROM tbl_sales
    WHERE ...
    AND Sales >
    (
        SELECT MAX(Sales)
        FROM OrderedValues
        WHERE RowNumAsc <= @ElementsToDiscard
    )
    AND Sales <
    (
        SELECT MIN(Sales)
        FROM OrderedValues
        WHERE RowNumDesc <= @ElementsToDiscard
    )
    

    Replace ROW_NUMBER with RANK or DENSE_RANK if you want to discard a certain number of unique values.

    Beyond these simple tricks you start to get into some pretty heavy stats. I have to deal with similar kinds of validation and it’s far too much material for a SO post. There are a hundred different algorithms that you can tweak in a dozen different ways. I would try to keep it simple if possible!

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have following SQL query that selects some results from my table: select avg(c3),
I have the following sql query that fetches images from an establishment table. The
I have following sql query that take only 1 second to execute: select a.date,
I have the following pseudo-SQL schema: table flight id int primary key date timestamp
I have the following example in a SQL table Cust Group Sales A 1
I have the following SQL query which populates a table with details of various
If I have the following SQL query CREATE TABLE #t1 (a INT NOT NULL
I have a simple SQL table of the following format: date | value ============
I have the following SQL query: SELECT * FROM table WHERE field_1 <> field_2
I have the following SQL query that performs a subquery and joins two tables

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.