我有一张名为 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。谢谢