2014-04-05 6 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'에서 구입할 수있었습니다. 나는 'CARP'또는 'YRB Bronze'가 될 수 있지만 둘 다가 아닌 단지 하나만 보여주고 싶습니다.

내가 '이름, 제목, 연도, 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; 

CTE에 내부 SELECT 현재 쿼리 +를 ROW_NUMBER() 라인되어야한다. 질문이 없으므로 최종 결과를 드릴 수는 없습니다. 그것이 당신을 위해 일할 때까지 당신은 그것으로 바이올린해야합니다.