开发者

SQL GROUP BY AND VALUE AND NON UNIQUE VALUES

开发者 https://www.devze.com 2023-04-05 14:07 出处:网络
This is my SQL: SELECT * FROM ( SELECT DISTINCT real_dep_date, real_price, resort_name, season_id, min_occ, free_sell,

This is my SQL:

SELECT *
FROM (
    SELECT DISTINCT real_dep_date,
           real_price,
           resort_name,
           season_id,
           min_occ,
           free_sell,
           MIN(real_price) OVER (PARTITION BY resort_name,real_dep_date) AS min_price
    开发者_如何学CFROM deals_panel_view
    WHERE ([1pax_disc] = [1pax_disc])
    AND   (real_dep_date >= season_start)
    AND   (season_name = 'winter 2012')
    AND   (chalet_url <> '')
    AND   (real_price <> 0)
    AND   (real_dep_date <= season_end)
    AND   (combined_chalet = 0)
    AND   (availability_spaces <> 0)
) deals_panel_view
WHERE min_price = real_price

This works but what happens is I get 3 results for a certain resort. This is because there are 3 chalets that are exactly the same.

I want to only show one, so I guess I would use a limit, but I don't know where.

I am using SQL Server 2005.

Any help would be great.


Try with this...

Select real_dep_date,
       real_price,
       resort_name,
       season_id,            
       min_occ,
       free_sell,
       min_price FROM (SELECT ROW_NUMBER() over (PARTITION BY resort_name,real_dep_date ORDER BY resort_name) as ROW,*
FROM (     
    SELECT DISTINCT real_dep_date,
           real_price,
           resort_name,
           season_id,            
           min_occ,
           free_sell,
           MIN(real_price) OVER (PARTITION BY resort_name,real_dep_date) AS min_price     
    FROM deals_panel_view     
    WHERE ([1pax_disc] = [1pax_disc])     
      AND (real_dep_date >= season_start)     
      AND (season_name = 'winter 2012')     
      AND (chalet_url <> '')     
      AND (real_price <> 0)     
      AND (real_dep_date <= season_end)     
      AND (combined_chalet = 0)     
      AND (availability_spaces <> 0) 
    ) deals_panel_view 
    WHERE min_price = real_price 
) Temp
where Row = 1
0

精彩评论

暂无评论...
验证码 换一张
取 消