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

  • Home
  • SEARCH
  • 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 6834345
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T23:06:01+00:00 2026-05-26T23:06:01+00:00

Customer Table (usage is kwH) +—-+———-+————+———-+———-+———-+——-+——-+ | ID | Customer | Account_no | Meter_no

  • 0
Customer Table (usage is kwH)
+----+----------+------------+----------+----------+----------+-------+-------+
| ID | Customer | Account_no | Meter_no | Supplier |  Active  | Usage | Repid | 
+----+----------+------------+----------+----------+----------+-------+-------+
|  1 | Joe      |        123 |      111 | NSTAR    | active   |  20   |  100  |
|  2 | Joe      |        123 |      222 | NSTAR    | active   |  30   |  100  |
|  3 | Joe      |        123 |      150 | NSTAR    | inactive |  60   |  100  |
|  4 | Sam      |        456 |      352 | SEP      | active   |  50   |  100  |
|  5 | Jill     |        789 |      222 | FES      | active   |  40   |  200  |
|  6 | Mike     |        883 |      150 | ABB      | inactive |  40   |  200  |
+----+----------+------------+----------+----------+----------+-------+-------+


Payment_Receive (table)
+------------+----------+-------------+-------------+
| Account_no | Supplier | Amount_paid | PaymentDate |
+------------+----------+-------------+-------------+
|        123 | NSTAR    | 20          | 2011-11-01  |
|        456 | SEP      | 40          | 2011-11-01  |
|        456 | SEP      | -40         | 2011-11-01  |
|        456 | SEP      | 40          | 2011-11-01  |
|        789 | FES      | 50          | 2011-11-01  |
|        883 | ABB      | 30          | 2011-11-01  |
+------------+----------+-------------+-------------+

The two tables are use for rep payout. We do not have control over the payout_table because it comes from outside. This creates certain problems because we can not do one-to-one match between the two tables. Leaving that aside, I would like to have payout calculated for RepID = 100 with certain criteria. This is the output I would like to see for RepId = 100

+------------+----------+-------------+-------------+-------------+
| Account_no | Supplier | Amount_paid |    Usage    | PaymentDate |
+------------+----------+-------------+-------------+-------------+
|        123 | NSTAR    | 20          |    60*      | 2011-11-01  |
|        456 | SEP      | 40          |    50       | 2011-11-01  |
|        456 | SEP      | -40         |    40       | 2011-11-01  |
|        456 | SEP      | 40          |    40       | 2011-11-01  |
+------------+----------+-------------+-------------+-------------+

Note here that

  • Account_no 123 exists twice in customers table, it must show one time in rep payout
  • 3 amounts were paid to account_no 456, all the three must show in the report

Reports are calculated on Monthly basis


Script for example (Updated with Usage column)

create database testcase
go

use testcase 
go

create table customers (
  id int not null primary key identity,
  customer_name varchar(25),
  account_no int,
  meter_no int,
  supplier varchar(20),
  active varchar(20),
  usage int,
  repid int
)

create table payments_received (
  account_no int,
  supplier varchar(20),
  amount_paid float,
  paymentdate smalldatetime
)

insert into customers values('Joe',123, 111,'NSTAR','active',20,100)
insert into customers values('Joe',123, 222,'NSTAR','active',30, 100)
insert into customers values('Joe',123, 150,'NSTAR','inactive',60,100)

insert into customers values('Sam',456, 352,'SEP','active',40,100)
insert into customers values('Jill',789, 222,'FES','active',40,200)
insert into customers values('Mike',883, 150,'ABB','inactive',40,200)

select * from customers

insert into payments_received values(123,'NSTAR',20,'2011-11-01')
insert into payments_received values(456,'SEP',40,'2011-11-01')
insert into payments_received values(456,'SEP',-40,'2011-11-01')
insert into payments_received values(456,'SEP',40,'2011-11-01')

insert into payments_received values(789,'FES',50,'2011-11-01')
insert into payments_received values(883,'ABB',30,'2011-11-01')

select * from payments_received

Updated: Updated The question and script

  • Usage been added to Customers table

  • Usage must appear int the result table

*60 = Notice that there are 2 active records (and one inactive). This could be the sum of the two, the one that is larger. This column is created problem removing duplicates

  • 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-26T23:06:02+00:00Added an answer on May 26, 2026 at 11:06 pm

    Two database brand independent options:

    option 1:

      Select 
       * 
      from 
          Payment_Receive PR
        inner join 
          (select distinct Account_no, Supplier 
           From Customer where Repid = 100 )  C
        on (PR.Account_no = C.Account_no 
           and PR.Supplier = C.Supplier )
    

    option 2:

      Select 
       * 
      from 
          Payment_Receive PR
      Where exists
          (select *
           From Customer C
           where 
              Repid = 100 and
              PR.Account_no = C.Account_no and
              PR.Supplier = C.Supplier )
    

    with date range:

    option 1:

      Select 
       * 
      from 
          Payment_Receive PR
        inner join 
          (select distinct Account_no, Supplier 
           From Customer where Repid = 100 )  C
        on (PR.Account_no = C.Account_no 
           and PR.Supplier = C.Supplier )
       where
         year(PR.PaymentDate) = 2011 and
         month(PR.PaymentDate) = 11
    

    option 2:

      Select 
       * 
      from 
          Payment_Receive PR
      Where exists
          (select *
           From Customer C
           where 
              Repid = 100 and
              PR.Account_no = C.Account_no and
              PR.Supplier = C.Supplier )
       and
         year(PR.PaymentDate) = 2011 and
         month(PR.PaymentDate) = 11
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

There is a customer table. I want to list active and inactive status in
I have a table that I use to keep track of customer usage. I
There is field name active in customer table. It validates as below in customer.rb:
Let's say I have a Customer table which has a PrimaryContactId field and a
I am trying to implement NOT NULL constraint in the customer table which is
I am using Spring and JDBCTemplate. The scenario is a CUSTOMER table and ORDERS
I have a performance problem on a query. First table is a Customer table
For example, in my WCF, if I have a Customer table and an Order
CREATE TABLE Customer ( customerID int identity (500,20) CONSTRAINT . . dateCreated datetime DEFAULT
Why does the MySQL query below give error 1066 (Not unique table/alias: 'customer') ?

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.