SQL Server - COUNT函数 - 可能与GROUP BY冲突?

时间:2018-05-17 09:23:58

标签: sql-server

我在视图中使用以下代码,以便从表中提取一些数据:

SELECT

esli.fDocumentGID AS DocGID,
count(esli.LineNumber) X,
esli.GID AS LineGID,
esli.LineNumber AS ItemNum,
esit.BarCode AS EAN,
esic.Code AS BuyerItemID,
esit.Code AS SellerItemID,
'' AS CustomTariffNumber,
'CU' AS ProductIdentifierExt,
ISNULL(CONVERT(decimal(18, 2), esli.Quantity), '0')*CONVERT(int, ISNULL(esim.Relation, 0)) AS PacketContentQuantity,
ISNULL(esim.fMUCode, '') AS PackageType,
ISNULL(CONVERT(decimal(18, 2), esli.Quantity), '0') AS QuantityValue,

-- Se aplica doar pentru storno/corectie --
'' AS QuantityValueWithdrawn,
-------------------------------------------

'S' AS TaxCategoryCoded,

-- Se aplica doar pentru storno/corectie --
'' AS TaxCategoryCodesWas,
-------------------------------------------

CONVERT(decimal(10, 2), ISNULL(REPLACE(REPLACE(esvc.Description, 'TVA', ''), '%', ''), '')) AS TaxPercent,

-- Se aplica doar pentru storno/corectie --
'' AS TaxPercentWas,
-------------------------------------------

CONVERT(decimal(18, 2), esdt.CurrencyVATValue) AS TaxAmount,

-- Se aplica doar pentru storno/corectie --
'' AS TaxAmountWithdrawn,
-------------------------------------------

CONVERT(decimal(18, 2), esdt.CurrencyPayableAmount) AS MonetaryGrossValue,

-- Se aplica doar pentru storno/corectie --
'' AS MonetaryGrossValueWithdrawn,
-------------------------------------------

CONVERT(decimal(18, 2), esdt.CurrencyNetValue) AS MonetaryNetValue,

-- Se aplica doar pentru storno/corectie --
'' AS MonetaryNetValueWithdrawn,
-------------------------------------------

CONVERT(decimal(18, 2), (esli.Quantity*esli.Price-esli.GrossDiscountValue)) AS MonetaryAmountPayable,

-- Se aplica doar pentru storno/corectie --
'' AS MonetaryAmountPayableWithdrawn,
-------------------------------------------


esimit.fMUCode AS UnitOfMeasure,
esim.fMUCode AS UnitOfMeasureXCBL,
esimit.fMUCode AS PackUnitOfMeasure,
CONVERT(decimal(10, 2), esli.NetValue) AS UnitPriceValue,

-- Se aplica doar pentru storno/corectie --
'' AS UnitPriceValueWas,
-------------------------------------------

CONVERT(decimal(10, 2), esli.TotalValue) AS UnitPriceValueGross,

-- Se aplica doar pentru storno/corectie --
'' AS UnitPriceValueGrossWas,
-------------------------------------------

LEFT(esit.Description, 35) AS ItemName,
'123' AS BuyerOrderNumberDet,
'1900-01-01' AS BuyerOrderDateDet,

-- Delivery detail --
ISNULL(esdtt.ADCode, '0') AS DeliveryDocumentNumberDet,
CONVERT(nvarchar(50), ISNULL(esdtt.ADRegistrationDate, '1900-01-01'), 101) AS DeliveryDateDet,
COUNT(esli.LineNumber) AS NumberOfLines,
CONVERT(decimal(18, 2), esli.CurrencyNetValue) AS NetValue,

-- Se aplica doar pentru storno/corectie --
'' AS NetValueDiff,
-------------------------------------------

CONVERT(decimal(18, 2), esli.TaxesValue) AS TaxValue,

-- Se aplica doar pentru storno/corectie --
'' AS TaxValueDiff,
-------------------------------------------

'' AS TaxableValue,

-- Se aplica doar pentru storno/corectie --
'' AS TaxableValueDiff,
-------------------------------------------

CONVERT(decimal(18, 2), esdt.CurrencyPayableAmount) AS GrossValue,

-- Se aplica doar pentru storno/corectie --
'' AS GrossValueDiff,
-------------------------------------------

-- Tax --
'S' AS TaxCategoryCodedd,
CONVERT(decimal(10, 2), ISNULL(REPLACE(REPLACE(esvc.Description, 'TVA', ''), '%', ''), '')) AS TaxPercent2,
CONVERT(decimal(18, 2), esdt.CurrencyVATValue) AS TaxNettoAmount,

-- Se aplica doar pentru storno/corectie --
'' AS TaxNettoAmountDiff,
-------------------------------------------

'' AS TaxableAmountt,

-- Se aplica doar pentru storno/corectie --
'' AS TaxableAmounttDiff,
-------------------------------------------

'' AS TaxAmountt,

-- Se aplica doar pentru storno/corectie --
'' AS TaxAmountDiff,
-------------------------------------------

'' AS TaxGrossAmount,

-- Se aplica doar pentru storno/corectie --
'' AS TaxGrossAmountDiff
-------------------------------------------




FROM ESFILineItem esli
JOIN ESFIDocumentTrade esdt ON esli.fDocumentGID=esdt.GID
LEFT JOIN ESFIDocumentTrade esdtt ON esdt.ADReferenceCode=esdtt.ADCode
JOIN ESFIItem esit ON esli.fItemGID=esit.GID
JOIN ESMMItemCodes esic ON esit.GID=esic.ItemGID
JOIN ESMMItemMU esim ON esli.fItemMUGID=esim.GID
JOIN ESGOZVATCategory esvc ON esli.fVATCategoryCode=esvc.Code
JOIN ESMMItemmu esimit ON esit.fMainMUGID=esimit.GID


WHERE esli.fDocumentGID=(SELECT TOP 1 GID FROM ERPBasic.dbo.EDINet_Invoices_Auchan)


GROUP BY  esli.LineNumber, esit.BarCode, esic.Code, esit.Code, esli.Quantity, esim.Relation, esim.fMUCode, esvc.Description,  esdt.CurrencyVATValue,
esdt.CurrencyPayableAmount, esdt.CurrencyNetValue, esli.Price, esli.GrossDiscountValue, esimit.fMUCode, esli.NetValue, esli.TotalValue, esit.Description, esli.CurrencyNetValue,
esli.TaxesValue, esli.fDocumentGID, esli.GID, esdtt.ADCode, esdtt.ADRegistrationDate


GO

关键在于,即使我使用COUNT函数来计算文档行中有多少行,我总是在两行上得到1。不应该是结果2?我有什么遗漏或者没有以正确的方式使用过吗?

我认为它可能与GROUP BY条款有关,我没有正确使用它。

输出

DocGID                                  X   LineGID
5236BD3F-668E-4B1E-9B3D-34E77B895FF1    1   2721B7D7-7AB6-44EC-BADB-F5F936DEDFE9
5236BD3F-668E-4B1E-9B3D-34E77B895FF1    1   70D9D3AE-5691-42C5-8212-EE7E079FC17F

谢谢!

1 个答案:

答案 0 :(得分:1)

您在Group By子句中使用esli.GID,这对于行来说是不同的。