合并sql中不能正常工作的行



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

它可能会合并这些行,但为了某些结果,它会将它们划分为两个不同的行,我需要的是,只有当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

为什么不排成一排?

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

group by trxTransactionSaleItem.TransactionKey,
         InvProduct.Id,
         InvProduct.UPC,
         trxTransactionSaleItem.Description,
         invproduct.Description2,
         invProduct.ProductGroupKey,
         Price,
         ISNULL(UOM.Description, '')

相关内容

  • 没有找到相关文章

最新更新