SQL查询以获取特定范围内的所有数据

时间:2009-11-17 20:20:32

标签: sql

我有一个包含三列的表(商品),id,product_id和price。

offer
----- 
id (integer)   
product_id (integer)
price (decimal)

我想触发一个SQL查询,该查询将返回价格范围之间的商品数量。 范围应该是0-1,1-2,2-3等

price_lower   price_upper    number_of_offers
-------------------------------------------------------------
0                     1            4
1                     2            1
2                     3            0
3                     4            6
4                     5            2
... etc

我这样做是为了获得0到1之间的报价数量

SELECT * FROM offer WHERE price BETWEEN 0 and 1;

获得所需结果的查询应该是什么。

任何形式的帮助将不胜感激。提前谢谢。

9 个答案:

答案 0 :(得分:4)

检查所有答案时。我已经成功地在你的帮助下编写了查询。 很少有人建议创建一个新表来存储我不想做的报价范围。

所以,这是我想要的SQL查询:

SELECT price as price_lower, (price + 1) as price_upper, (SELECT count(*) from offer WHERE price BETWEEN o.price and (o.price + 0.99)) from offer o GROUP BY price;

感谢所有人的努力。你们摇滚。

答案 1 :(得分:3)

您可以尝试这样的事情

DECLARE @Offer TABLE(
        ID INT IDENTITY (1,1),
        Product_ID INT,
        Price FLOAT
)

INSERT INTO @Offer SELECT 1, 0
INSERT INTO @Offer SELECT 1, .25
INSERT INTO @Offer SELECT 1, .5
INSERT INTO @Offer SELECT 1, .75
INSERT INTO @Offer SELECT 1, 1.
INSERT INTO @Offer SELECT 1, 1.25
INSERT INTO @Offer SELECT 1, 1.5
INSERT INTO @Offer SELECT 1, 1.75
INSERT INTO @Offer SELECT 1, 2.

INSERT INTO @Offer SELECT 2, 1
INSERT INTO @Offer SELECT 2, 1.25
INSERT INTO @Offer SELECT 2, 1.5
INSERT INTO @Offer SELECT 2, 1.75
INSERT INTO @Offer SELECT 2, 2.
INSERT INTO @Offer SELECT 2, 2.25
INSERT INTO @Offer SELECT 2, 2.5
INSERT INTO @Offer SELECT 2, 2.75
INSERT INTO @Offer SELECT 2, 3.

SELECT  Product_ID,
        FLOOR(Price) StartPrice,
        FLOOR(Price) + 1 EndPrice,
        COUNT(1) NumberItems
FROM    @Offer
GROUP BY Product_ID,
        FLOOR(Price)
ORDER BY 1, 2

答案 2 :(得分:1)

SELECT SUM(number_of_offers)FROM offer WHERE price_lower> = [最低价格] AND price_upper< = [最高价格]。

您必须填写方括号中的位,例如您可以在那里替换参数。

答案 3 :(得分:1)

要获得您列出的输出:

SELECT MIN(t.price) 'price_lower',
       MAX(t.price) 'price_upper',
       COUNT(*) 'number_of_offers'
  FROM OFFER t
 WHERE t.price BETWEEN 0 AND 1
UNION ALL
SELECT MIN(t.price) 'price_lower',
       MAX(t.price) 'price_upper',
       COUNT(*) 'number_of_offers'
  FROM OFFER t
 WHERE t.price BETWEEN 1 AND 2
UNION ALL
...

...为您想要的每个分组添加不同的SQL语句,更改WHERE子句以适应。

答案 4 :(得分:1)

我用过:

select floor(price) as price_lower,
       ceiling(price) as price_upper,
       count(*) as offercount
   from tbl_offer
   group by floor(price), ceiling(price)

对于以下测试数据:

insert into tbl_offer ([product_id],[price]) values (1, 1.9)
insert into tbl_offer ([product_id],[price]) values (2, 2.2)
insert into tbl_offer ([product_id],[price]) values (3, 2.3)
insert into tbl_offer ([product_id],[price]) values (4, 4.5)
insert into tbl_offer ([product_id],[price]) values (5, 2.7)

我得到了以下结果:

price_lower price_upper offercount  
----------- ----------- ----------- 
1           2           1
2           3           3
4           5           1

您提供的示例表中唯一缺少的是我没有price_lower 3,price_upper 4,offercount 0的行。

答案 5 :(得分:0)

这应该是正确的。一个警告是,如果任何东西花费一个整数,它将显示取决于你的sql风格,因为“之间”有时是包容性的。

答案 6 :(得分:0)

create table offer_range (price_lower integer, price_upper integer);

insert into offer_range (price_lower, price_upper) 
values (0, 1), (1, 2), (2, 3), (3, 4), (4, 5), -- ...etc
;

select r.price_lower,
r.price_upper,
(
select count(*)
from offer o 
where o.price between r.price_lower and r.price_upper
) as number_of_offers
from offer_range r
;

答案 7 :(得分:0)

这个允许您设置单独的 PriceRange 表,其中包含每种产品的范围。

DECLARE @Offer TABLE(
     ID INT IDENTITY (1,1)
     ,Product_ID INT
     ,Price decimal(8,2)
)

DECLARE @PriceRange TABLE(
     ID INT IDENTITY (1,1)
     ,Product_ID INT
     ,price_lower decimal(8,2)
     ,price_upper decimal(8,2)
)

SELECT x.Product_ID,  x.price_lower, x.price_upper, COUNT(*) as "number_of_offers"
   FROM 
    (
    SELECT o.Product_ID, o.Price, p.price_lower, p.price_upper,
      CASE 
        WHEN o.Price >= p.price_lower and o.Price < p.price_upper THEN 1
        ELSE 0
      END AS InRange           
    FROM @Offer AS o
      JOIN  @PriceRange as p ON o.Product_ID = p.Product_ID
    ) AS x
WHERE x.InRange = 1
GROUP BY x.Product_ID, x.price_lower, x.price_upper

答案 8 :(得分:0)

如果您需要指定最大范围数,请尝试以下方法: 您可以指定最大价格范围数量,并自动创建组价格范围和总计。

declare @Max_Steps int
set @Max_Steps= 6

select Price_Group * data.Price_Step as price_lower, (Price_Group+1)*data.Price_Step as price_upper, count(id) as number_of_offers
from
(
    select id, price, ceiling(price/Price_Step) as Price_Group, Price_Step
    from
    (
        select cast((max(price) - min(price))/@Max_Steps as decimal(15,2))  as Price_Step
        FROM offer
    ) PriceRangeCalculator
cross join  
    (
        SELECT id, price
        FROM offer
    ) SourceData
) data
group by data.Price_Group, data.Price_Step
order by data.Price_Group