如何获得具有相同外键的两列的总和

时间:2015-06-21 21:18:22

标签: sql sql-server-2005

我有三个表:Carrier(C),DropShipper(D)和ShoppingCart(S),具有以下架构

表C

c.id(Pk,int,not null)
c.dropshipperid(Fk,int,not null)
c.Prodid(int, not null
c.cost(money null)

表D

D.Dropshipperid(Pk,int,not null)
D.Dropshipper(nvarchar(50))
D.Remarks(nvarchar(50))

表S

s.cartid(PK, char(36))
s.prodid(pk,fk,int not null)
s.qty(int not null)

以下是示例数据:

c.id    c.dropshipperid  c.prodid    c.cost
--------------------------------------------
1            1             11          100
2            2             11          200
3           3             11          80
4            4             11           70
5            1             6          212
6            2             6          312
7           3             6          412
8           4             6          512


D.dropshipperid   D.dropshipper        D.Remarks
-------------------------------------------------
1                   Airmail               10-25days
2                    DHL                  23-5 days
3                     Fedex                6- 10days
4                       UPS                 4- 5days

S.cartid           s.prodid              s.qty
------------------------------------------------
   xxxx                 11                  2
    xxxx                 6                   2

这是我的sql

SELECT     D.DropShipper, C.Cost, D.Remarks,( S.Quantity * C.Cost)    AS  SubCost, S.CartID, C.DropShipperID, 
S.ProductID, C.ProductID AS cProductid,  S.Quantity
FROM C INNER JOIN D 
ON C.DropShipperID = D.DropShipperID 
INNER JOIN S 
 ON S.ProductID = S.ProductID
 WHERE (C.DropShipperID IN (1, 2, 3, 4, 5)) AND
(S.CartID = @cartid)

这是我qry的示例结果:

Dropshipper     cost     Remarkd     Subcost     Cartid    Dropshipperid
------------------------------------------------------------------------
   Airmail       100      Text         200        xxxx            1
   DHL           200                   400         xxxx           2   
   Fedex          80                   160         xxxx            3
   UPS            70                   140        xxxx            4
  Airmail         212                 424            xxxx          1
   DHL           312                  624          xxxx            2
   Fedex         412                  824         xxxx            3
   UPS           512                  1024        xxxx             4

这就是我需要的:

我不知道DropShipperID重复的是什么,即我只需要一套。那么SubCost应该是每个dropshipperID的子分类的总和。像这样的事情

DropShipperID   DropShipper    SubCost    etc
------------------------------------------------
1                Airmail          624
2                DHL               1014
3                Fedex              974
4                UPS                 1164  

1 个答案:

答案 0 :(得分:0)

试试这个,

 SELECT C.DropShipperID, D.DropShipper, 
        SUM(S.Quantity) AS Quantity, 
        SUM(C.Cost) AS Cost, 
        SUM(S.Quantity * C.Cost) AS SubCost
 FROM C INNER JOIN D ON C.DropShipperID = D.DropShipperID 
        INNER JOIN S ON S.ProductID = S.ProductID
 WHERE (C.DropShipperID IN (1, 2, 3, 4, 5))
       AND (S.CartID = @cartid)
 GROUP BY C.DropShipperID, D.DropShipper

select列表中的所有列都应显示在goup by子句中,但与sum()max()min()等聚合函数一起使用的列除外等

在您的情况下,数量,成本等列应与SUM()列表中的select函数一起使用,不应在group by子句中使用。