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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T23:07:06+00:00 2026-06-13T23:07:06+00:00

I have a table item_table like this: item age ————– 1 1 1 6

  • 0

I have a table item_table like this:

item   age
--------------    
1      1 
1      6 
2      2    

I have the other table price_table like this:

item    pricetype    price
--------------------------    
1       O             5
1       P             6
1       V             7
2       O             8
2       P             9
2       V             10

So, I want to inner join above two tables.

select *
from item_table i
inner join price_table p
on ...

There are some conditions about the on:

  1. if the average of age of an item is bigger than 3, then I do: inner join price_table on pricetype = 'O' or pricetype = 'P'
  2. If not, then I do: inner join price_table on pricetype = 'O' or pricetype = 'P' or pricetype = 'V'

So there are conditions for on conditions.

I then write the query like this:

select i.item, i.type, p.pricetype, p.price
from item_table i
inner join price_table p on i.item = p.item 
    and (avg(i.age) >= 3 and p.pricetype in ('O', 'P'))
        or (avg(i.age) < 3 and p.pricetype in ('O', 'P', 'V'))

The error is given: An aggregate cannot appear in an ON clause unless it is in a subquery contained in a HAVING clause or select list, and the column being aggregated is an outer reference.

I can’t move the avg to Having because other conditions are depending on the avg.

How can I write the select query?

  • 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-13T23:07:06+00:00Added an answer on June 13, 2026 at 11:07 pm
    select *
    from (
        select item, avg(age) as AvgAge
        from item_table
        group by item
    ) ia
    inner join price_table p on ia.item = p.item 
        and ((ia.AvgAge >= 3 and p.pricetype in ('O', 'P'))
            or (ia.AvgAge < 3 and p.pricetype in ('O', 'P', 'V')))
    

    SQL Fiddle Example 1

    This can be simplified to:

    select *
    from (
        select item, avg(age) as AvgAge
        from item_table
        group by item
    ) ia
    inner join price_table p on ia.item = p.item 
        and (p.pricetype in ('O', 'P')
            or (ia.AvgAge < 3 and p.pricetype = 'V'))
    

    SQL Fiddle Example 2

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

Sidebar

Related Questions

I am using SQL Server. I have a table item_table like this: item age
I have a table like this: Item Name Name_location Price 1 item1_london london 10
I have a table like this:- Selling Sales Item SIZE QTY Price U/M ----
I have a table like this id item user slot I want the SLOT
I have a Table [Price] like this Date Item Price 1-1-2012 Potion 500 1-1-2012
I have a stock transaction table like this: Item Date TxnType Qty Price ABC
I have a bill of materials table that is set up like this: item
I have a table like this: Item { int ItemID int ParentID string Name
I have a table like this: Item ProviderId Item1 1 Item1 2 Item2 1
So I have a table like this: Item Number Collection Items in Collection 1234

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.