title: Computed column expression
body: <p>I have a specific need for a computed column called <strong>ProductCode</strong></p>
  
  <pre><code>ProductId | SellerId | ProductCode
  1           1           000001
  2           1           000002
  3           2           000001       
  4           1           000003
  </code></pre>
  
  <p><strong>ProductId</strong> is identity, increments by 1.
  <strong>SellerId</strong> is a foreign key.</p>
  
  <p>So my computed column <strong>ProductCode</strong> must look how many products does Seller have and be in format 000000. The problem here is how to know which Sellers products to look for?</p>
  
  <p>I've written have a TSQL which doesn't look how many products does a seller have</p>
  
  <pre><code>ALTER TABLE dbo.Product
  ADD ProductCode AS  RIGHT('000000' + CAST(ProductId AS VARCHAR(6)) , 6) PERSISTED
  </code></pre>