2011-09-15 106 views
0

這是我的SQL:SQL GROUP BY和價值,非唯一值

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.

任何幫助都會很好。

+0

也許這三個小屋都沒有確切* *相同。如果他們是,DISTINCT會消除重複。他們可能在某些屬性或屬性上有所不同(顯然,您選擇的部分屬性)。 –

回答

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 
+0

似乎是格式問題 – richard

+0

用於SQL Server的Microsoft OLE DB提供程序錯誤'80040e14' 'PARTITION'附近的語法錯誤。 – richard

+0

關鍵字'BY'附近的語法不正確。我錯過了逗號現在得到這個 – richard