title: Postgres - Count() based on multiple columns
body: <p>I have a table : <code>device</code></p>
  
  <pre><code>----------------------------------------------------------------
  | device__some_id   |   device__os_name  |  device__os_version |
   ---------------------------------------------------------------
  |       0                   android               3.0            |
  
  |       0                   android               3.0            |
  
  |       0                   android               4.0            |
  
  |       1                   ios                   8.0            |
  
  |________________________________________________________________|
  </code></pre>
  
  <p>I have a query which returns <code>count(*)</code> for each tenant,os_name, os_version combination. 
   output :</p>
  
  <pre><code>--------------------------------------------------------------------------
  | device__some_id   |   device__os_name  |  device__os_version | Count |
   -------------------------------------------------------------------------
          0                    android               3.0               2
          0                    android               4.0               1
          1                    ios                   8.0               1
  </code></pre>
  
  <p>Query used :</p>
  
  <pre><code>SELECT f,p,q,count (*)
  FROM (SELECT  device__some_id as f, device__os_name as p, device__os_version as q FROM device) as foo
  GROUP BY f,p,q
  </code></pre>
  
  <p>Issue : </p>
  
  <p>I have another table dim_os which has the following fields :</p>
  
  <pre><code> | dim_os__some_id   |   dim_os__os_type  |  dim_os__os_version |
       ------------------------------------------------------------
              1                 android               3.0               
              2                 android               4.0              
              3                 ios                   8.0          
  </code></pre>
  
  <p>WHat I want to do is just replace each occurance of <code>device_os_name</code> and <code>device_os_version</code> in the output of first query by the <code>some_id</code> from the <code>dim_os</code> table.</p>
  
  <p>My try : </p>
  
  <pre><code>SELECT f, p, count (*)
  FROM (SELECT  dev.device__some_id as f, j2.dim_os__some_id as p  FROM device AS dev 
  INNER JOIN dim_os as j1 ON dev.device__os_name = j1.dim_os__os_type 
  INNER JOIN dim_os as j2 ON dev.device__os_version = j2.dim_os__os_version) as foo
  GROUP BY f,p ;      
  </code></pre>
  
  <p>WHich doesn't give me proper counts. Where am I going wrong?</p>