2011-11-20 59 views
2

我重新發布了我的原始問題,並修改了該問題,並回答了問題並選擇了最佳答案。修復付款問題

付款來自我們的供應商,其中的賬戶和代表根據哪個賬戶得到了多少錢。

Customers 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 | 
+------------+----------+-------------+-------------+ 

兩個表用於代表支付。每個帳戶都會收到付款,它們會根據Account_No和Supplier與我們的客戶進行匹配。我們無法控制payment_table,因爲它來自外部。這會產生一些問題,因爲我們無法在兩個表之間進行一對一的匹配。除此之外,我希望按照特定標準計算RepID = 100的支出。這是輸出,我想看到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 | 
+------------+----------+-------------+-------------+-------------+ 

這裏需要注意的是

  • account_no上123在客戶表三次存在,它必須顯示出代表支付一次
  • 3金額支付給account_no 456,所有三個必須顯示在報告中
  • * 60 =請注意,有兩個活動記錄(和一個非活動記錄)。這可能是兩個活動的總和。但是任何其他值都是可以接受的,如果這使得查詢變得容易(對於兩個或一個而不是其他值)
  • 請注意,用法列必須出現在輸出表中。這是爲我創建問題的列。如果我不包括這一切工作正常。
  • 帶有用法列的點,如果我對具有相同Account_No和Supplier但具有不同用法的同一客戶有兩個記錄,那麼在包含用量列時會使兩個記錄不同。因此,distinct不起作用來刪除此重複。

報告計算每月的基礎上對這個問題

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 

回答

2


腳本如何:

CREATE VIEW v_customers_by_rep 
AS 
    SELECT SUM(USAGE) AS USAGE , 
     REPID , 
     CAST(account_no AS VARCHAR) + '_' + Supplier AS UniqueId 
    FROM customers 
GROUP BY CAST(account_no AS VARCHAR) + '_' + Supplier , 
     REPID 
GO 
DECLARE 
    @repid INT 

SET @repid = 100 

SELECT pr.* , 
     u.Usage 
    FROM payments_received pr INNER JOIN v_customers_by_rep u 
    ON CAST(pr.account_no AS VARCHAR) + '_' + pr.Supplier = u.UniqueId 
WHERE u.repid = @repid 

您合作如果需要,還可以消除視圖中的不活動記錄。

+0

這是一個很棒的查詢。將實施我的實際數據的邏輯,並選擇接受的答案。 –