Re: how do i count() similar items

Поиск
Список
Период
Сортировка
От Bill Moran
Тема Re: how do i count() similar items
Дата
Msg-id 20100908143544.9a5b1025.wmoran@potentialtech.com
обсуждение исходный текст
Ответ на how do i count() similar items  (jackassplus <jackassplus@gmail.com>)
Список pgsql-general
In response to jackassplus <jackassplus@gmail.com>:

> lets say I have the following in the 'fruits' table:
>
> Round orange
> Sunkist orange
> navel orange
> strawberry
> blueberry
> sunkist orange
> apple
>
> how would I get something like the following:
>
> count as c | Fruit type
> ---------------------------------
> 4              | orange
> 2              | berry
> 1              | apple

Organize your data better.

If you want to analyze data in the way you describe, then you need to store
the data in a way that makes in analyzable.  I'm 100% sure that someone can
provide you with some query magic that will make the example you provided
produce the results you're wanting.  I'm also 100% sure that a few weeks
or months down the line you find exceptions that will break that solution,
and that said solution will never really be reliable.

If you're going to analyze fruits by type, then you need a type column.
You can then group by type to get count()s.  To ensure data integrity,
you should probably create a fruit_type table with a unique column that
lists the possible types, and then foreign key the fruit_type column in
the fruits table to that to ensure nothing funky is entered.  An enum
for type is another possibility.

--
Bill Moran
http://www.potentialtech.com
http://people.collaborativefusion.com/~wmoran/

В списке pgsql-general по дате отправления:

Предыдущее
От: Brar Piening
Дата:
Сообщение: Re: Postgres 32bit on Windows 64bit, related components
Следующее
От: "Donald Catanzaro, PhD"
Дата:
Сообщение: Re: Copy From csv file with double quotes as null