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 6651203
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T00:58:59+00:00 2026-05-26T00:58:59+00:00

I have two tables in Hive, t1 and t2 >describe t1; >date_id string >describe

  • 0

I have two tables in Hive, t1 and t2

>describe t1;
>date_id    string

>describe t2;
>messageid string,
 createddate string,
 userid int

> select * from t1 limit 3;        
> 2011-01-01 00:00:00 
  2011-01-02 00:00:00 
  2011-01-03 00:00:00 

> select * from t2 limit 3;
87211389    2011-01-03 23:57:01 13864753
87211656    2011-01-03 23:57:59 13864769
87211746    2011-01-03 23:58:25 13864785

What I want is to count previous three-day distinct userid for a given date.
For example, for date 2011-01-03, I want to count distinct userid from 2011-01-01 to 2011-01-03.
for date 2011-01-04, I want to count distinct userid from 2011-01-02 to 2011-01-04

I wrote the following query. But it does not return three-day result. It returns distinct userid per day instead.

SELECT to_date(t1.date_id), count(distinct t2.userid) FROM t1 JOIN t2 
ON (to_date(t2.createddate) = to_date(t1.date_id))  
WHERE date_sub(to_date(t2.createddate),0) > date_sub(to_date(t1.date_id), 3) 
AND to_date(t2.createddate) <= to_date(t1.date_id) 
GROUP by to_date(t1.date_id);

`to_date()` and `date_sub()` are date function in Hive. 

That said, the following part does not take effect.

WHERE date_sub(to_date(t2.createddate),0) > date_sub(to_date(t1.date_id), 3) 
AND to_date(t2.createddate) <= to_date(t1.date_id) 

EDIT: One solution can be (but it is super slow):

SELECT to_date(t3.date_id), count(distinct t3.userid) FROM
(
 SELECT * FROM t1  LEFT OUTER JOIN t2
 WHERE 
 (date_sub(to_date(t2.createddate),0) > date_sub(to_date(t1.date_id), 3)
  AND to_date(t2.createddate) <= to_date(t1.date_id)
 )
) t3 
GROUP by to_date(t3.date_id);

UPDATE: Thanks for all answers. They are good.
But Hive is a bit different from SQL. Unfortunately, they cannot use in HIVE.
My current solution is to use UNION ALL.

 SELECT * FROM t1 JOIN t2 ON (to_date(t1.date_id) = to_date(t2.createddate))
 UNION ALL
 SELECT * FROM t1 JOIN t2 ON (to_date(t1.date_id) = date_add(to_date(t2.createddate), 1)
 UNION ALL 
 SELECT * FROM t1 JOIN t2 ON (to_date(t1.date_id) = date_add(to_date(t2.createddate), 2)

Then, I do group by and count. In this way, I can get what I want.
Although it is not elegant, it is much efficient than cross join.

  • 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-05-26T00:59:00+00:00Added an answer on May 26, 2026 at 12:59 am

    The following should seem to work in standard SQL…

    SELECT
      to_date(t1.date_id),
      count(distinct t2.userid)
    FROM
      t1
    LEFT JOIN
      t2
        ON  to_date(t2.createddate) >= date_sub(to_date(t1.date_id), 2)
        AND to_date(t2.createddate) <  date_add(to_date(t1.date_id), 1)
    GROUP BY
      to_date(t1.date_id)
    

    It will, however, be slow. Because you are storing dates as strings, the using to_date() to convert them to dates. What this means is that indexes can’t be used, and the SQL engine can’t do Anything clever to reduce the effort being expended.

    As a result, every possible combination of rows needs to be compared. If you have 100 entries in T1 and 10,000 entries in T2, your SQL engine is processing a million combinations.

    If you store these values as dates, you don’t need to_date(). And if you index the dates, the SQL engine can quickly home in on the range of dates being specified.

    NOTE: The format of the ON clause means that you do not need to round t2.createddate down to a daily value.

    EDIT Why your code didn’t work…

    SELECT to_date(t1.date_id), count(distinct t2.userid) FROM t1 JOIN t2 
    ON (to_date(t2.createddate) = to_date(t1.date_id))  
    WHERE date_sub(to_date(t2.createddate),0) > date_sub(to_date(t1.date_id), 3) 
    AND to_date(t2.createddate) <= to_date(t1.date_id) 
    GROUP by to_date(t1.date_id);
    

    This joins t1 to t2 with an ON clause of (to_date(t2.createddate) = to_date(t1.date_id)). As the join is a LEFT OUTER JOIN, the values in t2.createddate MUST now either be NULL (no matches) or be the same as t1.date_id.

    The WHERE clause allows a much wider range (3 days). But the ON clause of the JOIN has already restricted you data down to a single day.

    The example I gave above simply takes your WHERE clause and put’s it in place of the old ON clause.

    EDIT

    Hive doesn’t allow <= and >= in the ON clause? Are you really fixed in to using HIVE???

    If you really are, what about BETWEEN?

    SELECT
      to_date(t1.date_id),
      count(distinct t2.userid)
    FROM
      t1
    LEFT JOIN
      t2
        ON to_date(t2.createddate) BETWEEN date_sub(to_date(t1.date_id), 2) AND date_add(to_date(t1.date_id), 1)
    GROUP BY
      to_date(t1.date_id)
    

    Alternatively, refactor your table of dates to enumerate the dates you want to include…

    TABLE t1 (calendar_date, inclusive_date) =
    { 2011-01-03, 2011-01-01
      2011-01-03, 2011-01-02
      2011-01-03, 2011-01-03
    
      2011-01-04, 2011-01-02
      2011-01-04, 2011-01-03
      2011-01-04, 2011-01-04
    
      2011-01-05, 2011-01-03
      2011-01-05, 2011-01-04
      2011-01-05, 2011-01-05 }
    
    SELECT
      to_date(t1.calendar_date),
      count(distinct t2.userid)
    FROM
      t1
    LEFT JOIN
      t2
        ON to_date(t2.createddate) = to_date(t1.inclusive_date)
    GROUP BY
      to_date(t1.calendar_date)
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have two tables that I can perfectly query seperately. table1 stores ranges: SELECT
I have two tables users registered_members I want to confirm values from user table
I have two tables, like this: master --------- empcode INT PRIMARY KEY name VARCHAR
I have two tables from target website in database and have to get post
I have two tables: USER: userID | uName | ----------------- 1 | John 2
Have two tables: CREATE TABLE IF NOT EXISTS `issue_details` ( `id` INT NOT NULL
I have two tables, users and visits. I want to select all users who
I have two tables: CREATE TABLE tblEatables ( `EatId` int UNSIGNED PRIMARY AUTO_INCREMENT, `Fruits`
Have two tables with a linking table between them. USERS +-------+---------+ | userID| Username|
I have two tables: Customers and Commands, it's a @OneToMany relation from Client to

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.