views:

1299

answers:

4

in Access DB... I need to extract the itemcode / desc combination for each itemcode in the following table where the desc has been used most frequently.

most frequently could possibly mean that there was only one version (see added record for pear)

in the case of itemcode 777, I'll have to decide later which description version to use. if there are multiple records, each containing a single version of a description, that will definately create an additional problem.

the original question should probably also include returning the first row for itemcodes like 777 where all the existing records for an itemcode contain a single, unique description (so that the count would always be 1). the first row may not always be the correct version - but I won't be able to automate that phase anyway.

---------------------
itemcode  | desc
---------------------
123       | apple
123       | apple
123       | apple
123       | apple 2
123       | apple-2
001       | orange
001       | orange
001       | ORANGE 1
001       | orange-1
666       | pear
777       | bananananana
777       | banana

so - I'm looking to end up with the following:

---------------------
itemcode  | desc
---------------------
123       | apple
001       | orange
666       | pear
777       | bananananana

I think I'm close, but the following only gets the description in the database which appears most frequently and only returns one row.

SELECT itemcode, desc, count(desc)
from table
group by itemcode, desc
having count(desc) = 
(
 select max(ct) from 
 (
  select itemcode, desc, count(desc) as ct
  from table
  group by itemcode, desc
  )
);

returns:

---------------------
itemcode  | desc
---------------------
123       | apple
A: 

Your query returns the MAX. Find a way to create a rule that would satisfy your requirements.

That "which appears most frequently" means what? appears>2? appears>3? appear>4?...

MarlonRibunal
It could only appear 1 time. I would want that as well.
42
Look at JP's answer. Maybe that will be enough for what you're looking for. :-)
MarlonRibunal
A: 
JP Alioto
This will not work. 123 Apple 50 times 123 Apple2 60 times 001 Orange 20 times - select top 2 and you get 2 entries for 123 but none for 001.
Daniel Brückner
sorry, that doesn't work for me.
42
Ah, I see know, okay.
JP Alioto
WTF? Posting T-SQL code will not go over well on a MS Access question..
DJ
A: 

Fixed and tested. It works as exspected - or better as designed, because it returns all rows if the highest count for an ItemCode appears several times.

SELECT ItemCode, ItemDescription, COUNT(ItemDescription) AS ItemCount
FROM Items I1
GROUP BY ItemCode, ItemDescription
HAVING COUNT(ItemDescription) = 
   (SELECT MAX(ItemCount)
      FROM (
         SELECT COUNT(ItemDescription) AS ItemCount
         FROM Items I2
         WHERE I2.ItemCode = I1.ItemCode
         GROUP BY ItemDescription
      ) I3
   )

UPDATE

Just simplifed the query a bit.

UPDATE

Unable to verify if it works with Access 2003. Tryed it, but Access keeps asking for I1.ItemCode.

Daniel Brückner
all the data is in one table. (not actually called table). this doesn't work for me.
42
Unfortunately, this won't work with Jet SQL. The engine does not support nested correlated sub-queries, AFAIR.
Tomalak
Just tested it with SQL 2005 and I don't use Access so I cannot say if it works with access.
Daniel Brückner
I know... The MS Jet engine is just not capable of some of the nicer things you can do on SQL server.
Tomalak
yep. access has really crippled my brain when it comes to sql.
42
+2  A: 

This would work through a correlated sub-query:

SELECT 
  t.itemcode, t.desc, Count(t.desc) AS CountOfdesc
FROM 
  [table] AS t
GROUP BY 
  t.itemcode, t.desc
HAVING 
  Count(t.desc) IN (
    SELECT TOP 1 
      Count(i.desc)
    FROM 
      [table] AS i
    WHERE 
      i.itemcode = t.itemcode
    GROUP BY 
      i.itemcode, i.desc
    ORDER BY 
      Count(i.desc) DESC
  )
  AND t.desc = (
    SELECT TOP 1 
      i.desc
    FROM 
      [table] AS i
    WHERE 
      i.itemcode = t.itemcode
    GROUP BY 
      i.itemcode, i.desc
    ORDER BY 
      i.desc
  )
;

Returns (tested with Access 2003):

itemcode  desc          CountOfdesc
001       orange        2
123       apple         3
666       pear          1
777       banana        1

BTW you should really not be calling a table "table" and a column "desc". Those are reserved SQL keywords, just avoid them to make your life easier.

Tomalak
"table" and "desc" were just stand-ins, not the real names.
42
@42: Good. :)
Tomalak
Yes, confirmed working in Access 2003. What version do you use?
Tomalak
Confirmed working as well
DJ
Brilliant! Super! Awesome!
42