Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8251419
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T00:13:51+00:00 2026-06-08T00:13:51+00:00

This is the below data in my Table1 BID PID TIME ———+——————-+———————- 1345653 330760137950

  • 0

This is the below data in my Table1

BID           PID                       TIME
---------+-------------------+----------------------
1345653       330760137950       2012-07-09 21:42:29
1345653       330760137950       2012-07-09 21:43:29
1345653       330760137950       2012-07-09 21:40:29
1345653       330760137950       2012-07-09 21:41:29
1345653       110909316904       2012-07-09 21:29:06
1345653       221065796761       2012-07-09 19:31:48

So If I need to clarify the above scenario- I have data in above table like this-
For USER 1345653 I have this PID 330760137950 four times but with different timestamps. So I need the output something like this-

Output that I need:-

1345653       330760137950       2012-07-09 21:43:29
1345653       330760137950       2012-07-09 21:42:29
1345653       330760137950       2012-07-09 21:41:29
1345653       110909316904       2012-07-09 21:29:06
1345653       221065796761       2012-07-09 19:31:48

So Basically If BID and PID are same but with different timestamps, then I need TOP 3 of those sorted with TIME in descending order

And for this I created rank UDF (User Defined Function) in Hive. And I wrote the below query but its not working for me. Can anyone help me on this?

SELECT bid, pid, rank(bid), time, UNIX_TIMESTAMP(time)
FROM (
    SELECT bid, pid, time
    FROM table1
    where to_date(from_unixtime(cast(UNIX_TIMESTAMP(time) as int))) = '2012-07-09'
    DISTRIBUTE BY bid,pid
    SORT BY bid, time desc
) a
WHERE rank(bid) < 3;

So with above query I am getting output like this

1345653       330760137950       2012-07-09 21:43:29
1345653       330760137950       2012-07-09 21:42:29
1345653       330760137950       2012-07-09 21:41:29

which is wrong as I am missing last two rows of the Expected Output above. Can anyone help me with this?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-08T00:13:53+00:00Added an answer on June 8, 2026 at 12:13 am
    select bid, pid, [time] from (
        select bid, pid, [time], rank() over (partition by bid, pid order by [time] desc) as k 
        from #temp ) as x 
    where k  <=3
    order by bid, pid, time desc 
    

    oh i’m in sql server. i don’t think you are……..

    anyway. my recommendation is that you move your rank function inside of the nested select you have. in the outside select you want it where it is less than three… i don’t know your syntax. i shouldn’t have answered this question. sorry…. lol

    here:
    http://ragrawal.wordpress.com/2011/11/18/extract-top-n-records-in-each-group-in-hadoophive/
    your rank() is in the outer select… it needs to be in the inner. leave the < 4 or <= 3 or whatever in the outer where statement, though. your query almost looks exactly like that example… just needs a few changes.

    based on the link and my absolute LACK of knowledge of Hive… i think you might want this:

    SELECT bid, pid, time
    FROM (
        SELECT bid, pid, rank(time) as rank, time
        FROM $compTable 
        DISTRIBUTE BY bid, pid
        SORT BY bid, pid, time desc
    ) a
    WHERE rank < 4
    ORDER BY bid, pid, time desc
    

    and i can’t test or compile because honestly i had no clue what hive was before you posted your question. (small world, i know, so sad – so true)

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

This is below data in Table2 ID2 | Count2 -----------+----------- 1345653 5 534140349 5
This is below data in Table2 ID2 | Count2 -----------+----------- 1345653 5 534140349 5
I have the data below saved as a pandas dataframe . With this data,
I am trying to binary serialize the data of vector. In this sample below
On the query below I keep getting this error: Cannot read the next data
I have written this query for retrieving data from mysql as below select FeedbackCode,EMailID,FeedbackDetail,
I have a data that looks like this . And my code below simply
Using the snippet below, I've attempted to extract the text data from this PDF
I have this array below and need to know the var to get the
I have a data file with 3 different sets of information like this below:

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.