title: MS SQL Computed column
body: <p>I want to create a column based on COUNT(*) on another table, and when a record is deleted from that table it should decrease the value in this new column and vice versa. So, here is the query:</p>
  
  <blockquote>
    <p>SELECT COUNT (*) FROM dbo.Korisnik1_FakturaStavka GROUP BY dbo.Korisnik1_FakturaStavka.FakturaID</p>
  </blockquote>
  
  <p>And it returns this: <img src="https://i.stack.imgur.com/1v4vc.jpg" alt="enter image description here"></p>
  
  <p>And when I try to create a computated column like this:</p>
  
  <blockquote>
    <p>CREATE TABLE test(
        NumberOF as (SELECT COUNT (*) FROM dbo.Korisnik1_FakturaStavka GROUP BY dbo.Korisnik1_FakturaStavka.FakturaID)  )</p>
  </blockquote>
  
  <p>I get the following error: </p>
  
  <pre><code>Subqueries are not allowed in this context. Only scalar expressions are allowed.
  </code></pre>
  
  <p>Here is the main table that I want to compute from:</p>
  
  <p><img src="https://i.stack.imgur.com/nb4yh.jpg" alt="enter image description here"></p>
  
  <p>How can I resolve this ?</p>