<?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>GROUP_CONCAT &#8211; Database Research &amp; Development</title>
	<atom:link href="https://www.dbrnd.com/tag/group_concat/feed/" rel="self" type="application/rss+xml" />
	<link>https://www.dbrnd.com</link>
	<description>BIGData &#124; NoSQL &#124; MSSQL &#124; MySQL &#124; PostgreSQL</description>
	<lastBuildDate>Tue, 21 Nov 2017 06:18:01 +0000</lastBuildDate>
	<language>en-US</language>
	<sy:updatePeriod>
	hourly	</sy:updatePeriod>
	<sy:updateFrequency>
	1	</sy:updateFrequency>
	<generator>https://wordpress.org/?v=7.0.4</generator>
	<item>
		<title>Find top N records for each group in MySQL</title>
		<link>https://www.dbrnd.com/2015/08/find-top-n-records-for-each-group-in-mysql/</link>
		
		<dc:creator><![CDATA[Anvesh Patel]]></dc:creator>
		<pubDate>Sun, 30 Aug 2015 15:44:45 +0000</pubDate>
				<category><![CDATA[MySQL]]></category>
		<category><![CDATA[MySQL DBA Script]]></category>
		<category><![CDATA[Script]]></category>
		<category><![CDATA[Anvesh Patel]]></category>
		<category><![CDATA[database]]></category>
		<category><![CDATA[database research and development]]></category>
		<category><![CDATA[FIND_IN_SET]]></category>
		<category><![CDATA[GROUP_CONCAT]]></category>
		<category><![CDATA[MySQL Command]]></category>
		<category><![CDATA[MySQL Database Administrator]]></category>
		<category><![CDATA[MySQL Database Designing]]></category>
		<category><![CDATA[MySQL Database Programming]]></category>
		<category><![CDATA[MySQL Error]]></category>
		<category><![CDATA[MySQL Performance Tunning]]></category>
		<category><![CDATA[MySQL Query]]></category>
		<category><![CDATA[MySQL Tips and Tricks]]></category>
		<guid isPermaLink="false">https://www.dbrnd.com/?p=464</guid>

					<description><![CDATA[In this post, I am sharing a demonstration on finding top N records for each group in MySQL. Today morning, I was working on one of the report and requirement was to find top 2 employee working hours for each department. I tried many scripts and finally got the solution. Create a table with sample [&#8230;]]]></description>
										<content:encoded><![CDATA[<p><a class="a2a_button_facebook" href="https://www.addtoany.com/add_to/facebook?linkurl=https%3A%2F%2Fwww.dbrnd.com%2F2015%2F08%2Ffind-top-n-records-for-each-group-in-mysql%2F&amp;linkname=Find%20top%20N%20records%20for%20each%20group%20in%20MySQL" title="Facebook" rel="nofollow noopener" target="_blank"></a><a class="a2a_button_twitter" href="https://www.addtoany.com/add_to/twitter?linkurl=https%3A%2F%2Fwww.dbrnd.com%2F2015%2F08%2Ffind-top-n-records-for-each-group-in-mysql%2F&amp;linkname=Find%20top%20N%20records%20for%20each%20group%20in%20MySQL" title="Twitter" rel="nofollow noopener" target="_blank"></a><a class="a2a_button_linkedin" href="https://www.addtoany.com/add_to/linkedin?linkurl=https%3A%2F%2Fwww.dbrnd.com%2F2015%2F08%2Ffind-top-n-records-for-each-group-in-mysql%2F&amp;linkname=Find%20top%20N%20records%20for%20each%20group%20in%20MySQL" title="LinkedIn" rel="nofollow noopener" target="_blank"></a><a class="a2a_button_pinterest" href="https://www.addtoany.com/add_to/pinterest?linkurl=https%3A%2F%2Fwww.dbrnd.com%2F2015%2F08%2Ffind-top-n-records-for-each-group-in-mysql%2F&amp;linkname=Find%20top%20N%20records%20for%20each%20group%20in%20MySQL" title="Pinterest" rel="nofollow noopener" target="_blank"></a><a class="a2a_button_reddit" href="https://www.addtoany.com/add_to/reddit?linkurl=https%3A%2F%2Fwww.dbrnd.com%2F2015%2F08%2Ffind-top-n-records-for-each-group-in-mysql%2F&amp;linkname=Find%20top%20N%20records%20for%20each%20group%20in%20MySQL" title="Reddit" rel="nofollow noopener" target="_blank"></a><a class="a2a_button_pocket" href="https://www.addtoany.com/add_to/pocket?linkurl=https%3A%2F%2Fwww.dbrnd.com%2F2015%2F08%2Ffind-top-n-records-for-each-group-in-mysql%2F&amp;linkname=Find%20top%20N%20records%20for%20each%20group%20in%20MySQL" title="Pocket" rel="nofollow noopener" target="_blank"></a><a class="a2a_dd addtoany_share_save addtoany_share" href="https://www.addtoany.com/share#url=https%3A%2F%2Fwww.dbrnd.com%2F2015%2F08%2Ffind-top-n-records-for-each-group-in-mysql%2F&#038;title=Find%20top%20N%20records%20for%20each%20group%20in%20MySQL" data-a2a-url="https://www.dbrnd.com/2015/08/find-top-n-records-for-each-group-in-mysql/" data-a2a-title="Find top N records for each group in MySQL"></a></p><p>In this post, I am sharing a demonstration on finding top N records for each group in MySQL.</p>
<p>Today morning, I was working on one of the report and requirement was to find top 2 employee working hours for each department.</p>
<p>I tried many scripts and finally got the solution.</p>
<p><strong>Create a table with sample data:</strong></p><pre class="crayon-plain-tag">CREATE DATABASE Employee;

CREATE TABLE Employee.EmployeePivotTest
(
     EmpName VARCHAR(255)
    ,EmpDeptName VARCHAR(255)
    ,EmpAvgWorkingHours INTEGER
);

INSERT INTO Employee.EmployeePivotTest VALUES
('Anvesh','Computer-IT',226)
,('Anvesh','Computer-IT',100)
,('Anvesh','Computer-IT',200)
,('Anvesh','Computer-IT',752)
,('Anvesh','Account',142)
,('Anvesh','Marketing',110)
,('Anvesh','Finance',236)
,('Anvesh','Account',120)
,('Neevan','Computer-IT',120)
,('Neevan','Finance',852)
,('Neevan','Account',326)
,('Neevan','Marketing',50)
,('Neevan','Finance',140);</pre><p>
<p><strong>Find a top N working hour by each employee:</strong></p><pre class="crayon-plain-tag">SELECT 
    EP.EmpName
    ,EmpDeptName
    ,EmpAvgWorkingHours
FROM Employee.EmployeePivotTest  AS EP
INNER JOIN
(
   SELECT 
	EmpName
	,GROUP_CONCAT(DISTINCT EmpAvgWorkingHours 
         ORDER BY EmpAvgWorkingHours DESC) 
         AS GroupedWorkingHours
   FROM Employee.EmployeePivotTest 
   GROUP BY EmpName
) AS T
ON EP.EmpName = T.EmpName 
AND FIND_IN_SET(EmpAvgWorkingHours,GroupedWorkingHours) &lt;=2
ORDER BY EmpName,EmpDeptName;</pre><p>
<p><strong>Result:</strong></p>
<p><a href="https://www.dbrnd.com/wp-content/uploads/2015/08/MySQLTopN.png"><img decoding="async" class="alignnone wp-image-468 size-full" title="MySQL Top N Record for Each Group" src="https://www.dbrnd.com/wp-content/uploads/2015/08/MySQLTopN.png" alt="MySQL Top N Record for Each Group" width="308" height="117" srcset="https://www.dbrnd.com/wp-content/uploads/2015/08/MySQLTopN.png 308w, https://www.dbrnd.com/wp-content/uploads/2015/08/MySQLTopN-300x114.png 300w, https://www.dbrnd.com/wp-content/uploads/2015/08/MySQLTopN-150x57.png 150w" sizes="(max-width: 308px) 100vw, 308px" /></a></p>
<p>Now check the above result, each employee has two top 2 records based on their working hours.</p>
<blockquote><p>In the above query, I used GROUP_CONCAT and FIND_IN_SET.<br />
GROUP_CONCAT groups the column value into a single string and FIND_IN_SET is used to get the position of the string in a comma delimited string list.</p></blockquote>
<p>You can change the value of 2 to N for your customized report.</p>
]]></content:encoded>
					
		
		
			</item>
	</channel>
</rss>
