title: SQL Server - COUNT function - possible conflict with GROUP BY?
body: <p>I'm using the below code in a view in order to extract some data from a table:</p>
  
  <pre><code>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
  </code></pre>
  
  <p>The point is that, even though I have used <code>COUNT</code> function to count how many lines are in the document lines, I always get 1 on both rows. Shouldn't be the result 2? Is there anything that I am missing or I haven't used in a proper way?</p>
  
  <p>I'm thinking that it might be something related to <code>GROUP BY</code> clause which I haven't used it properly.</p>
  
  <p><strong>OUTPUT</strong></p>
  
  <pre><code>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
  </code></pre>
  
  <p>Thanks!</p>