<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Technical stuff &#187; mySQL</title>
	<atom:link href="http://www.tekkie.ro/tag/mysql/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.tekkie.ro</link>
	<description>Requirements are like water. They&#039;re easier to build on when they&#039;re frozen.</description>
	<lastBuildDate>Fri, 11 Jun 2010 11:33:21 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=abc</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>mySQL: find out distinct values of a column and the number of their occurrences</title>
		<link>http://www.tekkie.ro/software/mysql-find-out-distinct-values-column-number-their-occurrences/</link>
		<comments>http://www.tekkie.ro/software/mysql-find-out-distinct-values-column-number-their-occurrences/#comments</comments>
		<pubDate>Sat, 20 Jun 2009 12:33:05 +0000</pubDate>
		<dc:creator>Georgiana</dc:creator>
				<category><![CDATA[Quick and dirty]]></category>
		<category><![CDATA[Software]]></category>
		<category><![CDATA[testing]]></category>
		<category><![CDATA[mySQL]]></category>
		<category><![CDATA[quickies]]></category>

		<guid isPermaLink="false">http://www.tekkie.ro/?p=178</guid>
		<description><![CDATA[So, we need to find out different values of a column and the number of their occurrences. Let&#8217;s set up a small test first:

-- Step #1: prepare the ground
CREATE DATABASE IF NOT EXISTS `test`;

DROP TABLE IF EXISTS `test`.`test_count_distinct`;

CREATE TABLE `test`.`test_count_distinct` (
  `id` INT(11) NOT NULL auto_increment
      PRIMARY KEY,
  [...]]]></description>
			<content:encoded><![CDATA[<p>So, we need to find out different values of a column and the number of their occurrences. Let&#8217;s set up a small test first:</p>
<pre class="brush: sql">
-- Step #1: prepare the ground
CREATE DATABASE IF NOT EXISTS `test`;

DROP TABLE IF EXISTS `test`.`test_count_distinct`;

CREATE TABLE `test`.`test_count_distinct` (
  `id` INT(11) NOT NULL auto_increment
      PRIMARY KEY,
  `title` VARCHAR(50) NOT NULL DEFAULT &#039;&#039;,
  `date_created` TIMESTAMP NOT NULL DEFAULT 0,
  `date_updated` TIMESTAMP NOT NULL
      DEFAULT CURRENT_TIMESTAMP
      ON UPDATE CURRENT_TIMESTAMP
);

-- using NULL insert for a timestamp column is
--    just like using NOW()
-- @link http://dev.mysql.com/doc/refman/4.1/en/timestamp.html
INSERT INTO `test`.`test_count_distinct` ( `title`, `date_created` )
VALUES
  ( &#039;value&#039;,   NULL )
, ( &#039;value 1&#039;, NULL )
, ( &#039;value 2&#039;, NULL )
, ( &#039;value&#039;,   NULL )
, ( &#039;value 1&#039;, NULL )
, ( &#039;value 2&#039;, NULL )
, ( &#039;value&#039;,   NULL )
, ( &#039;value 3&#039;, NULL )
;
</pre>
<p>Now let&#8217;s find out how many different values are there in the `title` column, and how many times each value occurs:</p>
<pre class="brush: sql">
-- Step #2: do work
--          here is where we find out
--          different values of `title` and
--          the number of their occurrences
SELECT
        DISTINCT `t`.`title` AS `title`,
        COUNT( `t`.`title`) AS `cnt`
    FROM `test`.`test_count_distinct` t
    GROUP BY `title`
    ORDER BY `cnt` DESC
;
</pre>
<p>And the result will look like:</p>
<pre class="brush: sql">
+---------+-----+
| title   | cnt |
+---------+-----+
| value   |   3 |
| value 2 |   2 |
| value 1 |   2 |
| value 3 |   1 |
+---------+-----+
4 rows in set (0.00 sec)
</pre>
<p>You can get the code <a href="http://www.tekkie.ro/wp-content/uploads/2009/06/count_distinct.sql" title="mySQL code for count distinct test">here</a>.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.tekkie.ro/software/mysql-find-out-distinct-values-column-number-their-occurrences/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
	</channel>
</rss>
