我有這段代碼,我很難理解它爲什麼不起作用。我已經搜索瞭解決方案,這實際上是我的代碼結束了它的方式,但它只是不正確的工作......任何幫助將不勝感激。SQL Server:在結果集中查找最大值
我想要做的是爲每個客戶端選擇一條記錄。所選的記錄應該是債務人餘額最高的記錄。相反,我將所有債務人的餘額全部拿到手。幾乎像它不是將它們分組返回給客戶端...
SELECT
Debtorness.ClientName,
Debtorness.DebtorName,
MAX(Debtorness.DFB)
FROM
(SELECT
Clients.Name AS ClientName,
Debtors.Name AS DebtorName,
(CalcAging.FundedBalance) AS DFB
FROM
Clients
INNER JOIN
CalcAging ON Clients.ClientKey = CalcAging.ClientKey
INNER JOIN
Debtors ON CalcAging.DebtorKey = Debtors.DebtorKey
INNER JOIN
CalcClient ON Clients.ClientKey = CalcClient.ClientKey
WHERE
Clients.Inactive='0'
GROUP BY
Clients.Name, Debtors.Name, CalcAging.FundedBalance,
CalcAging.MasterDebtorKey) AS Debtorness
GROUP BY
Debtorness.ClientName, Debtorness.DebtorName, Debtorness.DFB
ORDER BY
Debtorness.ClientName ASC
是我所得到的示例輸出:
Client1 | Debtor23 | Balance
Client1 | Debtor47 | Balance
Client1 | Debtor14 | Balance
Client2 | Debtor72 | Balance
Client2 | Debtor30 | Balance
等等......對於所有的客戶。再次,我想看到所有的客戶,但我只想要那個客戶的餘額最高的債務人......謝謝!
太謝謝你了!現在,我必須查看分區正在做什麼,並將其劃分爲......哈哈。儘管如此,這就像一個冠軍!乾杯! –
請記住接受這個答案是正確的,如果你幫助你的問題。 –