如何获得具有相同外键的两列的总和
How do I get Sum of two Columns with Same Foreign Key
我有三个表:Carrier(C)、DropShipper(D) 和 ShoppingCart(S),具有以下架构
Table C
c.id(Pk,int,not null)
c.dropshipperid(Fk,int,not null)
c.Prodid(int, not null
c.cost(money null)
TableD
D.Dropshipperid(Pk,int,not null)
D.Dropshipper(nvarchar(50))
D.Remarks(nvarchar(50))
Table 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)
这是我查询的示例结果:
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 的 subcost 的总和。像这样的东西
DropShipperID DropShipper SubCost etc
------------------------------------------------
1 Airmail 624
2 DHL 1014
3 Fedex 974
4 UPS 1164
试试这个,
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()
、[=15 等聚合函数一起使用的列除外=]等
在你的例子中,数量、成本等列应该与 select
列表中的 SUM()
函数一起使用,而不应该在 group by
子句中使用。
我有三个表:Carrier(C)、DropShipper(D) 和 ShoppingCart(S),具有以下架构
Table C
c.id(Pk,int,not null)
c.dropshipperid(Fk,int,not null)
c.Prodid(int, not null
c.cost(money null)
TableD
D.Dropshipperid(Pk,int,not null)
D.Dropshipper(nvarchar(50))
D.Remarks(nvarchar(50))
Table 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)
这是我查询的示例结果:
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 的 subcost 的总和。像这样的东西
DropShipperID DropShipper SubCost etc
------------------------------------------------
1 Airmail 624
2 DHL 1014
3 Fedex 974
4 UPS 1164
试试这个,
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()
、[=15 等聚合函数一起使用的列除外=]等
在你的例子中,数量、成本等列应该与 select
列表中的 SUM()
函数一起使用,而不应该在 group by
子句中使用。