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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T01:48:57+00:00 2026-06-18T01:48:57+00:00

I have a table: tblUnit (ID, Name, PriceFrom, PriceTo) 1, Audi, 170, 340 2,

  • 0

I have a table:

tblUnit (ID, Name, PriceFrom, PriceTo)

1, Audi, 170, 340
2, BMW,  250, 290
3, Ford, 275, 500
4, Kia,  110, 250
5, VW,   135, 460

And then I have predefined price ranges like this:

tblPriceRange(ID, PriceFrom, PriceTo)

1,   0,  100
2, 100,  200
3, 200,  300
4, 300,  400
5, 400, 1000

I am trying to count the number of vehicles that exists within one or more price ranges. BMW is only in 1 range, while Audi is in 3 ranges and Ford is in 3.

The result I am after should look something like this:

VehiclesPerRange:
(RangeFrom, RangeTo, NoVehicles)
  0, 100,  0
100, 200,  3
200, 300,  5
300, 400,  3
400, 1000, 2

I have read through lots of posts on this forum and elsewhere about grouping by a range. But those examples are focused on 1 single price that should be grouped towards a range. I understand how to do this via a join etc., but I cant figure out how to write SQL to group a range towards a range.

Any suggestions is highly appreciated!

  • 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-18T01:48:59+00:00Added an answer on June 18, 2026 at 1:48 am

    You want to join tblUnit with tblPriceRange matching rows where the ranges overlap. Two ranges overlap if the begin of the second is before the end of the first and the begin of the first before the end of the second, so your join condition would look like this:

    SELECT *
    FROM dbo.tblUnit u
    JOIN dbo.tblPriceRange p
    ON u.PriceFrom < p.PriceTo
    AND p.PriceFrom < u.PriceTo
    

    After that you just have to group and count:

    SELECT PriceFrom, PriceTo, COUNT(1)
    FROM(
        SELECT p.PriceFrom, p.PriceTo
        FROM dbo.tblUnit u
        JOIN dbo.tblPriceRange p
        ON u.PriceFrom < p.PriceTo
        AND p.PriceFrom < u.PriceTo
    )X
    GROUP BY PriceFrom, PriceTo;
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

i have table name is ads like that: id ad_country ad_gender ad_birthday 1 05
I have table with, folowing structure. tbl id name 1 AAA 2 BBB 3
I have table data like this id id1 name 1 1 test1 1 1
I have Table Students with columns Student ID and Student Name. I also have
I have table of shops name, ID, Type,etc. Also I have classes of shops
I have table with name users and a column in it named source .
I have table with data about tasks like Id, Name, Date, WorkerId, VehicleId (and
i have table category the format is like this |id|parent_id|name| |1 |0 |sport| |2
I have table definition like below: Place (id, name) Review (id, userid, placeid) Favorite
I have table with 50 entries (users with such details like Name Surname Location

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.