title: Max match same numbers from each row
body: <p>To generate 1mln rows of report with the below mentioned script is taking almost 2 days so, really appreciate if somebody could help me with different script which the report can be generated within 10-15mins please.</p>
  <p>The requirement of the report is as following;</p>
  <p>Table “cover” contains 5mln rows &amp; 6 columns of data and likewise table “data” contains 500,000 rows and 6 columns.</p>
  <p>So, each numbers of the rows in table cover has to go through table date and provide the maximum matches.</p>
  <p>For instance, as mentioned on the below tables, there could be 3 matches in row #1, 2 matches in row #2 and 5 matches in row #3 so the script has to select the max selection which is 5 in row #3.</p>
  <p><a href="https://i.stack.imgur.com/2ki2W.png" rel="nofollow noreferrer">Sample table</a></p>
  <pre><code>UPDATE public.cover_sheet AS fc 
    SET maxmatch = (SELECT MAX(tmp.mtch) 
                    FROM (
                      SELECT (SELECT CASE WHEN fc.a=drwo.a THEN 1 ELSE 0 END) + 
                             (SELECT CASE WHEN fc.b=drwo.b THEN 1 ELSE 0 END) + 
                             (SELECT CASE WHEN fc.c=drwo.c THEN 1 ELSE 0 END) + 
                             (SELECT CASE WHEN fc.d=drwo.d THEN 1 ELSE 0 END) +
                             (SELECT CASE WHEN fc.e=drwo.e THEN 1 ELSE 0 END) +
                             (SELECT CASE WHEN fc.f=drwo.f THEN 1 ELSE 0 END) AS mtch 
                      FROM public.data AS drwo
                    ) AS tmp) 
  WHERE fc.code&gt;0;
  
  SELECT * 
  FROM public.cover_sheet AS fc 
  WHERE fc.maxmatch&gt;0;
  </code></pre>