【问题标题】:Any idea of how I can get the sum of the most recent invoices per customer compared to a specific amount?知道如何将每个客户的最新发票总和与特定金额相比较吗?
【发布时间】:2018-11-04 20:39:13
【问题描述】:

我们在 SQL Server 中使用 3 个表,一个用于客户交易 (custtrn),一个用于客户详细信息 (customer),另一个 (cusfindata) 用于保存每个客户必须支付的剩余金额。

我需要找到最近的发票,但这些发票的总和不得超过他们必须支付的金额。

我可以给你的一些额外信息是:

custtrn.cusid = customer.id
and cusfindata.cusid = customer.id

更具体和更简单一点:如果我欠一家公司 1000 欧元,并且我确实有该公司的一些发票,我如何才能找到其总和等于或小于 1000 欧元的最新发票

数据样本可以是:

enter image description here

在此示例中,我需要从 Dimitris 发票编号 16、15、12 中检索总共 90 欧元,因为下一张(第 11 号)超过了 100 的金额。

【问题讨论】:

  • 这里的大多数人都希望样本数据等为格式化文本,而不是图像(或图像链接)。
  • (A) 编辑您的标题以总结您的具体技术问题。这里没有人关心您的客户和发票;我们关心一个特定的技术问题。 (B) 你的英文还行,不用道歉。但你的问题不清楚。为了清楚起见,它可以使用一些重写。我建议你再考虑一下。
  • 这个问题在姐妹网站 DBA Stack Exchange 上可能会更好。
  • 早上好,感谢您的建议,我会再次提出这个建议。我已经编辑好了倾斜

标签: sql sql-server sum invoice


【解决方案1】:

也许这个查询满足您的要求。

测试数据:

drop table if exists Customer;
drop table if exists Cusfindata;
drop table if exists Custtrn;

create table Customer (
    id int identity,
    code int,
    name varchar(30)
);

create table Cusfindata (
    id int identity,
    custid int,
    amount numeric(10,0)
);

create table Custtrn (
    id int identity,
    invoicenumber int,
    custid int,
    amount numeric(10,0),
    invoicedate date
);

insert into Customer(code,name)
    values ( 100 , 'Dimitris' ), ( 102 , 'George' ), ( 104 , 'John' );

insert into Cusfindata( custid, amount)
    values ( 100, 100 ) , ( 102 , 50 ) , ( 104 , 80 );

insert into Custtrn(invoicenumber,custid,amount,invoicedate)
    values (  1 , 100 ,  50 , '20180701' ) , (  2 , 100 ,  30 , '20180708' ) , 
           (  3 , 102 ,  20 , '20180715' ) , (  4 , 102 ,  56 , '20180722' ) ,
           (  5 , 104 ,  54 , '20180729' ) , (  6 , 100 ,  80 , '20180805' ) ,
           (  7 , 104 , 150 , '20180812' ) , (  8 , 102 ,  20 , '20180819' ) ,
           (  9 , 102 ,  23 , '20180826' ) , ( 10 , 102 ,  60 , '20180902' ) ,
           ( 11 , 100 ,  40 , '20180909' ) , ( 12 , 100 ,  20 , '20180916' ) ,
           ( 13 , 104 ,  20 , '20180923' ) , ( 14 , 104 ,  30 , '20180930' ) ,
           ( 15 , 100 ,  60 , '20181007' ) , ( 16 , 100 ,  10 , '20181014' ) ;

解决方案:

with my_summary as (
    select c.code,
           c.name,
           cf.amount as limit,
           ct.invoicedate, 
           ct.invoicenumber,
           ct.amount [invoice amount],
           SUM(ct.amount) over (partition by c.code order by ct.invoicedate desc,ct.invoicenumber desc) [total to pay]
    from Customer c 
        join Cusfindata cf on c.code = cf.custid
        join Custtrn ct on c.code = ct.custid
    -- exclude invoices with amount greater than limit 
    where ct.amount <= cf.amount 
) 
select s.* 
    from my_summary s
    where s.[total to pay] <= s.limit;

结果:

【讨论】:

  • 谢谢 Jacek,我会试一试,我会在今天晚些时候回来。今天是个好日子!
  • Jacek,我收到错误消息“未启用并行数据仓库 (PDW) 功能。”这是迄今为止看到的版本问题(我的版本是 Microsoft SQL Server 2008 (SP4) - 10.0.6000.29 (X64) Sep 3 2014 04:11:34 Copyright (c) 1988-2008 Microsoft Corporation Standard Edition (64- bit) o​​n Windows NT 6.0 (Build 6002: Service Pack 2) ) 我认为:“SUM(ct.amount) over (partition by c.code order by ct.invoicedate desc,ct.invoicenumber desc) [total支付]”与它有关...
猜你喜欢
  • 1970-01-01
  • 2023-01-18
  • 2022-06-13
  • 1970-01-01
  • 1970-01-01
  • 2013-10-24
  • 1970-01-01
  • 2015-07-19
  • 1970-01-01
相关资源
最近更新 更多