79698073

Date: 2025-07-11 09:06:12
Score: 1.5
Natty:
Report link

Thank you all for your efforts helping me to better understand that there is moore than one way to arrive at a solution that can vary between db engines. I had to use @ValNik suggestion to use a subquery in order to finish our new item information webpage presenting yearly price changes summary.

This is propably not optimal but it works and that is good enough.

SELECT MYPERIOD,MYQTY1,SALES,COST,PRICE,COSTPRICE,MARGIN,
    LAG( PRICE)OVER(ORDER BY MYPERIOD) AS PREV_PRICE
FROM(
SELECT
    LEFT(p.D3611_Transaktionsda, 4) AS MYPERIOD,
    SUM(p.D3631_Antal) AS MYQTY1,
    SUM(p.D3653_Debiterbart) AS SALES,
    SUM(p.D3651_Kostnad) AS COST,
    SUM(p.D3653_Debiterbart) / SUM(p.D3631_Antal) AS PRICE,
    SUM(p.D3651_Kostnad) / SUM(D3631_Antal) AS COSTPRICE,
    (SUM(p.D3653_Debiterbart) - SUM(p.D3651_Kostnad)) / SUM(p.D3653_Debiterbart) AS MARGIN  
  FROM PUPROTRA AS p
  WHERE p.D3605_Artikelkod = 'XYZ'
    AND p.D3601_Ursprung='O'
    AND p.D3625_Transaktionsty = 'U'
    AND p.D3631_Antal<>0 AND p.D3653_Debiterbart<>0
  GROUP BY LEFT(p.D3611_Transaktionsda, 4)
) as T
Reasons:
  • Blacklisted phrase (0.5): Thank you
  • Long answer (-1):
  • Has code block (-0.5):
  • User mentioned (1): @ValNik
  • Self-answer (0.5):
  • Low reputation (1):
Posted by: Acke