<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	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/"
		>
<channel>
	<title>Comments on: SQL Query Query</title>
	<atom:link href="http://crookedtimber.org/2005/04/22/sql-query-query/feed/" rel="self" type="application/rss+xml" />
	<link>http://crookedtimber.org/2005/04/22/sql-query-query/</link>
	<description>Out of the crooked timber of humanity, no straight thing was ever made</description>
	<lastBuildDate>Sun, 21 Mar 2010 15:05:31 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.9.2</generator>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
		<item>
		<title>By: Kieran Healy</title>
		<link>http://crookedtimber.org/2005/04/22/sql-query-query/comment-page-1/#comment-69116</link>
		<dc:creator>Kieran Healy</dc:creator>
		<pubDate>Fri, 22 Apr 2005 23:56:55 +0000</pubDate>
		<guid isPermaLink="false">http://crookedtimber.org/2005/04/22/sql-query-query/#comment-69116</guid>
		<description>Problem solved! Thanks a million everyone -- followup &quot;here&quot;:http://crookedtimber.org/2005/04/22/crooked-timbers-field-of-positions/.</description>
		<content:encoded><![CDATA[	<p>Problem solved! Thanks a million everyone&#8212;followup <a href="http://crookedtimber.org/2005/04/22/crooked-timbers-field-of-positions/" title="">here</a>.</p>
 ]]></content:encoded>
	</item>
	<item>
		<title>By: Cranky Observer</title>
		<link>http://crookedtimber.org/2005/04/22/sql-query-query/comment-page-1/#comment-69101</link>
		<dc:creator>Cranky Observer</dc:creator>
		<pubDate>Fri, 22 Apr 2005 22:11:06 +0000</pubDate>
		<guid isPermaLink="false">http://crookedtimber.org/2005/04/22/sql-query-query/#comment-69101</guid>
		<description>Kieran,
Problem solved?  Or do you need more help?

Cranky</description>
		<content:encoded><![CDATA[	<p>Kieran,<br />
Problem solved?  Or do you need more help?</p>

	<p>Cranky</p>
 ]]></content:encoded>
	</item>
	<item>
		<title>By: KCinDC</title>
		<link>http://crookedtimber.org/2005/04/22/sql-query-query/comment-page-1/#comment-69088</link>
		<dc:creator>KCinDC</dc:creator>
		<pubDate>Fri, 22 Apr 2005 20:31:28 +0000</pubDate>
		<guid isPermaLink="false">http://crookedtimber.org/2005/04/22/sql-query-query/#comment-69088</guid>
		<description>5.0? We&#039;re still cautiously examining 4.1.</description>
		<content:encoded><![CDATA[	<p>5.0? We&#8217;re still cautiously examining 4.1.</p>
 ]]></content:encoded>
	</item>
	<item>
		<title>By: jet</title>
		<link>http://crookedtimber.org/2005/04/22/sql-query-query/comment-page-1/#comment-69081</link>
		<dc:creator>jet</dc:creator>
		<pubDate>Fri, 22 Apr 2005 20:11:35 +0000</pubDate>
		<guid isPermaLink="false">http://crookedtimber.org/2005/04/22/sql-query-query/#comment-69081</guid>
		<description>Anyone here had good vibes using MySQL 5.0?  </description>
		<content:encoded><![CDATA[	<p>Anyone here had good vibes using MySQL 5.0?</p>
 ]]></content:encoded>
	</item>
	<item>
		<title>By: Scott Swank</title>
		<link>http://crookedtimber.org/2005/04/22/sql-query-query/comment-page-1/#comment-69032</link>
		<dc:creator>Scott Swank</dc:creator>
		<pubDate>Fri, 22 Apr 2005 17:54:33 +0000</pubDate>
		<guid isPermaLink="false">http://crookedtimber.org/2005/04/22/sql-query-query/#comment-69032</guid>
		<description>You can also head over to this site:

http://pipetalk.quest-pipelines.com/default.asp?boardid=mysql

And ask questions about mysql that a very knowledgable fellow named Andrew Gilfrin (among others) will answer for you.  Tell him I sent you.  :)</description>
		<content:encoded><![CDATA[	<p>You can also head over to this site:</p>

	<p><a href="http://pipetalk.quest-pipelines.com/default.asp?boardid=mysql" rel="nofollow">http://pipetalk.quest-pipelines.com/default.asp?boardid=mysql</a></p>

	<p>And ask questions about mysql that a very knowledgable fellow named Andrew Gilfrin (among others) will answer for you.  Tell him I sent you.  :)</p>
 ]]></content:encoded>
	</item>
	<item>
		<title>By: abb1</title>
		<link>http://crookedtimber.org/2005/04/22/sql-query-query/comment-page-1/#comment-69030</link>
		<dc:creator>abb1</dc:creator>
		<pubDate>Fri, 22 Apr 2005 17:46:30 +0000</pubDate>
		<guid isPermaLink="false">http://crookedtimber.org/2005/04/22/sql-query-query/#comment-69030</guid>
		<description>Yeah, I don&#039;t do MySQL, but I suspect that&#039;s right: in MySQL you probably need to do explicit joins rather than FROM table_name, table_name, table_name WHERE.</description>
		<content:encoded><![CDATA[	<p>Yeah, I don&#8217;t do MySQL, but I suspect that&#8217;s right: in MySQL you probably need to do explicit joins rather than <span class="caps">FROM</span> table_name, table_name, table_name <span class="caps">WHERE</span>.</p>
 ]]></content:encoded>
	</item>
	<item>
		<title>By: KCinDC</title>
		<link>http://crookedtimber.org/2005/04/22/sql-query-query/comment-page-1/#comment-69026</link>
		<dc:creator>KCinDC</dc:creator>
		<pubDate>Fri, 22 Apr 2005 17:44:20 +0000</pubDate>
		<guid isPermaLink="false">http://crookedtimber.org/2005/04/22/sql-query-query/#comment-69026</guid>
		<description>Argh, in the second query it should actually be SUM, not COUNT.</description>
		<content:encoded><![CDATA[	<p>Argh, in the second query it should actually be <span class="caps">SUM</span>, not <span class="caps">COUNT</span>.</p>
 ]]></content:encoded>
	</item>
	<item>
		<title>By: KCinDC</title>
		<link>http://crookedtimber.org/2005/04/22/sql-query-query/comment-page-1/#comment-69024</link>
		<dc:creator>KCinDC</dc:creator>
		<pubDate>Fri, 22 Apr 2005 17:43:23 +0000</pubDate>
		<guid isPermaLink="false">http://crookedtimber.org/2005/04/22/sql-query-query/#comment-69024</guid>
		<description>Ugh, the COUNT stuff gets eaten. Not sure what&#039;s triggering it.  In the first query it should be COUNT[*] with parentheses in place of brackets, and in the second it&#039;s supposed to be COUNT[ c.category_nicename = &#039;category_name1&#039; ], for example.

Crossing my fingers that the brackets survive posting.</description>
		<content:encoded><![CDATA[	<p>Ugh, the <span class="caps">COUNT</span> stuff gets eaten. Not sure what&#8217;s triggering it.  In the first query it should be <span class="caps">COUNT</span>[*] with parentheses in place of brackets, and in the second it&#8217;s supposed to be <span class="caps">COUNT</span>[ c.category_nicename = &#8216;category_name1&#8217; ], for example.</p>

	<p>Crossing my fingers that the brackets survive posting.</p>
 ]]></content:encoded>
	</item>
	<item>
		<title>By: Cranky Observer</title>
		<link>http://crookedtimber.org/2005/04/22/sql-query-query/comment-page-1/#comment-69023</link>
		<dc:creator>Cranky Observer</dc:creator>
		<pubDate>Fri, 22 Apr 2005 17:43:21 +0000</pubDate>
		<guid isPermaLink="false">http://crookedtimber.org/2005/04/22/sql-query-query/#comment-69023</guid>
		<description>Scott&#039;s example looks good.  Just check to be sure it isn&#039;t giving you multiple copies of the same summary (he used a different approach from what I would have used, and I don&#039;t have a MySQL handy to test).  If it doesn&#039;t work let me know.

_A Visual Introduction to SQL_ by Chappel et. al. is a very straightforward, readable, and usable introduction to this kind of stuff.  It has been in print for 20 years so I guess others think the same!  I actually prefer the 1st edition (blue cover) to the 2nd edition (white and green cover) if you can find one used.

Cranky

</description>
		<content:encoded><![CDATA[	<p>Scott&#8217;s example looks good.  Just check to be sure it isn&#8217;t giving you multiple copies of the same summary (he used a different approach from what I would have used, and I don&#8217;t have a MySQL handy to test).  If it doesn&#8217;t work let me know.</p>

	<p><em>A Visual Introduction to <span class="caps">SQL</span></em> by Chappel et. al. is a very straightforward, readable, and usable introduction to this kind of stuff.  It has been in print for 20 years so I guess others think the same!  I actually prefer the 1st edition (blue cover) to the 2nd edition (white and green cover) if you can find one used.</p>

	<p>Cranky</p>
 ]]></content:encoded>
	</item>
	<item>
		<title>By: KCinDC</title>
		<link>http://crookedtimber.org/2005/04/22/sql-query-query/comment-page-1/#comment-69020</link>
		<dc:creator>KCinDC</dc:creator>
		<pubDate>Fri, 22 Apr 2005 17:37:24 +0000</pubDate>
		<guid isPermaLink="false">http://crookedtimber.org/2005/04/22/sql-query-query/#comment-69020</guid>
		<description>This gets the needed data (I just tried it on a WordPress blog I manage):

SELECT u.user_nickname, c.category_nicename, COUNT(*) FROM `wp_users` u INNER JOIN wp_posts p ON u.ID = p.post_author INNER JOIN wp_post2cat pc ON p.ID = pc.post_id INNER JOIN wp_categories c ON pc.category_id = c.cat_ID GROUP BY u.user_nickname, c.category_nicename

It&#039;s not in the table format you want, but I&#039;d do that part in PHP or something rather than trying to do it in the query -- which you could do with something long and hard-coded like 

SELECT u.user_nickname,
COUNT( category_nicename = &#039;category_name1&#039;) AS category_name1_count,
COUNT( category_nicename = &#039;category_name2&#039;) AS category_name2_count,
...
FROM `wp_users` u INNER JOIN wp_posts p ON u.ID = p.post_author INNER JOIN wp_post2cat pc ON p.ID = pc.post_id INNER JOIN wp_categories c ON pc.category_id = c.cat_ID GROUP BY u.user_nickname</description>
		<content:encoded><![CDATA[	<p>This gets the needed data (I just tried it on a WordPress blog I manage):</p>

	<p><span class="caps">SELECT</span> u.user_nickname, c.category_nicename, <acronym title="*">COUNT</acronym> <span class="caps">FROM </span>`wp_users` u <span class="caps">INNER JOIN</span> wp_posts p ON u.ID = p.post_author <span class="caps">INNER JOIN</span> wp_post2cat pc ON p.ID = pc.post_id <span class="caps">INNER JOIN</span> wp_categories c ON pc.category_id = c.cat_ID <span class="caps">GROUP BY</span> u.user_nickname, c.category_nicename</p>

	<p>It&#8217;s not in the table format you want, but I&#8217;d do that part in <span class="caps">PHP</span> or something rather than trying to do it in the query&#8212;which you could do with something long and hard-coded like</p>

	<p><span class="caps">SELECT</span> u.user_nickname,<br />
<acronym title=" category_nicename = &#8216;category_name1&#8217;">COUNT</acronym> AS category_name1_count,<br />
<acronym title=" category_nicename = &#8216;category_name2&#8217;">COUNT</acronym> AS category_name2_count,<br />
&#8230;<br />
<span class="caps">FROM </span>`wp_users` u <span class="caps">INNER JOIN</span> wp_posts p ON u.ID = p.post_author <span class="caps">INNER JOIN</span> wp_post2cat pc ON p.ID = pc.post_id <span class="caps">INNER JOIN</span> wp_categories c ON pc.category_id = c.cat_ID <span class="caps">GROUP BY</span> u.user_nickname</p>
 ]]></content:encoded>
	</item>
	<item>
		<title>By: Kieran Healy</title>
		<link>http://crookedtimber.org/2005/04/22/sql-query-query/comment-page-1/#comment-69018</link>
		<dc:creator>Kieran Healy</dc:creator>
		<pubDate>Fri, 22 Apr 2005 17:35:26 +0000</pubDate>
		<guid isPermaLink="false">http://crookedtimber.org/2005/04/22/sql-query-query/#comment-69018</guid>
		<description>Hey Cranky, that works. Cool. How do I include the fancy names?</description>
		<content:encoded><![CDATA[	<p>Hey Cranky, that works. Cool. How do I include the fancy names?</p>
 ]]></content:encoded>
	</item>
	<item>
		<title>By: Scott Swank</title>
		<link>http://crookedtimber.org/2005/04/22/sql-query-query/comment-page-1/#comment-69015</link>
		<dc:creator>Scott Swank</dc:creator>
		<pubDate>Fri, 22 Apr 2005 17:33:39 +0000</pubDate>
		<guid isPermaLink="false">http://crookedtimber.org/2005/04/22/sql-query-query/#comment-69015</guid>
		<description>Hmm, word press doesn&#039;t like my sql statements and mangled them.  I&#039;ll e-mail them to you directly.</description>
		<content:encoded><![CDATA[	<p>Hmm, word press doesn&#8217;t like my sql statements and mangled them.  I&#8217;ll e-mail them to you directly.</p>
 ]]></content:encoded>
	</item>
	<item>
		<title>By: Scott Swank</title>
		<link>http://crookedtimber.org/2005/04/22/sql-query-query/comment-page-1/#comment-69011</link>
		<dc:creator>Scott Swank</dc:creator>
		<pubDate>Fri, 22 Apr 2005 17:30:28 +0000</pubDate>
		<guid isPermaLink="false">http://crookedtimber.org/2005/04/22/sql-query-query/#comment-69011</guid>
		<description>Kieran,

Start out with the following.  I won&#039;t guarantee that it&#039;s valid mysql syntax (though it is correct sql syntax) -- but it should be close enough that anyone that&#039;s worked with mysql even a bit can get it the rest of the way to a correct phrasing.

SELECT u.user-nicename,
       c.category-nicename
  FROM wp-users  u,
       wp-posts  p,
       wp-post2cat  pc,
       wp-categories  c
 WHERE p.post-author = u.id
   AND pc.post-id = p.id
   AND c.cat-id = pc.category-id;

This should give you all of the author/category pairings.  Now to summarize them and count combinations you change this to the following:

SELECT u.user-nicename,
       c.category-nicename,
       COUNT(*) AS post-cnt
  FROM wp-users  u,
       wp-posts  p,
       wp-post2cat  pc,
       wp-categories  c
 WHERE p.post-author = u.id
   AND pc.post-id = p.id
   AND c.cat-id = pc.category-id
 GROUP BY
       u.user-nicename,
       c.category-nicename;

If you&#039;ve gotten this far, then you almost certainly want to simply pull the results into a tool like Excel and pivot things from there.  If you&#039;re feeling bold, then you can...

SELECT u.user-nicename,
       COUNT(DECODE(c.category-nicename, &#039;academia&#039;, &#039;x&#039;)) AS academia-cnt,
       COUNT(DECODE(c.category-nicename, &#039;blogging&#039;, &#039;x&#039;)) AS blogging-cnt,
       COUNT(DECODE(c.category-nicename, &#039;etc&#039;, &#039;x&#039;)) AS etc-cnt
  FROM wp-users  u,
       wp-posts  p,
       wp-post2cat  pc,
       wp-categories  c
 WHERE p.post-author = u.id
   AND pc.post-id = p.id
   AND c.cat-id = pc.category-id
 GROUP BY
       u.user-nicename;

This is saying: 1) group by user, 2) count occurances of &quot;this funny decode thing&quot;.  I won&#039;t guarantee that mysql supports the function decode(), but here&#039;s what it does so that you can look for a suitable replacement.  It maps a value, in our case category, to a list of pairs of values.  In the first case we want to compare the category with &#039;academia&#039; and if it matches then we count up &#039;x&#039;, if it does not match then we count up nothing (called NULL in databases).  So this counts the number of occurances of &#039;academia&#039;.  The next column similarly counts the number of occurances of &#039;blogging&#039;, and you would add additional columns similiarly.

As the cranky indicated though, you&#039;ll have to keep this up to date manually as you add/remove categories. 

Cheers,
Scott</description>
		<content:encoded><![CDATA[	<p>Kieran,</p>

	<p>Start out with the following.  I won&#8217;t guarantee that it&#8217;s valid mysql syntax (though it is correct sql syntax)&#8212;but it should be close enough that anyone that&#8217;s worked with mysql even a bit can get it the rest of the way to a correct phrasing.</p>

	<p><span class="caps">SELECT</span> u.user-nicename,<br />
c.category-nicename<br />
<span class="caps">FROM</span> wp-users  u,<br />
wp-posts  p,<br />
wp-post2cat  pc,<br />
wp-categories  c<br />
<span class="caps">WHERE</span> p.post-author = u.id<br />
<span class="caps">AND</span> pc.post-id = p.id<br />
<span class="caps">AND</span> c.cat-id = pc.category-id;</p>

	<p>This should give you all of the author/category pairings.  Now to summarize them and count combinations you change this to the following:</p>

	<p><span class="caps">SELECT</span> u.user-nicename,<br />
c.category-nicename,<br />
<acronym title="*">COUNT</acronym> AS post-cnt<br />
<span class="caps">FROM</span> wp-users  u,<br />
wp-posts  p,<br />
wp-post2cat  pc,<br />
wp-categories  c<br />
<span class="caps">WHERE</span> p.post-author = u.id<br />
<span class="caps">AND</span> pc.post-id = p.id<br />
<span class="caps">AND</span> c.cat-id = pc.category-id<br />
<span class="caps">GROUP BY</span><br />
u.user-nicename,<br />
c.category-nicename;</p>

	<p>If you&#8217;ve gotten this far, then you almost certainly want to simply pull the results into a tool like Excel and pivot things from there.  If you&#8217;re feeling bold, then you can&#8230;</p>

	<p><span class="caps">SELECT</span> u.user-nicename,<br />
<acronym title="DECODE(c.category-nicename, &#8216;academia&#8217;, &#8216;x&#8217;">COUNT</acronym>) AS academia-cnt,<br />
<acronym title="DECODE(c.category-nicename, &#8216;blogging&#8217;, &#8216;x&#8217;">COUNT</acronym>) AS blogging-cnt,<br />
<acronym title="DECODE(c.category-nicename, &#8216;etc&#8217;, &#8216;x&#8217;">COUNT</acronym>) AS etc-cnt<br />
<span class="caps">FROM</span> wp-users  u,<br />
wp-posts  p,<br />
wp-post2cat  pc,<br />
wp-categories  c<br />
<span class="caps">WHERE</span> p.post-author = u.id<br />
<span class="caps">AND</span> pc.post-id = p.id<br />
<span class="caps">AND</span> c.cat-id = pc.category-id<br />
<span class="caps">GROUP BY</span><br />
u.user-nicename;</p>

	<p>This is saying: 1) group by user, 2) count occurances of &#8220;this funny decode thing&#8221;.  I won&#8217;t guarantee that mysql supports the function decode(), but here&#8217;s what it does so that you can look for a suitable replacement.  It maps a value, in our case category, to a list of pairs of values.  In the first case we want to compare the category with &#8216;academia&#8217; and if it matches then we count up &#8216;x&#8217;, if it does not match then we count up nothing (called <span class="caps">NULL</span> in databases).  So this counts the number of occurances of &#8216;academia&#8217;.  The next column similarly counts the number of occurances of &#8216;blogging&#8217;, and you would add additional columns similiarly.</p>

	<p>As the cranky indicated though, you&#8217;ll have to keep this up to date manually as you add/remove categories.</p>

	<p>Cheers,<br />
Scott</p>
 ]]></content:encoded>
	</item>
	<item>
		<title>By: Cranky Observer</title>
		<link>http://crookedtimber.org/2005/04/22/sql-query-query/comment-page-1/#comment-69010</link>
		<dc:creator>Cranky Observer</dc:creator>
		<pubDate>Fri, 22 Apr 2005 17:25:20 +0000</pubDate>
		<guid isPermaLink="false">http://crookedtimber.org/2005/04/22/sql-query-query/#comment-69010</guid>
		<description>Here is an attempt, trying to be as vendor-neutral as I can.  Formatting will get screwed up when I post though.

select wp-posts.post-author,
       wp-post2cat.category-id,
       count(*)
   from wp-posts,
        wp-post2cat
   where wp-posts.ID = wp-post2cat.post-id
   group by wp-posts.post-author,
            wp-post2cat.category-id
   order by wp-posts.post-author,
            wp-post2cat.category-id


Why don&#039;t you see if that works, and if so I can add in the fancy names if you want.

Cranky</description>
		<content:encoded><![CDATA[	<p>Here is an attempt, trying to be as vendor-neutral as I can.  Formatting will get screwed up when I post though.</p>

	<p>select wp-posts.post-author,<br />
wp-post2cat.category-id,<br />
count(*)<br />
from wp-posts,<br />
wp-post2cat<br />
where wp-posts.ID = wp-post2cat.post-id<br />
group by wp-posts.post-author,<br />
wp-post2cat.category-id<br />
order by wp-posts.post-author,<br />
wp-post2cat.category-id</p>


	<p>Why don&#8217;t you see if that works, and if so I can add in the fancy names if you want.</p>

	<p>Cranky</p>
 ]]></content:encoded>
	</item>
	<item>
		<title>By: Jayanne</title>
		<link>http://crookedtimber.org/2005/04/22/sql-query-query/comment-page-1/#comment-69008</link>
		<dc:creator>Jayanne</dc:creator>
		<pubDate>Fri, 22 Apr 2005 17:22:54 +0000</pubDate>
		<guid isPermaLink="false">http://crookedtimber.org/2005/04/22/sql-query-query/#comment-69008</guid>
		<description>OT, but I could do with some blogging software help! Is there a help forum for Typepad people? -- Blogger and Tripod are so slow it&#039;s a struggle to post there

TIA</description>
		<content:encoded><![CDATA[	<p>OT, but I could do with some blogging software help! Is there a help forum for Typepad people?&#8212;Blogger and Tripod are so slow it&#8217;s a struggle to post there</p>

	<p><span class="caps">TIA</span></p>
 ]]></content:encoded>
	</item>
</channel>
</rss>
