【发布时间】:2013-06-11 14:09:17
【问题描述】:
我有一个名为 products 的表。
ProductId| ProductName| ProductType| ProductSize
1 | a | yellow | 12
2 | b | green | 13
3 | c | yellow | 12
4 | d | yellow | 15
________________________________________________
我想将每个产品的计数作为最后的一列,其中 productType 和 ProductSize 匹配,这是我想要的例外结果..
ProductID|ProductName|ProductType|ProductSize|TotalProduct
1 | a | yellow | 12 | 2
2 | b | green | 13 | 1
3 | c | yellow | 12 | 2
4 | d | yellow | 15 | 1
_________________________________________________________
我尝试过但失败的一些方法是这样的。
select ProductId, ProductName, ProductType, ProductSize,
(select count(*) from Product where ProductType=(Products.ProductType) and ProductSize=(products.productSize)) as [TotalProduct] from Products
所有记录的返回 totalProduct = 4。 谢谢
【问题讨论】:
-
请通过添加适当的标签(Oracle、SQL Server、MySQL 等)来指定您的目标关系数据库管理系统。可能有一些答案利用了不受普遍支持的语言或产品功能。此外,通过使用特定的 RDBMS 对其进行标记,您的问题可能会受到更适合回答的人的关注。
-
我使用的是 sql server 2008
标签: sql join count subquery where