1

只有当它们存在于另一个表中时,我才能对一个表中的值求和。

例如 table1 - typeOfBiscuits

biscuits
jaffacakes
digestives

table2 - inventry
type number
digestives 2
digestives 3
jaffacakes 2
digestives 3
jaffacakes 4
cheesecake 1

期望的结果select Count(number) from Inventry where type = <not sure how to get type from typeOfBisuit table>

type count
digestives 8
jaffacakes 6

我的尝试如下,但我收到“对多部分字符串的引用”错误

select Count(number) from Inventry where type = typeOfBiscuits.type
4

3 回答 3

1
SELECT typeOfBiscuits.biscuits, Sum(Inventory.Number) as [Count]
FROM typeOfBiscuits INNER JOIN Inventory
ON typeOfBiscuits.biscuits = Inventory.type
GROUP BY typeOfBiscuits.type
于 2013-08-27T12:35:07.337 回答
0

尝试这个:

SELECT TYPE, 
       Sum(NUMBER) AS [Count] 
FROM   TABLE2 
WHERE  TYPE IN (SELECT TYPEOFBISCUITS 
                FROM   TABLE1) 
GROUP  BY TYPE 
于 2013-08-27T12:35:45.790 回答
0

请试试:

SELECT 
 t1.Type,
 SUM(number) as [Count]
FROM
 inventry t1 INNER JOIN typeOfBiscuits t2 ON t1.Type=t2.Type
GROUP BY t1.Type
于 2013-08-27T12:33:58.157 回答