title: Union of multiple Database queries with same parameters
body: <p>I am having this code and its running as expected. However I am trying to find a better way to rewrite the following query as it can be seen that the dates and account codes are repeated all the time.</p>
  <p>The data is being extracted from 3 Databases i.e. Db1, Db2 and Db3.
  The tables of each Database are similar. Even the AcctCodes to be extracted are similar.</p>
  <p>So,I am wondering if the following code can be rewritten in few lines.</p>
  <p>Since, the AcctCodes are similar, so adding an empty row with database name as Headers between each query helps me to identify them.</p>
  <blockquote>
  <p>Select 'Outlet1','0','0' from Dummy</p>
  </blockquote>
  <p>So if there is a better version of the following code, please let me know. Thanks.</p>
  <pre><code>Select 'Outlet1','0','0' from Dummy
  
  UNION ALL
  
  SELECT 
      T1.&quot;AcctCode&quot;,T1.&quot;AcctName&quot;, SUM(T0.&quot;Debit&quot;) - SUM(T0.&quot;Credit&quot;) as TotalBal
      FROM Db1.Table1 T0  
          INNER JOIN Db1.Table2 T1 ON T0.&quot;Account&quot; = T1.&quot;AcctCode&quot; 
          INNER JOIN Db1.Table3 T2 ON T0.&quot;TransId&quot; = T2.&quot;TransId&quot;
      WHERE 
          T1.&quot;AcctCode&quot; in
          (
          '105004001','105005001','105006001','105007001','105008001','105009001','105104001','105105001','105106001',
          '105107001','105108001','105109001','106001001','107009001','109018001','109022001','201001001','201002001'
          )      
          AND '01.01.0001' &lt;= '01.07.2020'  
          AND '31.03.2021' &gt;= T0.&quot;RefDate&quot;  
      GROUP BY T1.&quot;AcctCode&quot;,T1.&quot;AcctName&quot;
  
  UNION ALL
  
  Select 'Outlet2','0','0' from Dummy
  
  UNION ALL
  
  SELECT 
      T1.&quot;AcctCode&quot;,T1.&quot;AcctName&quot;, SUM(T0.&quot;Debit&quot;) - SUM(T0.&quot;Credit&quot;) as TotalBal
      FROM Db2.Table1 T0  
          INNER JOIN Db2.Table2 T1 ON T0.&quot;Account&quot; = T1.&quot;AcctCode&quot; 
          INNER JOIN Db2.Table3 T2 ON T0.&quot;TransId&quot; = T2.&quot;TransId&quot;
      WHERE 
          T1.&quot;AcctCode&quot; in
          (
          '105004001','105005001','105006001','105007001','105008001','105009001','105104001','105105001','105106001',
          '105107001','105108001','105109001','106001001','107009001','109018001','109022001','201001001','201002001'
          )      
          AND '01.01.0001' &lt;= '01.07.2020'  
          AND '31.03.2021' &gt;= T0.&quot;RefDate&quot;  
      GROUP BY T1.&quot;AcctCode&quot;,T1.&quot;AcctName&quot;
  
  UNION ALL
  
  Select 'Outlet3','0','0' from Dummy
  
  UNION ALL
  
  SELECT 
      T1.&quot;AcctCode&quot;,T1.&quot;AcctName&quot;, SUM(T0.&quot;Debit&quot;) - SUM(T0.&quot;Credit&quot;) as TotalBal
      FROM Db3.Table1 T0  
          INNER JOIN Db3.Table2 T1 ON T0.&quot;Account&quot; = T1.&quot;AcctCode&quot; 
          INNER JOIN Db3.Table3 T2 ON T0.&quot;TransId&quot; = T2.&quot;TransId&quot;
      WHERE 
          T1.&quot;AcctCode&quot; in
          (
          '105004001','105005001','105006001','105007001','105008001','105009001','105104001','105105001','105106001',
          '105107001','105108001','105109001','106001001','107009001','109018001','109022001','201001001','201002001'
          )      
          AND '01.01.0001' &lt;= '01.07.2020'  
          AND '31.03.2021' &gt;= T0.&quot;RefDate&quot;  
      GROUP BY T1.&quot;AcctCode&quot;,T1.&quot;AcctName&quot;
  </code></pre>
  <p>I have reframed my code now and now it looks something like this.</p>
  <pre><code>SELECT 
      'Databse1' as DataSource,T1.&quot;AcctCode&quot;,T1.&quot;AcctName&quot;, SUM(T0.&quot;Debit&quot;) - SUM(T0.&quot;Credit&quot;) as TotalBal
      FROM Db1.Table1 T0  
          INNER JOIN Db1.Table2 T1 ON T0.&quot;Account&quot; = T1.&quot;AcctCode&quot; 
          INNER JOIN Db1.Table3 T2 ON T0.&quot;TransId&quot; = T2.&quot;TransId&quot;
      WHERE 
          T1.&quot;AcctCode&quot; in
          (
          '105004001','105005001','105006001','105007001','105008001'
          )      
          AND '01.01.0001' &lt;= '01.07.2020'  
          AND '31.03.2021' &gt;= T0.&quot;RefDate&quot;  
      GROUP BY T1.&quot;AcctCode&quot;,T1.&quot;AcctName&quot;
  
  UNION ALL
  
  SELECT 
      'Database2' As DataSource,T1.&quot;AcctCode&quot;,T1.&quot;AcctName&quot;, SUM(T0.&quot;Debit&quot;) - SUM(T0.&quot;Credit&quot;) as TotalBal
      FROM Db2.Table1 T0  
          INNER JOIN Db2.Table2 T1 ON T0.&quot;Account&quot; = T1.&quot;AcctCode&quot; 
          INNER JOIN Db2.Table3 T2 ON T0.&quot;TransId&quot; = T2.&quot;TransId&quot;
      WHERE 
          T1.&quot;AcctCode&quot; in
          (
          '105004001','105005001','105006001','105007001' ,'105008001' 
          )      
          AND '01.01.0001' &lt;= '01.07.2020'  
          AND '31.03.2021' &gt;= T0.&quot;RefDate&quot;  
      GROUP BY T1.&quot;AcctCode&quot;,T1.&quot;AcctName&quot;
  
  UNION ALL
  
  SELECT 
      'Database3' As DataDource,T1.&quot;AcctCode&quot;,T1.&quot;AcctName&quot;, SUM(T0.&quot;Debit&quot;) - SUM(T0.&quot;Credit&quot;) as TotalBal
      FROM Db3.Table1 T0  
          INNER JOIN Db3.Table2 T1 ON T0.&quot;Account&quot; = T1.&quot;AcctCode&quot; 
          INNER JOIN Db3.Table3 T2 ON T0.&quot;TransId&quot; = T2.&quot;TransId&quot;
      WHERE 
          T1.&quot;AcctCode&quot; in
          (
          '105004001','105005001','105006001','105007001', '105008001'
          )      
          AND '01.01.0001' &lt;= '01.07.2020'  
          AND '31.03.2021' &gt;= T0.&quot;RefDate&quot;  
      GROUP BY T1.&quot;AcctCode&quot;,T1.&quot;AcctName&quot;
  </code></pre>
  <p>So, my question is if it's possible to mention the AcctCode globally i.e. AcctCode would be same for all and all the sub queries will be using the same AcctCode...</p>