0

I have the following query:

SELECT dbo.saleDocumentDetails.ID,
       dbo.saleDocumentDetails.DocumentID,
       dbo.saleDocumentDetails.invProductID,
       dbo.saleDocumentDetails.Qty,
       dbo.saleDocumentDetails.QtyConfirmed,
       dbo.saleDocumentDetails.AmountPrice,
       dbo.saleDocumentDetails.AmountUserPrice,
       dbo.saleDocumentDetails.AmountCost,
       dbo.saleDocumentDetails.RespiteDays,
       dbo.saleDocumentDetails.Date_UpdateLast,
       CASE 
            WHEN io1.Code = '33' THEN SUM(idd.Qty)
            ELSE SUM(0)
       END                               AS Qty_BackSale,
       CASE 
            WHEN io1.Code = '41' THEN SUM(idd.Qty)
            ELSE SUM(0)
       END                               AS Qty_Stock
FROM   dbo.saleDocuments
       INNER JOIN dbo.saleDocumentDetails
            ON  dbo.saleDocuments.ID = dbo.saleDocumentDetails.DocumentID
       LEFT OUTER JOIN dbo.invDocumentDetails   AS  idd
            ON  dbo.saleDocumentDetails.ID = idd.saleDocumentDetailID
       LEFT OUTER JOIN dbo.invDocuments  AS id2
            ON  id2.ID = idd.invDocumentID
       LEFT JOIN dbo.invOperations       AS io1
            ON  (io1.ID = id2.invOperationID)
WHERE  (dbo.saleDocumentDetails.ID = 1295617)
GROUP BY
       dbo.saleDocumentDetails.ID,
       dbo.saleDocumentDetails.DocumentID,
       dbo.saleDocumentDetails.invProductID,
       dbo.saleDocumentDetails.Qty,
       dbo.saleDocumentDetails.QtyConfirmed,
       dbo.saleDocumentDetails.AmountPrice,
       dbo.saleDocumentDetails.AmountUserPrice,
       dbo.saleDocumentDetails.AmountCost,
       dbo.saleDocumentDetails.RespiteDays,
       dbo.saleDocumentDetails.Date_UpdateLast,
       io1.Code

its result is:

enter image description here

I would like to achieve:

enter image description here

How should I change the query?

0

3 Answers 3

1

As mentioned JRLambert, move SUM() outside CASE, and remove io1.Code from GROUP BY

Sign up to request clarification or add additional context in comments.

Comments

1

Move the SUM() in your CASE statements outside of the CASE:

   SELECT 
       dbo.saleDocumentDetails.ID,
       dbo.saleDocumentDetails.DocumentID,
       dbo.saleDocumentDetails.invProductID,
       dbo.saleDocumentDetails.Qty,
       dbo.saleDocumentDetails.QtyConfirmed,
       dbo.saleDocumentDetails.AmountPrice,
       dbo.saleDocumentDetails.AmountUserPrice,
       dbo.saleDocumentDetails.AmountCost,
       dbo.saleDocumentDetails.RespiteDays,
       dbo.saleDocumentDetails.Date_UpdateLast,
       SUM(CASE 
            WHEN io1.Code = '33' THEN idd.Qty
            ELSE 0
       END)                              AS Qty_BackSale,
       SUM(CASE 
            WHEN io1.Code = '41' THEN idd.Qty
            ELSE 0
       END)                              AS Qty_Stock
   FROM
       dbo.saleDocuments
       INNER JOIN dbo.saleDocumentDetails
           ON  dbo.saleDocuments.ID = dbo.saleDocumentDetails.DocumentID
       LEFT OUTER JOIN dbo.invDocumentDetails   AS  idd
           ON  dbo.saleDocumentDetails.ID = idd.saleDocumentDetailID
       LEFT OUTER JOIN dbo.invDocuments  AS id2
           ON  id2.ID = idd.invDocumentID
       LEFT JOIN dbo.invOperations       AS io1
           ON  (io1.ID = id2.invOperationID)
WHERE
    (dbo.saleDocumentDetails.ID = 1295617)
GROUP BY
    dbo.saleDocumentDetails.ID,
    dbo.saleDocumentDetails.DocumentID,
    dbo.saleDocumentDetails.invProductID,
    dbo.saleDocumentDetails.Qty,
    dbo.saleDocumentDetails.QtyConfirmed,
    dbo.saleDocumentDetails.AmountPrice,
    dbo.saleDocumentDetails.AmountUserPrice,
    dbo.saleDocumentDetails.AmountCost,
    dbo.saleDocumentDetails.RespiteDays,
    dbo.saleDocumentDetails.Date_UpdateLast,
    io1.Code;

1 Comment

Thanks, it is needed to remove io1.Code from group by.
0

Have you tried to remove group by dbo.saleDocumentDetails.ID ?

2 Comments

He needs to keep that.
Yes. but all of the columns are same except the two last columns.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.