title: Build specific SQL query
body: <p>please- help.
  I can't handle to build the specific query.</p>
  
  <p>This is the query (slighty simplified)</p>
  
  <p><code>$id</code> in this case = id_user</p>
  
  <pre><code>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
  </code></pre>
  
  <p>I have a problem with <code>margin</code> - it has to be <code>SUM</code> of two columns but, if you can see, <code>WERT</code> column cannot be equal 0 ( because <code>BRNTZ / WERT</code>). In database sometimes this column equal 0 or even <code>null</code>. </p>
  
  <p>I tried <code>NULLIF</code> but i just can't build it. The best i've done is:</p>
  
  <pre><code>  SELECT TOP 1 SUM(BRNTZ / (WERT+0.001) * 100)...
  </code></pre>
  
  <p>Second of all, Can you tell me if it is good at all?
  I am afraid that the <code>margin</code> can be taken from another <code>ARTIKELNR</code>. It's all about  <code>WHERE</code> statement. </p>
  
  <pre><code>margin = (SELECT TOP 1 SUM(BRNTZ / WERT*100 ) FROM BEW 
           WHERE b.ARTIKELNR = ARTIKELNR AND BEW_LIEF_KUND_NR = $id )
  </code></pre>
  
  <p><strong>EDIT:</strong></p>
  
  <p>i'm usinq SQL DB<br>
  When <code>WERT == 0</code> then <code>SUM(BRNTZ / WERT*100 ) = 0</code></p>