<?xml version="1.0"?>
<rss version="2.0">
	<channel>
		<title>SQL Grouping</title>
		<link>http://www.allegro.cc/forums/view/597988</link>
		<description>Allegro.cc Forum Thread</description>
		<webMaster>matthew@allegro.cc (Matthew Leverton)</webMaster>
		<lastBuildDate>Thu, 16 Oct 2008 23:28:09 +0000</lastBuildDate>
	</channel>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>Just another general SQL question regarding grouping. Basically, I&#39;m pulling 25 rows from a table, and would like to group rows by a particular field (we&#39;ll call it field1). I&#39;d do something like this:</p><p><span class="source-code">SELECT <span class="k3">*</span>, COUNT<span class="k2">(</span>row_id<span class="k2">)</span> AS total FROM table GROUP BY field1 LIMIT <span class="n">25</span></span></p><p>This nicely clumps together the rows that share the same value in field1, leaving the total variable to signify how many rows have been grouped. However, what I&#39;m looking to do is as follows:</p><div class="source-code snippet"><div class="inner"><pre>  field1   <span class="k3">|</span>   field2 
<span class="k3">-</span><span class="k3">-</span><span class="k3">-</span><span class="k3">-</span><span class="k3">-</span><span class="k3">-</span><span class="k3">-</span><span class="k3">-</span><span class="k3">-</span><span class="k3">-</span><span class="k3">-</span><span class="k3">-</span><span class="k3">-</span><span class="k3">-</span><span class="k3">-</span><span class="k3">-</span><span class="k3">-</span><span class="k3">-</span><span class="k3">-</span><span class="k3">-</span><span class="k3">-</span><span class="k3">-</span><span class="k3">-</span>
   corn    <span class="k3">|</span>   pizza
   hotdog  <span class="k3">|</span>   cow
   corn    <span class="k3">|</span>   man
</pre></div></div><p>

The above query will take this result set and will group row 1 and 3 because they both share an identical value for field1. It will also return row 2. What&#39;s the best way to also grab each of the grouped rows other values? So effectively, if more than one row is found with an identical field1 value, I&#39;d like to be able to say:</p><p>Two rows had a value of corn in field1. These two rows had the following values for field2: pizza, man.</p><p>What&#39;s the best way to handle this?
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (ngiacomelli)</author>
		<pubDate>Thu, 16 Oct 2008 16:55:07 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><div class="quote_container"><div class="title">Quote:</div><div class="quote"><p>
What&#39;s the best way to handle this?
</p></div></div><p>
An embedded SQL select query when doing the WHERE clause.
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (Archon)</author>
		<pubDate>Thu, 16 Oct 2008 17:26:07 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>I have absolutely no concept of that. Any good online resources?
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (ngiacomelli)</author>
		<pubDate>Thu, 16 Oct 2008 17:41:59 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>Perhaps:</p><p>SELECT field1, GROUP_CONCAT(field2), COUNT(*) FROM foo GROUP BY field1
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (Matthew Leverton)</author>
		<pubDate>Thu, 16 Oct 2008 18:27:10 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>Or two queries if you need the individual values... <img src="http://www.allegro.cc/forums/smileys/huh.gif" alt="???" />
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (bamccaig)</author>
		<pubDate>Thu, 16 Oct 2008 23:28:09 +0000</pubDate>
	</item>
</rss>
