2014-04-05 54 views
0

結果表(CSV)看起來是這樣的:刪除SQL中的部分重複?

 
NAME    ,TITLE     ,YEAR ,QNTY ,CLUB   ,PRICE ,LOWEST_CLUB ,LOWEST 
Andy Aardverk  ,Avarice is Good   ,1998,1,Basic   ,218.95, CARP   ,215.95 
Andy Aardverk  ,Avarice is Good   ,1998,1,Basic   ,218.95, YRB Bronze  ,215.95 
Andy Aardverk  ,Yon-juu Hachi   ,1948,1,Basic   ,44.95, CARP   ,41.95 
Boswell Biddles  ,Not That London!   ,2003,1,Basic   ,12.5, CAA   ,10 
Boswell Biddles  ,Not That London!   ,2003,1,Basic   ,12.5, Readers Digest ,10 
Cary Cizek   ,Ringo to Nashi   ,1997,1,Basic   ,32.95, YRB Gold  ,29.95 
Cary Cizek   ,Ringo to Nashi   ,1997,1,Basic   ,32.95, York Club  ,29.95 
Cary Cizek   ,Toronto Underground  ,2001,1,YRB Gold  ,14.45, York Club  ,12.95 
Egbert Engles  ,Capricia's Conundrum  ,1993,1,CARP   ,13.45, Guelph Club ,12.95 
Egbert Engles  ,Tande mou nai   ,2002,1,Basic   ,112.95, Oprah   ,104.95 
Egbert Engles  ,Tande mou nai   ,2002,1,Basic   ,112.95, YRB Silver  ,104.95 
Ekksdwl Qjksynn  ,I don't think so   ,2001,1,YRB Gold  ,12.5, CAA   ,11.5 
George Wolf   ,Math is fun!    ,1995,1,YRB Silver  ,13.5, CAA   ,12 
Jack Daniels  ,Eigen Eigen    ,1980,1,York Club  ,57.95, Oprah   ,56.95 
Jack Daniels  ,Okay Why Not?   ,2001,1,York Club  ,18.45, Oprah   ,17.45 
Jackie Johassen  ,Getting into Snork U. ,2004,1,YRB Silver  ,21.95, Waterloo Club ,20.45 
Jackie Johassen  ,Not That London!   ,2003,1,Basic   ,12.5, CAA   ,10 
Klive Kittlehart ,Will Snoopy find Lucy? ,1990,1,YRB Bronze  ,14.95, YRB Gold  ,12.95 
Lux Luthor   ,Is Math is fun?   ,1996,1,Basic   ,72.95, Oprah   ,69.95 
Lux Luthor   ,Tropical Windsor   ,2004,1,Basic   ,18.95, Oprah   ,17.95 
Nigel Nerd   ,Are my feet too big?  ,1993,1,Basic   ,13.95, CAA   ,11.45 
Nigel Nerd   ,Dogs are not Cats  ,1995,1,Basic   ,35.95, UofT Club  ,32.95 
Phil Regis   ,Databases made Real Hard ,2002,1,Basic   ,39.95, Oprah   ,35.95 
Pretence Parker  ,Tchuss     ,2002,1,Basic   ,24.95, Guelph Club ,21.95 
Qfwfq    ,The Earth is not Enough ,2003,1,YRB Gold  ,37.37, Oprah   ,36.37 
Qfwfq    ,Under Your Bed   ,2004,1,Oprah   ,14.85, CAA   ,13.85 
Suzy Sedwick  ,Are my feet too big?  ,1993,1,YRB Silver  ,12.95, Oprah   ,11.45 
Tracy Turnip  ,Will Snoopy find Lucy? ,1990,1,Basic   ,15.95, Readers Digest ,13.95 
Tracy Turnip  ,Will Snoopy find Lucy? ,1990,1,Basic   ,15.95, YRB Silver  ,13.95 
Tracy Turnip  ,Yon-juu Hachi   ,1948,1,Readers Digest ,41, York Club  ,40.95 
Valerie Vixen  ,Base de Donne   ,2003,1,YRB Bronze  ,23.95, Readers Digest ,20.95 
Xia Xu    ,Where art thou Bertha? ,2003,1,Basic   ,30.95, CAA   ,26.95 
Yves Yonge   ,Radiator Barbecuing  ,2002,2,Basic   ,14.2, Waterloo Club ,12.2 
Zebulon Zilio  ,Transmorgifacation  ,2004,1,Basic   ,288.73, CAA   ,278.73 
    34 record(s) selected.,,,,,,, 

我希望能夠只顯示最低的選項之一。例如'Andy Aardverk'購買'Avarice is Good'並且可以以較低的價格從'CARP'或'YRB Bronze'購買。我只想要一個人展示它,因爲它可能是'鯉魚'或'YRB青銅',但不是兩者。

我嘗試「按組」上使用的姓名,名稱,年份,qnty,俱樂部,價格',但被賦予此錯誤:

'SQL0119N An expression starting with "LOWEST_CLUB" specified in a SELECT 
clause, HAVING clause, or ORDER BY clause is not specified in the GROUP BY 
clause or it is in a SELECT clause, HAVING clause, or ORDER BY clause with a 
column function and no GROUP BY clause is specified. SQLSTATE=42803' 

回答

0

這本來是與你的實際的查詢更容易,但無論如何,我會放棄它。 您可以通過使用CTE這樣解決這個問題:

;WITH CTE AS (
    SELECT 
     ROW_NUMBER() OVER(PARTITION BY [NAME], [TITLE] ORDER BY LOWEST_CLUB ASC, some_fallback_if_two_prices_are_the_same ASC) AS RowNumber, 
     [NAME], 
     [TITLE], 
     col1, 
     col2, 
     lowest_price, 
     some_fallback_if_two_prices_are_the_same 
    FROM [Table] 
) 
SELECT * --or rewrite your columns if you want to avoid the RowNumber 
FROM CTE 
WHERE RowNumber = 1; 

SELECT的CTE裏面應該是你當前的查詢+的ROW_NUMBER()線。 鑑於我沒有查詢,我無法給你最終結果。你將不得不擺弄它,直到它適合你。