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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T03:39:09+00:00 2026-06-11T03:39:09+00:00

Following is the problem statement: I have a database table with entries that look

  • 0

Following is the problem statement:

I have a database table with entries that look like (this is fictional table 🙂 ):

Company    Customer ID         Date          

Amazon            1         2012-08-30      
Amazon            2         2012-09-30
Amazon            2         2012-09-1
amazon            2         2012-12-10
eBay              1         2012-03-10
eBay              10        2012-01-01
BananaR           1         2012-02-20  
BananaR           1         2012-02-28
BananaR           1         2012-05-10
BananaR           1         2012-05-25

Now each entry in this table denotes the security check done for online transaction. So the 1st row would mean that “For Customer ID 1, Amazon did a online security check on 2012-08-30 for a transaction”.

So, I need to figure how many additional security checks where done for the same Customer and company combination in every month after the 1st security check. i.e.

For customer 2, Amazon did its 1st check on 2012-09-01, then the additional ones would be ones on 2012-09-30 and 2012-12-10. So expected answer is:

Company      CustomerID     Month            Additional checks
Amazon          1            2012-08               0
Amazon          2            2012-09               1
Amazon          2            2012-12               1
eBay            1            2012-03               0
eBay            10           2012-01               0
BananaR         1            2012-02               1
BananaR         1            2012-05               2

My existing query outputs for additional checks since 1st check done in every month, but I do not know if I can back track to previous months. So my query looks like:

select company as 'Company',
       customer_id as 'Customer',
       format_date as 'Month',
       (add_count-1) as 'Additioanl Checks'
  from (select count(*) as add_count,
               company,
               customer_id,
               to_char(date, 'yyyy-mm') as format_date
          from my_table
          group by company, customer_id, format_date)

The result is get now is:

Company      Customer         Month       Additional Checks
Amazon         1               2012-08         0
Amazon         2               2012-09         1
**Amazon       2               2012-12         0**
eBay           1               2012-03         0
eBay           10              2012-01         0
BananaR        1               2012-02         1
**BananaR      1               2012-05         1**

The rows in ** are incorrect since they are counting for additional checks per month basis and not since the 1st check was done in previous months.

  • 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-11T03:39:11+00:00Added an answer on June 11, 2026 at 3:39 am

    If I correctly understood your logic, you want to subtract 1 from the count for the first month, but not for subsequent months. Here is one way:

    select Company,
           Customer,
           to_char(date, 'yyyy-mm') as month,
           count(*) - (case
                         when to_char(min(MinDate), 'yyyy-mm') = to_char(date, 'yyyy-mm')
                         then 1
                         else 0
                       end)
      from (select t.*,
                   min(date) over (partition by company, customer) as MinDate
              from t) t
      group by Company, Customer, to_char(date, 'yyyy-mm')
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have 2 database tables that looks like this Subscriptions table member_id active 5
I have a problem creating the following SQL Statement using LINQ & C# select
i have got a problem with my SQL Statement: Table structure: CREATE TABLE tags
I was pointed out by someone else that the following database design have serious
I have the following problem while i scroll the table view: NSCFString objectAtIndex:]: unrecognized
I have a quite serious performance issue with the following statement that i can't
I currently face the following problem: I have written a Java program that parses
I have written a working T-SQL MERGE statement. The premise is that Database A
thanks allready for helping me out with this. The following problem occurs: I have
I have the following problem. I have embedded a map using Google Maps' API.

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.