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

  • Home
  • SEARCH
  • 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 8348021
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T07:33:46+00:00 2026-06-09T07:33:46+00:00

table >>> sales state | date | item | px 101 1/1/2012 tennis 2

  • 0

table >>> sales

state | date | item | px

101   1/1/2012   tennis   2
101   1/1/2012   soccer   10
101   1/1/2012   crystal  100
101   1/1/2012   soccer   10

102   1/1/2012   crystal  100
102   1/1/2012   tennis   2
102   1/2/2012   tennis   2

103   1/2/2012   tennis   2
103   1/2/2012   crystal  100
103   1/3/2012   soccer   10
103   1/3/2012   crystal  100


$query="SELECT state, MAX(date), COUNT(state), AVG(px) FROM sales GROUP BY state ORDER by state ASC";

… produces:

state | date | item count | avg px

101   1/1/2012   4   30.5
102   1/2/2012   3   34.67
103   1/3/2012   4   53

Question: How can the query be modified to produce the average px of DISTINCT items in each state? (Instead of the average px of ALL items in each state)

E.g., the average for state 101 should/would be (2+10+100)/3 = 37.33; i.e., the average px for 1 tennis, 1 soccer and 1 crystal.

08-04-12 EDIT >>> Changed px column header in second table to avg px. / edited for clarity. thx.

  • 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-06-09T07:33:47+00:00Added an answer on June 9, 2026 at 7:33 am

    Give this a try.

    EDIT: Added an inner query to distinct based on the item and state.

    SELECT
        state,
        CONVERT(DATETIME, AVG(AverageDate)) AS AverageDate,
        SUM(ItemCount) AS [item count],
        AVG(px) AS px
    FROM
    (
        SELECT
            state,
            item,
            COUNT(*) AS ItemCount,
            AVG(CAST(date AS INT)) AS AverageDate,
            AVG(px) AS px
        FROM sales
        GROUP BY
            state,
            item
    )
    GROUP BY state;
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a query like this: SELECT state AS ls,count(*) AS total, max(sales) AS
I have a table with sales per store as follows: SQL> select * from
I need a little help brainstorming on a query. I have a sales table
I have a table named with Sales having the following columns: Sales_ID|Product_Code|Zone|District|State|Distributor|Total_Sales Now i
I have the following SQL table - Date StoreNo Sales 23/4 34 4323.00 23/4
A client needs a sales report by country state/province. There is an orders table
I have a table called Sales: id_sale id_ticket total state ------- --------- ----- -----
can anyone generate a query for me. Lets say i have a table sales(saleID,
I have 2 tables like this: Stock Table product_id bigint(20) qty float Sales Table
Given the following table Month Product Sales --------- ------- ----- January 1 10 January

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.