Sql server 盘点项目对应的列表项目

Sql server 盘点项目对应的列表项目,sql-server,Sql Server,我想要对应UPC的UPC计数,但并没有得到下面这样的正确结果 SELECT a.UPC,COUNT(*) FROM StoreTransactions a WITH (NOLOCK) JOIN StoreTransactions_Expanded_UOM c ON a.StoreTransactionID = c.StoreTransactionID LEFT JOIN ProductCatalog cat ON a.ProductID = cat.ProductID LEFT JOIN Pr

我想要对应UPC的UPC计数,但并没有得到下面这样的正确结果

SELECT  a.UPC,COUNT(*)
FROM StoreTransactions a WITH (NOLOCK)
JOIN StoreTransactions_Expanded_UOM c
ON a.StoreTransactionID = c.StoreTransactionID
LEFT JOIN ProductCatalog cat
ON a.ProductID = cat.ProductID
LEFT JOIN ProductCatalogBase base
ON cat.ProductCatalogID = base.ProductCatalogID
JOIN ProductIdentifiers d
ON cat.ProductID = d.ProductID AND d.ProductIdentifierTypeID = 2

GROUP BY a.UPC
, d.IdentifierValue, cat.PackDesc, a.ReportedCost, 
base.ManualHigh, base.ManualLow,cat.DateTimeCreated,cat.DateTimeLastUpdate
ORDER BY count(*) desc

您只需按a.UPC进行分组:

UPC             Count
071990316006    1463
026565245455    4530

为什么要按所有这些列进行分组?您应该只按a.UPC进行分组。为什么诺洛克?同意@SeanLange。如果只需要一个
UPC
计数,则只需
按a.UPC进行分组
。删除
分组依据中的其他列
。您还可以
将ProductIdentifiers
左JOIN ProductCatalog cat
表连接起来。这可能会给你不确定的结果。
SELECT  a.UPC,COUNT(*)
FROM StoreTransactions a WITH (NOLOCK)
JOIN StoreTransactions_Expanded_UOM c
ON a.StoreTransactionID = c.StoreTransactionID
LEFT JOIN ProductCatalog cat
ON a.ProductID = cat.ProductID
LEFT JOIN ProductCatalogBase base
ON cat.ProductCatalogID = base.ProductCatalogID
JOIN ProductIdentifiers d
ON cat.ProductID = d.ProductID AND d.ProductIdentifierTypeID = 2
GROUP BY a.UPC
ORDER BY count(*) desc