<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:taxo="http://purl.org/rss/1.0/modules/taxonomy/" version="2.0">
  <channel>
    <title>question Re: How to find nth highest salary in hive. in Archives of Support Questions (Read Only)</title>
    <link>https://community.cloudera.com/t5/Archives-of-Support-Questions/How-to-find-nth-highest-salary-in-hive/m-p/100419#M13322</link>
    <description>&lt;P&gt;Awesome! Marked as favorite post now. &lt;span class="lia-unicode-emoji" title=":slightly_smiling_face:"&gt;🙂&lt;/span&gt;&lt;/P&gt;</description>
    <pubDate>Tue, 22 Dec 2015 22:35:45 GMT</pubDate>
    <dc:creator>gbraccialli3</dc:creator>
    <dc:date>2015-12-22T22:35:45Z</dc:date>
    <item>
      <title>How to find nth highest salary in hive.</title>
      <link>https://community.cloudera.com/t5/Archives-of-Support-Questions/How-to-find-nth-highest-salary-in-hive/m-p/100417#M13320</link>
      <description>&lt;P&gt;Hi guys,&lt;/P&gt;&lt;P&gt;I know hive is Data Warehousing tool which is not suited as OLTP. In regular RDBMS Structure i have way like "Correlated queries" helps to find nth highest salary from a table as following.&lt;/P&gt;&lt;P&gt;Is there any way in hive also.The query is related to true/false conditions.&lt;/P&gt;&lt;PRE&gt;SELECT * /*This is the outer query part */
FROM Employee Emp1
WHERE (N-1) = ( /* Subquery starts here */
SELECT COUNT(DISTINCT(Emp2.Salary))
FROM Employee Emp2
WHERE Emp2.Salary &amp;gt; Emp1.Salary)

&lt;/PRE&gt;</description>
      <pubDate>Tue, 22 Dec 2015 19:45:32 GMT</pubDate>
      <guid>https://community.cloudera.com/t5/Archives-of-Support-Questions/How-to-find-nth-highest-salary-in-hive/m-p/100417#M13320</guid>
      <dc:creator>bsuresh</dc:creator>
      <dc:date>2015-12-22T19:45:32Z</dc:date>
    </item>
    <item>
      <title>Re: How to find nth highest salary in hive.</title>
      <link>https://community.cloudera.com/t5/Archives-of-Support-Questions/How-to-find-nth-highest-salary-in-hive/m-p/100418#M13321</link>
      <description>&lt;P&gt;
	. &lt;A rel="user" href="https://community.cloudera.com/users/1146/bsuresh.html" nodeid="1146"&gt;@Suresh Bonam&lt;/A&gt; yes you can calculate the nth element, see the following example:&lt;/P&gt;&lt;P&gt;
	&lt;STRONG&gt;Create sample table (emp=employees)&lt;/STRONG&gt;&lt;/P&gt;
&lt;PRE&gt;create table emp(id Int, salary Double)
row format delimited 
  fields terminated by ',' 
  stored as textfile
&lt;/PRE&gt;&lt;P&gt;
	I have added the following &lt;STRONG&gt;data&lt;/STRONG&gt; to that table:&lt;/P&gt;
&lt;PRE&gt;1,423
2,23
3,67878
4,84
5,9999999
6,1
7,578
8,8877
9,4445
10,756
11,84
&lt;/PRE&gt;&lt;P&gt;
	&lt;STRONG&gt;Understanding the data&lt;/STRONG&gt;&lt;/P&gt;&lt;P&gt;
	Lets look at the sorted salaries first:&lt;/P&gt;
&lt;PRE&gt;select * from emp order by salary;
&lt;/PRE&gt;&lt;P&gt;
	Result:&lt;/P&gt;
&lt;PRE&gt;emp.id	emp.salary
6	1
2	23
4	84
11	84
1	423
7	578
10	756
9	4445
8	8877
3	67878
5	9999999
&lt;/PRE&gt;&lt;P&gt;
	&lt;EM&gt;Note: We have two identical salaries in the table (=84)&lt;/EM&gt;&lt;/P&gt;&lt;P&gt;
	&lt;STRONG&gt;Create query to select unique salaries&lt;/STRONG&gt;&lt;/P&gt;&lt;P&gt;
	In order to select the nth-element we need to remove the duplicate values (e.g. distinct) and create some kind of row counter or index. I decided to use Group-By to remove the duplicate salaries and row_number-UDF to generate the index.&lt;/P&gt;
&lt;PRE&gt;select salary, ROW_NUMBER() over (ORDER BY salary) as row_no from emp group by salary
&lt;/PRE&gt;&lt;P&gt;
	Result:&lt;/P&gt;
&lt;PRE&gt;salary	row_no
1	1
23	2
84	3
423	4
578	5
756	6
4445	7
8877	8
67878	9
9999999	10
&lt;/PRE&gt;&lt;P&gt;
	&lt;STRONG&gt;Putting everything together&lt;/STRONG&gt;&lt;/P&gt;
&lt;PRE&gt;select * from (
  select salary, ROW_NUMBER() over (ORDER BY salary) as row_no from emp group by salary) res 
where res.row_no = 4
&lt;/PRE&gt;&lt;P&gt;
	Result:&lt;/P&gt;
&lt;PRE&gt;res.salary    res.row_no
423	      4
&lt;/PRE&gt;&lt;P&gt;&lt;EM&gt;
	Note: We received the correct result and not 84 since we've removed the duplicate salaries in the subquery &lt;span class="lia-unicode-emoji" title=":slightly_smiling_face:"&gt;🙂&lt;/span&gt;  &lt;/EM&gt;&lt;/P&gt;</description>
      <pubDate>Tue, 22 Dec 2015 20:55:17 GMT</pubDate>
      <guid>https://community.cloudera.com/t5/Archives-of-Support-Questions/How-to-find-nth-highest-salary-in-hive/m-p/100418#M13321</guid>
      <dc:creator>jstraub</dc:creator>
      <dc:date>2015-12-22T20:55:17Z</dc:date>
    </item>
    <item>
      <title>Re: How to find nth highest salary in hive.</title>
      <link>https://community.cloudera.com/t5/Archives-of-Support-Questions/How-to-find-nth-highest-salary-in-hive/m-p/100419#M13322</link>
      <description>&lt;P&gt;Awesome! Marked as favorite post now. &lt;span class="lia-unicode-emoji" title=":slightly_smiling_face:"&gt;🙂&lt;/span&gt;&lt;/P&gt;</description>
      <pubDate>Tue, 22 Dec 2015 22:35:45 GMT</pubDate>
      <guid>https://community.cloudera.com/t5/Archives-of-Support-Questions/How-to-find-nth-highest-salary-in-hive/m-p/100419#M13322</guid>
      <dc:creator>gbraccialli3</dc:creator>
      <dc:date>2015-12-22T22:35:45Z</dc:date>
    </item>
    <item>
      <title>Re: How to find nth highest salary in hive.</title>
      <link>https://community.cloudera.com/t5/Archives-of-Support-Questions/How-to-find-nth-highest-salary-in-hive/m-p/100420#M13323</link>
      <description>&lt;P&gt;Thanks &lt;span class="lia-unicode-emoji" title=":slightly_smiling_face:"&gt;🙂&lt;/span&gt; &lt;/P&gt;</description>
      <pubDate>Wed, 23 Dec 2015 01:15:18 GMT</pubDate>
      <guid>https://community.cloudera.com/t5/Archives-of-Support-Questions/How-to-find-nth-highest-salary-in-hive/m-p/100420#M13323</guid>
      <dc:creator>jstraub</dc:creator>
      <dc:date>2015-12-23T01:15:18Z</dc:date>
    </item>
    <item>
      <title>Re: How to find nth highest salary in hive.</title>
      <link>https://community.cloudera.com/t5/Archives-of-Support-Questions/How-to-find-nth-highest-salary-in-hive/m-p/100421#M13324</link>
      <description>&lt;P&gt;hi i tried this but got the below error. pls do help me out. I am quite new to hive.&lt;/P&gt;&lt;P&gt;hive&amp;gt; select salary, ROW_NUMBER() over (ORDER BY salary) as row_no from emp group by salary;
FAILED: ParseException line 1:33 mismatched input '(' expecting FROM near 'over' in from clause&lt;/P&gt;</description>
      <pubDate>Tue, 29 Mar 2016 19:52:32 GMT</pubDate>
      <guid>https://community.cloudera.com/t5/Archives-of-Support-Questions/How-to-find-nth-highest-salary-in-hive/m-p/100421#M13324</guid>
      <dc:creator>ssatya618</dc:creator>
      <dc:date>2016-03-29T19:52:32Z</dc:date>
    </item>
    <item>
      <title>Re: How to find nth highest salary in hive.</title>
      <link>https://community.cloudera.com/t5/Archives-of-Support-Questions/How-to-find-nth-highest-salary-in-hive/m-p/100422#M13325</link>
      <description>&lt;P&gt;Use DENSE_RANK instead of ROW_NUMBER() with GROUP BY , as it will take only 1 map-reduce job to achieve it as compared to 2 map-reduce jobs by latter one.&lt;/P&gt;&lt;P&gt;&lt;BR /&gt;&lt;/P&gt;&lt;P&gt;select * from (&lt;/P&gt;&lt;P&gt;  select salary, DENSE_RANK() over (ORDER BY salary DESC) as row_no from emp) res &lt;/P&gt;&lt;P&gt;where res.row_no = 2;&lt;/P&gt;</description>
      <pubDate>Wed, 15 May 2019 02:57:57 GMT</pubDate>
      <guid>https://community.cloudera.com/t5/Archives-of-Support-Questions/How-to-find-nth-highest-salary-in-hive/m-p/100422#M13325</guid>
      <dc:creator>nikhilaggarwal</dc:creator>
      <dc:date>2019-05-15T02:57:57Z</dc:date>
    </item>
    <item>
      <title>Re: How to find nth highest salary in hive.</title>
      <link>https://community.cloudera.com/t5/Archives-of-Support-Questions/How-to-find-nth-highest-salary-in-hive/m-p/289744#M13326</link>
      <description>&lt;P&gt;Ho to find 2nd(nth) highest salary in each department? Please find below sample dataset.&lt;/P&gt;&lt;P&gt;1,423,depA&lt;BR /&gt;2,23,depA&lt;BR /&gt;3,67878,depB&lt;BR /&gt;11,84,depB&lt;BR /&gt;5,9999999,depA&lt;BR /&gt;9,4445,depA&lt;BR /&gt;8,8877,depB&lt;BR /&gt;10,756,depC&lt;BR /&gt;4,84,depC&lt;BR /&gt;7,578,depA&lt;BR /&gt;6,1,depC&lt;/P&gt;&lt;P&gt;output-&lt;/P&gt;&lt;P&gt;10,756,depC&lt;BR /&gt;4,84,depC&lt;BR /&gt;6,1,depC&lt;/P&gt;</description>
      <pubDate>Thu, 13 Feb 2020 19:33:01 GMT</pubDate>
      <guid>https://community.cloudera.com/t5/Archives-of-Support-Questions/How-to-find-nth-highest-salary-in-hive/m-p/289744#M13326</guid>
      <dc:creator>LetsHadoop</dc:creator>
      <dc:date>2020-02-13T19:33:01Z</dc:date>
    </item>
    <item>
      <title>Re: How to find nth highest salary in hive.</title>
      <link>https://community.cloudera.com/t5/Archives-of-Support-Questions/How-to-find-nth-highest-salary-in-hive/m-p/289763#M13327</link>
      <description>&lt;P&gt;Hi&amp;nbsp;&lt;a href="https://community.cloudera.com/t5/user/viewprofilepage/user-id/74206"&gt;@LetsHadoop&lt;/a&gt;,&amp;nbsp;&lt;/P&gt;
&lt;P&gt;As this thread is older and was marked 'Solved' in Dec 2015 you would have a better chance of receiving a helpful answer by &lt;A href="https://community.cloudera.com/t5/forums/postpage/board-id/Questions" target="_blank" rel="noopener"&gt;starting a new thread&lt;/A&gt;. This will also provide you with an opportunity to provide details about your table design that could aid others in providing an answer more directly tailored to your question.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Fri, 14 Feb 2020 00:35:20 GMT</pubDate>
      <guid>https://community.cloudera.com/t5/Archives-of-Support-Questions/How-to-find-nth-highest-salary-in-hive/m-p/289763#M13327</guid>
      <dc:creator>ask_bill_brooks</dc:creator>
      <dc:date>2020-02-14T00:35:20Z</dc:date>
    </item>
    <item>
      <title>Re: How to find nth highest salary in Mysql.</title>
      <link>https://community.cloudera.com/t5/Archives-of-Support-Questions/How-to-find-nth-highest-salary-in-hive/m-p/298472#M13328</link>
      <description>&lt;P&gt;Please watch the below video:-&lt;/P&gt;&lt;P&gt;&lt;A href="https://www.youtube.com/watch?v=fBeN8VQm0_Q" target="_blank"&gt;https://www.youtube.com/watch?v=fBeN8VQm0_Q&lt;/A&gt;&lt;/P&gt;&lt;P&gt;Please like comment share and subscribe my channel, If you loved it.&lt;/P&gt;</description>
      <pubDate>Tue, 23 Jun 2020 13:24:43 GMT</pubDate>
      <guid>https://community.cloudera.com/t5/Archives-of-Support-Questions/How-to-find-nth-highest-salary-in-hive/m-p/298472#M13328</guid>
      <dc:creator>loekshagrawal</dc:creator>
      <dc:date>2020-06-23T13:24:43Z</dc:date>
    </item>
  </channel>
</rss>

