views:

49

answers:

1

Hi All,

I have a table "RANGES" with the following columns

   LowRange  as int
   HighRange as int

with the data similar to below

select * from Ranges

LowRange   HighRange
1          100
101        200
201        300
.
.
.
901        1000

And another table "infoDetails" with the following columns

Range as INT
amount as money

with the data similar to below

select * from infoDetails

Range   Amount
23      34.00
235     44.00
345     34.00
678     100.00
555     100.00
530     100.00
510     100.00

I need one report with the following format without cursor.

LowRange    HighRange    Count    Amount

1           100           1        34.00
101         200           0        0.00
.
.
501         600           3        300.00
601         700           1        100.00

Your suggestions are welcome !!

Thanks in Advance

Solai

+1  A: 

Assuming your ranges are correct and they don't overlap nor do they leave gaps:

SELECT LowRange, HighRange, count(*), SUM(Amount)
FROM Ranges r
LEFT JOIN infoDetails d on d.Range between r.LowRange and r.HighRange
GROUP BY LowRange, HighRange;
Remus Rusanu
thank you, but this is not returning all the rows of Range table.
solairaja
Use LEFT JOIN to return rows for ranges that have no details, I edited the post
Remus Rusanu