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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T05:24:53+00:00 2026-06-11T05:24:53+00:00

Very simplified, a table with some sample data: action_date account_id 1/1/2010 123 1/1/2010 123

  • 0

Very simplified, a table with some sample data:

action_date account_id
1/1/2010    123
1/1/2010    123
1/1/2010    456
1/2/2010    123
1/3/2010    789

For the data above, I need a query that will give the following:

action_date num_events  num_unique_accounts  num_unique_accounts_wtd
1/1/2010    3           2                    2
1/2/2010    1           1                    2
1/3/2010    1           1                    3

As you can see here, num_unique_accounts_wtd gives a kind of rolling end date for the unique period…

At first, one would think a query of the form

WITH
    events AS
    (
        SELECT
            action_date
            , COUNT(account_id) num_events
            , COUNT(DISTINCT account_id) num_unique_accounts
        FROM     actions
        GROUP BY action_date
    )
SELECT
    action_date
    , num_events
    , num_unique_accounts
    , SUM(num_unique_accounts) OVER (PARTITION BY NEXT_DAY(action_date, 'Monday') - 7 ORDER BY action_date ASC) num_unique_accounts_wtd
FROM events

would work but if you look closely it just adds the num_unique_accounts each day.. if the query were to run, for clarity, for 1/2/2010 it would give num_unique_accounts_wtd = 3 because of the 2 + 1.

Any ideas?

EDIT: Added one more row of data and output for clarity

  • 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-11T05:24:55+00:00Added an answer on June 11, 2026 at 5:24 am

    It seemed the answer may have been to be able to modify the analytical function to include something of the form

    COUNT(DISTINCT ...) OVER (PARTITION BY ... ORDER BY ... RANGE BETWEEN ... AND ...) 
    

    because RANGE BETWEEN allows expressions so the PARTITION BY window can be further subsetted in order to get what we’re looking for — unfortunately Oracle gives a

    ORA-30487 DISTINCT functions and RATIO_TO_REPORT cannot have an ORDER BY
    

    error so we can’t use this.

    Subsequent to googling the error I found others attempting the same thing (here and here) and within the links two answers were found — one of which I used for my real-world data.

    For reference, the answer for this question with the model in the original post would be something of the form:

    SELECT    action_date, COUNT(account_id) num_attempts, MAX(num_accounts) num_unique_accounts_wtd
    FROM
    (
        SELECT
            action_date
            , account_id
            , SUM(is_unique) OVER (PARTITION BY NEXT_DAY(action_date, 'Monday') - 7 ORDER BY action_date ASC, account_id ASC) num_accounts
        FROM
        (
            SELECT
                action_date
                , account_id
                , CASE
                    WHEN LAG(account_id) OVER (PARTITION BY NEXT_DATE(action_date, 'Monday') - 7, account_id ORDER BY action_date ASC) = account_id 
                    THEN 0
                    ELSE 1
                END is_unique
                FROM
                    actions
        )
    )
    GROUP BY  action_date
    

    So the data is

    1. iterated and determines if, for the week for each account number, it is unique or not
    2. then for each week, first order the set by action date then account_id and creating a running total
    3. group by action date and take the max week to date number
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I need some help with the following situation: Very simplified table users_log +-----------------+-------------------+ |
So I've noticed some very weird behaviour from my program. Here's a simplified code
Let us say I have a table (everything is very much simplified): create table
I have this existing schema where a schedule table looks like this (very simplified).
I need to do a very complex query. At one point, this query must
Given a very simplified table with the columns Name, Action, Status, Timepoint 1 A
Hello guys this is a very simplified version of my table: I will like
I've pasted a very simplified version of my SQL query below. The problem that
Very simplified, I have two tables Source and Target. declare @Source table (SourceID int
Here's my proposed (very simplified to illustrate the problem space) design for a C#

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.