1

我似乎有一个作家阻止的时刻。

我需要一个查询,该查询将在特定日期按客户和帐户返回最大付款。

2 个表,其中一个用于客户和客户数据(客户 1 有 50 个客户),第二个表用于接收付款和日期。

所以我想要一个显示的表格:

Client | Account | Paid   | Date
----------------------------------------
   1   | 1234    | £2.99  | 01/11/2012
   2   | 1235    | £4.99  | 08/12/2012

希望这是有道理的!在我的脑海里它应该很简单!

谢谢

4

3 回答 3

0

这个问题很有趣。使用聚合查询解决了这个问题。

SQL 小提琴:http ://www.sqlfiddle.com/#!3/29256/1/0

select d.[Client], c.[Account],
max([Paid])as 'PAID', 
d.[Date] 
from Customer c, Data d where
d.[Client] = c.[Client] 
group by d.[Client],c.[Account],d.[Date];

产生结果:

CLIENT  ACCOUNT PAID    DATE
1   Stack   123 January, 01 2013 00:00:00+0000
1   Stack   234 January, 02 2013 00:00:00+0000
1   Stack   33  January, 03 2013 00:00:00+0000
2   Over    674 January, 04 2013 00:00:00+0000
2   Over    2424    January, 05 2013 00:00:00+0000
2   Over    4545    January, 06 2013 00:00:00+0000
3   Flow    22  January, 07 2013 00:00:00+0000
3   Flow    3456    January, 08 2013 00:00:00+0000
3   Flow    445 January, 09 2013 00:00:00+0000

如果我们遇到客户可能拥有多个帐户的情况,并且我们想找到在特定日期支付的最大金额的客户帐户,我们将不得不做一些更复杂的事情:

select *
from Customer c2, Data d2, 
(
select d.[Client],
max([Paid])as 'PAID', 
d.[Date] 
from Customer c, 
Data d 
where
d.[Client] = c.[Client] group by d.[Client],d.[Date]
) subQ
where c2.[client] = d2.[client] and
c2.[client] = subQ.[client] and
d2.[Paid] = subQ.[Paid] and
d2.[Date] = subQ.[Date];

由于您无法将其他列添加到不属于 group by 的查询中 - 请参阅:http ://technicalsmile.blogspot.gr/2011/04/column-is-invalid-in-select-list.html

于 2013-04-12T08:52:06.397 回答
0
select  
  Customer.name, 
  Account.name as account_name, 
  [date], 
  max(paid) 
from Data,
     Customer,
     Account
where Data.client = Customer.id
  and Data.account = Account.id
group by Customer.name, Account.name, [date]

http://www.sqlfiddle.com/#!3/93baf/2

于 2013-04-12T09:43:08.647 回答
0

SQL Server 支持Common Table ExpressionWindowing Function. 查询使用DENSE_RANK()根据最大Paid为每个给出排名值Account。所以第一个值是最大值,排名值为1。这是您要过滤的值。

假设链接两个表的列是Client

WITH record
AS
(
    SELECT  a.Client, a.Account, b.Paid, b.Date,
            DENSE_RANK() OVER (PARTITION BY a.Client ORDER BY b.Paid DESC) rn
    FROM    Customer a
            INNER JOIN Data b
                ON a.Client = b.Client
)
SELECT  Client, Account, Paid, Date
FROM    record
WHERE   rn = 1

考虑以下记录(来自SQLFiddle Demo

客户表

╔════════╦═════════╗
║ CLIENT ║ ACCOUNT ║
╠════════╬═════════╣
║      1 ║ Stack   ║
║      2 ║ Over    ║
║      3 ║ Flow    ║
╚════════╩═════════╝

数据表

╔════════╦══════╦════════════════════════════════╗
║ CLIENT ║ PAID ║              DATE              ║
╠════════╬══════╬════════════════════════════════╣
║      1 ║  123 ║ January, 01 2013 00:00:00+0000 ║
║      1 ║  234 ║ January, 02 2013 00:00:00+0000 ║
║      1 ║   33 ║ January, 03 2013 00:00:00+0000 ║
║      2 ║  674 ║ January, 04 2013 00:00:00+0000 ║
║      2 ║ 2424 ║ January, 05 2013 00:00:00+0000 ║
║      2 ║ 4545 ║ January, 06 2013 00:00:00+0000 ║
║      3 ║   22 ║ January, 07 2013 00:00:00+0000 ║
║      3 ║ 3456 ║ January, 08 2013 00:00:00+0000 ║
║      3 ║  445 ║ January, 09 2013 00:00:00+0000 ║
╚════════╩══════╩════════════════════════════════╝

连接表的结果

╔════════╦═════════╦══════╦════════════════════════════════╗
║ CLIENT ║ ACCOUNT ║ PAID ║              DATE              ║
╠════════╬═════════╬══════╬════════════════════════════════╣
║      1 ║ Stack   ║  123 ║ January, 01 2013 00:00:00+0000 ║
║      1 ║ Stack   ║  234 ║ January, 02 2013 00:00:00+0000 ║ << Expected Output
║      1 ║ Stack   ║   33 ║ January, 03 2013 00:00:00+0000 ║
║      2 ║ Over    ║  674 ║ January, 04 2013 00:00:00+0000 ║
║      2 ║ Over    ║ 2424 ║ January, 05 2013 00:00:00+0000 ║
║      2 ║ Over    ║ 4545 ║ January, 06 2013 00:00:00+0000 ║ << Expected Output
║      3 ║ Flow    ║   22 ║ January, 07 2013 00:00:00+0000 ║
║      3 ║ Flow    ║ 3456 ║ January, 08 2013 00:00:00+0000 ║ << Expected Output
║      3 ║ Flow    ║  445 ║ January, 09 2013 00:00:00+0000 ║
╚════════╩═════════╩══════╩════════════════════════════════╝

最终输出

╔════════╦═════════╦══════╦════════════════════════════════╗
║ CLIENT ║ ACCOUNT ║ PAID ║              DATE              ║
╠════════╬═════════╬══════╬════════════════════════════════╣
║      1 ║ Stack   ║  234 ║ January, 02 2013 00:00:00+0000 ║
║      2 ║ Over    ║ 4545 ║ January, 06 2013 00:00:00+0000 ║
║      3 ║ Flow    ║ 3456 ║ January, 08 2013 00:00:00+0000 ║
╚════════╩═════════╩══════╩════════════════════════════════╝
于 2013-04-12T08:49:01.513 回答