SQL GROUP BY和VALUE以及非独特的值

时间:2011-09-15 09:46:32

标签: sql sql-server-2005

这是我的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
    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

这样可行,但是我会得到3个结果。这是因为有3个小木屋完全相同。

我想只显示一个,所以我想我会使用限制,但我不知道在哪里。

我正在使用SQL Server 2005。

任何帮助都会很棒。

1 个答案:

答案 0 :(得分:0)

试试这个...

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