这就是我的代码的样子:
SELECT p.productid AS 'Redni broj',
p.code AS 'Sifra robe',
p.unitname AS 'Jedinica mere',
di.quantity AS 'Kolicina',
di.price 'Stara cena',
di.itemvalue 'Stari iznos',
di.price2 AS 'Nova cena',
(di.price2 * di.quantity) AS 'Novi iznos',
(di.price2 * di.quantity - di.itemvalue) AS 'Difference',
SUM(di.price2 * di.quantity - di.itemvalue > 0) AS 'Positive difference',
SUM(di.price2 * di.quantity - di.itemvalue < 0) AS 'Negative Difference'
FROM product AS p
LEFT JOIN productcategorypr AS pc ON p.productid = pc.productid
LEFT JOIN documentitem AS di ON p.productid = di.productid
GROUP BY p.productid
我需要将Diffrence过滤为2列,所有差异均低于0,所有差异均高于0,差异为NewPrice - Old Price ... 谢谢你的建议...
答案 0 :(得分:4)
此表达式不正确:
SUM(di.price2 * di.quantity - di.itemvalue > 0) AS 'Positive difference'
您需要将其替换为CASE
表达式:
SUM(CASE WHEN di.price2 * di.quantity > di.itemvalue THEN di.price2 * di.quantity - di.itemvalue ELSE 0 END) AS 'Positive difference'
使用Negative difference
表达式做同样的事情:
SUM(CASE WHEN di.price2 * di.quantity < di.itemvalue THEN di.price2 * di.quantity - di.itemvalue ELSE 0 END) AS 'Negative Difference'
答案 1 :(得分:0)
SUM(案例时间(di.price2 * di.quantity - di.itemvalue)&gt; 0那么(di.price2 * di.quantity - di.itemvalue)ELSE 0 END)AS'积极差异'
SUM(案例时间(di.price2 * di.quantity - di.itemvalue)&lt; 0 THEN(di.price2 * di.quantity - di.itemvalue)ELSE 0 END)AS'Degative Difference'