Question

The resulting table (CSV) looks like this:

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.,,,,,,,

I want to be able to only show one of the lowest options. For example 'Andy Aardverk' has purchased 'Avarice is Good' and could have bought it from 'CARP' or 'YRB Bronze' for a lower price. I only want one to show so it could be 'CARP' or 'YRB Bronze' but not both.

I tried to use 'group by' on 'name, title, year, qnty, club, price' but was given this error:

 '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'
Was it helpful?

Solution

It would have been easier with your actual query, but I'll give it a go anyways. You can solve this problem by using a CTE like this:

;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;

That SELECT inside the CTE should be your current query + the ROW_NUMBER() line. Seeing as I don't have the query, I can't give you a final result. You'll have to fiddle with it until it works for you.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top