title: How can I write a mathematical formula in SQL Server
body: <p>I'm trying to write a formula that does the following : </p>
  
  <ul>
  <li>takes the <code>LOS_Day</code> (calculated datediff with two columns <code>AdmitDate</code> and <code>DischargeDate</code>) and multiplies it by 100, then divides it by <code>NumberofBeds</code> (was calculated using the <code>Count</code> function from <code>V_Bed</code> table) and multiplies it by input date range (number of days) from the user <code>@fromdate - @todate</code></li>
  </ul>
  
  <p><a href="https://i.stack.imgur.com/LgW8v.gif" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/LgW8v.gif" alt="enter image description here"></a></p>
  
  <p>Find the query I used here:</p>
  
  <pre><code>SELECT 
      pt.regcode, 
      pt.admissionnumber, 
      pt.admissionid, 
      pt.admitdate, 
      pt.dischargedate, 
      wardbed.bedid, 
      bed.bedname, 
      wardbed.ward, 
      DATEDIFF(HOUR, pt.admitdate, pt.dischargedate) AS LOS_Hour, 
      DATEDIFF(DAY, pt.admitdate, pt.dischargedate)  AS LOS_day, 
      (SELECT COUNT(*) FROM v_beds) AS NumberofBeds 
  FROM   
      v_bedallocations bed, 
      v_dbpatientadmissions pt, 
      v_beds wardbed  
  WHERE  
      pt.admissionnumber = bed.admissionnumber 
      AND pt.admissionid = wardbed.admissionid 
      AND bed.bedname = wardbed.bedname 
  GROUP BY 
      bed.bedname, pt.regcode, pt.admissionnumber, 
      pt.admissionid, pt.admitdate, pt.dischargedate, 
      wardbed.bedid, bed.bedname, wardbed.ward 
  </code></pre>
  
  <p>Here's the output from that query:
  <a href="https://i.stack.imgur.com/wR6hv.jpg" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/wR6hv.jpg" alt="enter image description here"></a></p>