Hi
Sorry to bother you peeps again but would like some help on a COUNT. I want to return the count of rows returned by PO number and add it into a column at the end of the query. So if I return 8 rows I expect the number at the end to be 8. The next PO may have 5 so 5 would be displayed. I have been playing in the area where it says SUM (Distinct) but using count. Just cant seem tog et it working, any ideas please. Many Thanks
Sorry to bother you peeps again but would like some help on a COUNT. I want to return the count of rows returned by PO number and add it into a column at the end of the query. So if I return 8 rows I expect the number at the end to be 8. The next PO may have 5 so 5 would be displayed. I have been playing in the area where it says SUM (Distinct) but using count. Just cant seem tog et it working, any ideas please. Many Thanks
SQL:
SELECT dbo.PurchaseOrderHeader.DateTimeCreated, dbo.PurchaseOrderHeader.DateExpected, dbo.PurchaseOrderLine.Quantity, dbo.PurchaseOrderLine.QuantityReceived,
dbo.PurchaseOrderLine.Quantity - dbo.PurchaseOrderLine.QuantityReceived AS PoOutstandingQty,
dbo.PurchaseOrderLine.UnitCostPrice * dbo.PurchaseOrderLine.Quantity + dbo.PurchaseOrderLine.TotalAdditionalCost AS PoOriginalValue,
dbo.PurchaseOrderLine.TotalAdditionalCost / NULLIF (dbo.PurchaseOrderLine.TotalVolume, 0) AS AdditonalCost, dbo.PurchaseOrderLine.UnitCostPrice AS CostperM3,
dbo.PurchaseOrderLine.TotalAdditionalCost / NULLIF (dbo.PurchaseOrderLine.TotalVolume, 0) + dbo.PurchaseOrderLine.UnitCostPrice AS CombineCostM3,
dbo.PurchaseOrderHeader.TimberMill, dbo.Supplier.Name, dbo.PurchaseOrderHeader.OrderStatus AS DetermineOUtstandingPO,
dbo.PurchaseOrderLine.LineStatus AS IndetfyOrderComplete, dbo.Product.ProductCode, dbo.Product.Description, dbo.Branch.Name AS Quay,
dbo.Users.Name AS POCreated, dbo.PurchaseOrderHeader.OrderType, dbo.PurchaseOrderHeader.PurchaseOrderNumber,
dbo.StockReceiptLine.QuantityReceived AS SRQtyReceived, dbo.StockReceiptHeader.DateReceived
---SUM(DISTINCT (dbo.StockReceiptHeader.StockReceiptNumber))AS Stock
FROM dbo.PurchaseOrderHeader INNER JOIN
dbo.PurchaseOrderLine ON dbo.PurchaseOrderHeader.PurchaseOrderID = dbo.PurchaseOrderLine.PurchaseOrderID INNER JOIN
dbo.Supplier ON dbo.PurchaseOrderHeader.SupplierID = dbo.Supplier.SupplierID INNER JOIN
dbo.Product ON dbo.PurchaseOrderLine.ProductID = dbo.Product.ProductID INNER JOIN
dbo.Branch ON dbo.PurchaseOrderHeader.BranchID = dbo.Branch.BranchID INNER JOIN
dbo.Users ON dbo.PurchaseOrderHeader.CreatedByID = dbo.Users.UserID INNER JOIN
dbo.StockReceiptLine ON dbo.PurchaseOrderHeader.PurchaseOrderID = dbo.StockReceiptLine.PurchaseOrderID AND
dbo.PurchaseOrderLine.PurchaseOrderLineID = dbo.StockReceiptLine.PurchaseOrderLineID AND dbo.Product.ProductID = dbo.StockReceiptLine.ProductID INNER JOIN
dbo.StockReceiptHeader ON dbo.StockReceiptLine.StockReceiptID = dbo.StockReceiptHeader.StockReceiptID
WHERE (dbo.PurchaseOrderHeader.Deleted = 0)
GROUP BY dbo.PurchaseOrderHeader.DateTimeCreated, dbo.PurchaseOrderHeader.DateExpected, dbo.PurchaseOrderLine.Quantity, dbo.PurchaseOrderLine.QuantityReceived,
dbo.PurchaseOrderLine.Quantity - dbo.PurchaseOrderLine.QuantityReceived,
dbo.PurchaseOrderLine.UnitCostPrice * dbo.PurchaseOrderLine.Quantity + dbo.PurchaseOrderLine.TotalAdditionalCost,
dbo.PurchaseOrderLine.TotalAdditionalCost / NULLIF (dbo.PurchaseOrderLine.TotalVolume, 0), dbo.PurchaseOrderLine.UnitCostPrice,
dbo.PurchaseOrderLine.TotalAdditionalCost / NULLIF (dbo.PurchaseOrderLine.TotalVolume, 0) + dbo.PurchaseOrderLine.UnitCostPrice,
dbo.PurchaseOrderHeader.TimberMill, dbo.Supplier.Name, dbo.PurchaseOrderHeader.OrderStatus, dbo.PurchaseOrderLine.LineStatus, dbo.Product.ProductCode,
dbo.Product.Description, dbo.Branch.Name, dbo.Users.Name, dbo.PurchaseOrderHeader.OrderType, dbo.PurchaseOrderHeader.PurchaseOrderNumber,
dbo.StockReceiptLine.QuantityReceived, dbo.StockReceiptHeader.DateReceived
HAVING (dbo.PurchaseOrderHeader.OrderType = 1) AND (dbo.PurchaseOrderHeader.PurchaseOrderNumber = 35489)