I am trying to run a query in SQL Server 2008 against a table where some data was entered inconsistently, and I have to process this.
Example table data:
OrderID Qty Price MarkedUpTotal
1 10 1.00 11.00
1 -1 1.00 -1.10
1 -1 1.00 1.10
I need to handle a situation where the Qty value is negative, but MarkedUpTotal was entered as positive.
I would like to run the following query:
SELECT OrderID, SUM(Qty) as OrderTotalQty,
SUM(Qty*Price) as InternalCost,
CASE WHEN Qty < 0 and MarkedUpTotal > 0
THEN sum(-1*MarkedUpTotal)
ELSE SUM(MarkedUpTotal) END as ClientCost
FROM OrderItems
GROUP BY OrderID
However, when I run this request, I get the following error:
The column is not valid in the selection list because it is neither contained in the aggregate function nor in the GROUP BY clause.
The MarkedUpTotal column is not valid in the select list because it is not contained in the aggregate function or in the GROUP BY clause.
I need the following result:
OrderID OrderTotalQty InternalCost ClientCost
1 8 8.00 8.80
, GROUP BY Qty MarkedUpTotal, CASE. ( CASE), , Qty Price GROUP BY.
SQL ? , ?
, . MarkedUpTotal , SUM (MarkedUpTotal) temp.