please- help. I can't handle to build the specific query.
This is the query (slighty simplified)
$id in this case = id_user
SELECT
ARTIKELNR, ARTIKELTEXT, B_DATA,
margin = (SELECT TOP 1 SUM(BRNTZ / WERT*100 ) FROM BEW
WHERE b.ARTIKELNR = a.ARTIKELNR AND USR_NR = $id )
FROM BEW b
LEFT JOIN arb a ON b.ARTIKELNR = a.ARTIKELNR
WHERE b.USR_NR = $id
ORDER BY B_DATA DESC
I have a problem with margin - it has to be SUM of two columns but, if you can see, WERT column cannot be equal 0 ( because BRNTZ / WERT). In database sometimes this column equal 0 or even null.
I tried NULLIF but i just can't build it. The best i've done is:
SELECT TOP 1 SUM(BRNTZ / (WERT+0.001) * 100)...
Second of all, Can you tell me if it is good at all?
I am afraid that the margin can be taken from another ARTIKELNR. It's all about WHERE statement.
margin = (SELECT TOP 1 SUM(BRNTZ / WERT*100 ) FROM BEW
WHERE b.ARTIKELNR = ARTIKELNR AND BEW_LIEF_KUND_NR = $id )
EDIT:
i'm usinq SQL DB
When WERT == 0 then SUM(BRNTZ / WERT*100 ) = 0
SUM(BRNTZ / WERT*100 )to be ifWERTis 0 ?WERT= 0 thenSUM(BRNTZ / WERT*100 )= 0