在mssql中给出错误

时间:2016-09-08 06:00:58

标签: sql-server

我正在尝试通过mssql中的group_by子句获取记录,但它给我一个错误,如 -

  

专栏' SYWOTP.dbo.MLPurchaseOrder.POID'在选择列表中无效,因为它不包含在聚合函数或GROUP BY子句中。

以下是我的查询...

SELECT "MLPurchaseOrder"."POID",  "MLPurchaseOrder"."PONumber",    "MLPurchaseOrder"."CreatedDate",  "MLPurchaseOrderDetails"
."POID" as "MapperPOID",  "MLStorageSystem"."ProductID",       "ProductMaster"."ProductName", "ProductEntityMapper"
."ActualPriceUSD"
FROM "SYWOTP"."dbo"."MLPurchaseOrder"
JOIN "SYWOTP"."dbo"."MLPurchaseOrderDetails" ON   "MLPurchaseOrderDetails"."POID" = "MLPurchaseOrder"."POID"

JOIN "SYWOTP"."dbo"."MLStorageSystem" ON   "MLStorageSystem"."MLStorageID"   = "MLPurchaseOrderDetails"."MLStorageID"

JOIN "ExBMPCatalog"."dbo"."ProductMaster" ON "ProductMaster"."ProductID" = "MLStorageSystem"."ProductID"

JOIN "ExBMPCatalog"."dbo"."ProductEntityMapper" ON   "ProductEntityMapper"."productID" = "MLStorageSystem"
."ProductID"
WHERE "ProductMaster"."MerchantID" = 2
AND "MLStorageSystem"."EXBWarehouseID" IS NULL
AND "MLPurchaseOrderDetails"."BatchHash" IS NULL
OR "MLPurchaseOrderDetails"."BatchHash" = ''
GROUP BY "ExBMPCatalog"."dbo"."ProductMaster"."ProductID"
ORDER BY "ProductName" ASC
OFFSET 0 ROWS FETCH NEXT 10 ROWS ONLY

任何帮助都会很棒...提前thnxx:)

2 个答案:

答案 0 :(得分:1)

请使用正确的别名以避免代码复杂性。您应该将select语句中的所有列添加到Group by或者您似乎没有在select中使用任何聚合函数,因此您只需使用Distinct。

试试这个,

使用分组依据:

SELECT MLPO.POID
    ,MLPO.PONumber
    ,MLPO.CreatedDate
    ,MLPOD.POID AS MapperPOID
    ,MLSS.ProductID
    ,PM.ProductName
    ,PEM.ActualPriceUSD
FROM SYWOTP.dbo.MLPurchaseOrder MLPO
JOIN SYWOTP.dbo.MLPurchaseOrderDetails MLPOD ON MLPOD.POID = MLPO.POID
JOIN SYWOTP.dbo.MLStorageSystem MLSS ON MLSS.MLStorageID = MLPOD.MLStorageID
JOIN ExBMPCatalog.dbo.ProductMaster PM ON PM.ProductID = MLSS.ProductID
JOIN ExBMPCatalog.dbo.ProductEntityMapper PEM ON PEM.productID = MLSS.ProductID
WHERE PM.MerchantID = 2
    AND MLSS.EXBWarehouseID IS NULL
    AND MLPOD.BatchHash IS NULL
    OR MLPOD.BatchHash = ''
GROUP BY MLPO.POID
    ,MLPO.PONumber
    ,MLPO.CreatedDate
    ,MLPOD.POID
    ,MLSS.ProductID
    ,PM.ProductName
    ,PEM.ActualPriceUSD
ORDER BY ProductName ASC 
OFFSET 0 ROWS FETCH NEXT 10 ROWS ONLY

使用Distinct:

SELECT DISTINCT MLPO.POID
    ,MLPO.PONumber
    ,MLPO.CreatedDate
    ,MLPOD.POID AS MapperPOID
    ,MLSS.ProductID
    ,PM.ProductName
    ,PEM.ActualPriceUSD
FROM SYWOTP.dbo.MLPurchaseOrder MLPO
JOIN SYWOTP.dbo.MLPurchaseOrderDetails MLPOD ON MLPOD.POID = MLPO.POID
JOIN SYWOTP.dbo.MLStorageSystem MLSS ON MLSS.MLStorageID = MLPOD.MLStorageID
JOIN ExBMPCatalog.dbo.ProductMaster PM ON PM.ProductID = MLSS.ProductID
JOIN ExBMPCatalog.dbo.ProductEntityMapper PEM ON PEM.productID = MLSS.ProductID
WHERE PM.MerchantID = 2
    AND MLSS.EXBWarehouseID IS NULL
    AND MLPOD.BatchHash IS NULL
    OR MLPOD.BatchHash = ''

ORDER BY ProductName ASC 
OFFSET 0 ROWS FETCH NEXT 10 ROWS ONLY

答案 1 :(得分:0)

您按

分组
"ExBMPCatalog"."dbo"."ProdcutMaster".

但你的桌子好像是

"ExBMPCatalog"."dbo"."ProductMaster

始终使用别名,这也使代码可读