-3

我在为行合并时遇到了这个问题,我已经进行了查询,但结果不正确。

它可能会合并行,但对于某些结果,它将它们分成两个不同的行,我需要的是只有在 id 匹配时才将它们全部放在一行中。

这是查询:

Select  
    trxTransactionSaleItem.TransactionKey   
    , 'Sale' As TrxnType
    , InvProduct.Id 
    , InvProduct.UPC
    , trxTransactionSaleItem.Description
    , invproduct.Description2
    , invProduct.ProductGroupKey
    , sum (Quantity/ISNULL(UOMBaseQuantity,1)) as Quantity
    , Price
    , SUM(DiscountAmount) AS DA
    , SUM(SurchargeTotal) AS ST
    , sum (Total) as Total
    , ISNULL(UOM.Description,'') as UOM
From    
    trxTransactionSaleItem
INNER JOIN  
    InvProduct on trxTransactionSaleItem.ProductKey = InvProduct.ProductKey
LEFT JOIN 
    InvUOMGroupDetail UOMD on UOMGroupDetailKey = UOMD.UOMGroupDetailKey
LEFT JOIN 
    InvUOM UOM on UOMD.UOMKey = UOM.UOMKey
Where 
    Type = 0 
    And IsExchange = 0
    And trxTransactionSaleItem.TransactionKey = 60000000022537
group by 
    trxTransactionSaleItem.TransactionKey
    , InvProduct.Id
    , InvProduct.UPC
    , trxTransactionSaleItem.Description
    , invproduct.Description2
    , invProduct.ProductGroupKey
    , Quantity 
    , Price
    , DiscountAmount 
    , SurchargeTotal
    , Total
    , UOM.Description

那么为什么它不排成一排呢?

4

1 回答 1

1

group by应该只有不在聚合函数中的字段。它应该看起来像:

group by trxTransactionSaleItem.TransactionKey,
         InvProduct.Id,
         InvProduct.UPC,
         trxTransactionSaleItem.Description,
         invproduct.Description2,
         invProduct.ProductGroupKey,
         Price,
         ISNULL(UOM.Description, '')
于 2015-02-15T14:24:55.320 回答