Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/joomla/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Sql 如何获得具有相同外键的两列之和_Sql_Sql Server 2005 - Fatal编程技术网

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

Sql 如何获得具有相同外键的两列之和,sql,sql-server-2005,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.c

我有三个表: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的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()
min()


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

非常感谢您的时间,在测试您的更正时,它出现了以下错误:Msg 8120,级别16,状态1,过程GetCarrierCost,第5行列“tbl_DropShipper.DropShipperID”在选择列表中无效,因为它未包含在聚合函数或GROUP BY子句中。当我添加它所请求的列时,它仍然会重复出现相同的D.dropshipperidaldalso,当做一些小的代码更改时,请解释一下你做了什么。除了OP(显然,在本例中,OP也是如此)之外,这个答案几乎对任何人都没有用处。@siride,我所做的是通过采用Nikhil查询来替换我之前的查询,从而添加groupby函数。并弹出此错误:“D.DropShipperID”在选择列表中无效,因为它不包含在聚合函数或GROUP BY子句中。我将D.DropshipperID按函数包括在groupby C.DropshipperID,D.DropshipperID,D.DropShipper中,错误移到了Qtyso,所以我的最后一个groupby函数是这样的:groupby C.DropshipperID,D.DropshipperID,D.DropShipper,S.Qty,C.Cost。它在这一点上执行,但结果仍然不是我想要的。@NikhilKM,是的,它帮助了我一百万次,正是我想要的。