用于从多列

时间:2015-06-17 00:38:15

标签: sql-server vb6

我在SQL Server 2008 R2数据库中有以下表格:

Customers
=========
CustID  CustName
======  ========
0       NULL
1       A
2       B
3       C

InterestingCustomers
====================
CustID
======
0
1
3

Orders
======
CustID  OrderID  InvoiceTotal  Discount
======  =======  ============  ========
0       NULL     2000          NULL
0       100      NULL          500      
1       1        100           NULL
1       2        90            15
2       3        300           25
2       4        50            0
3       5        100           10
3       6        200           25
3       7        150           NULL
3       8        120           20

我想要一个查询,显示所有订单中每个CustID和CustName的InvoiceTotal和Discount列的最大值,但前提是客户出现在InterestingCutomers中并且ID> 0:

CustID  CustName  MaxInvoiceTotal  MaxDiscount
1       A         100              15
3       C         200              25

我遇到的一个问题是我的应用程序是使用Jet 4.0和ADO的旧版VB6。这缺乏对托管我的数据库的SQL Server 2008的许多高级功能的支持。我可用的SQL函数列在这里:https://support.microsoft.com/en-us/kb/294698

到目前为止,我已经能够获得最大InvoiceTotal,但我无法扩展相同的查询以返回任何其他列的最大值。

以下正确返回每个有效CustID的最大InvoiceTotal:

SELECT Customers.CustID, CustName, InvoiceTotal, Discount 
FROM ((Customers INNER JOIN Interesting ON Customers.CustID = Interesting.CustID) INNER JOIN Orders ON Customers.CustID = Orders.CustID) 
WHERE ((CustID > 0) 
AND (InvoiceTotal IN 
(SELECT MAX(InvoiceTotal) AS MaxInvoiceTotal 
FROM Orders 
WHERE Orders.CustID = Customers.CustID)))

返回:

CustID  CustName  MaxInvoiceTotal
1       A         100            
3       C         200            

但是,扩展WHERE子句以将Discount限制为每个客户的最大值仅返回InvoiceTotal和Discount碰巧最大且属于相同顺序的记录:

WHERE ((CustID > 0) AND (InvoiceTotal IN 
(SELECT MAX(InvoiceTotal) AS MaxInvoiceTotal 
FROM Orders 
WHERE Orders.CustID = Customers.CustID)) AND 
Discount IN 
(SELECT MAX(Discount) AS MaxDiscount 
FROM Orders 
WHERE Orders.CustID = Customers.CustID))

返回:

CustID  CustName  MaxInvoiceTotal  MaxDiscount
3       C         200              25

未选择CustID 1,因为其最大InvoiceTotal值属于与其最大Discount值不同的顺序。我假设两个条件都针对每个有资格获得这个目标的订单进行测试,但我想要一些方法来删除OrderID与InvoiceTotal和Discount之间的关联。或者附加条款不起作用。

我怀疑我可能在UNION领域,但考虑到我所概述的限制,我想检查是否有更清洁或更有效的方法。

2 个答案:

答案 0 :(得分:1)

SELECT Customers.CustID, CustName, MaxInvoiceTotal, MaxDiscount 
FROM Customers 
INNER JOIN Interesting ON Customers.CustID = Interesting.CustID
INNER JOIN (SELECT CustID, MAX(Discount) AS MaxDiscount 
            FROM Orders
            Group by CustID) MaxDiscountOrders ON Customers.CustID =     
            MaxDiscountOrders.CustID
INNER JOIN (SELECT CustID, MAX(InvoiceTotal) AS MaxInvoiceTotal 
            FROM Orders
            Group by CustID) MaxInvoiceTotalOrders ON Customers.CustID     
            = MaxInvoiceTotalOrders.CustID
WHERE CustID > 0

试试这个..虽然我没有检查语法错误。

答案 1 :(得分:0)

SELECT CustID,
       CustName = MAX(CustName),
       InvoiceTotal = SUM(InvoiceTotal),
       Discount = SUM(Discount)
FROM Orders O
INNER JOIN Customers C ON O.CustID = C.CustID
WHERE O.CustID IN (SELECT CustID 
                   FROM InterestingCustomers)
AND O.CustID>0
GROUP BY CustID