title: Presto SQL - distinct in selective group by
body: <pre><code>ID  Name     Gender  Country 
  1   Arun     Male    India
  2   Akshay   Male    England
  3   Chethna  Female  India
  4   Priya    Female  China
  5   Piyush   Male    India
  6   Arun     Male    England
  7   Tisha    Female  England
  8   Chethna  Female  China
  </code></pre>
  
  <p>I want to group them into male/female first, then the country associated.</p>
  
  <pre><code>Query1 : select Gender, count(distinct name) from Table group by Gender
  
  Output: 
  Gender   count(distinct name)
  Male     3
  Female   3
  </code></pre>
  
  <p>Copying the result in JSON like this, 
  result : {male : {count : 3}, female : {count : 3} }</p>
  
  <pre><code>Query2 : select Gender, Country, count(distinct name) from Table group by Gender, Country
  
  Output:
  Gender  Country    count(distinct name)
  Male    India      2
  Male    England    2
  Female  India      1
  Female  China      2
  Female  England    1
  </code></pre>
  
  <p>Adding this result in the above Json,</p>
  
  <p>result : {Male:{count:3,India:{count:2},England:{count:2}},Female:{count:3,India:{count:1},China:{count:2},England:{count:1}}}</p>
  
  <p>So can I achieve this in a single query?</p>