Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>SQL is easier to understand inside out.</p> <blockquote> <p>The problem is to identify the percentage of times that 'Attribute6' is populated for the set of rows when there are more than 1 value for 'Attribute5' when 'Attibute1' to 'Attribute4' were the same. </p> </blockquote> <p>Breaks down like this:</p> <ol> <li>Where attributes 1-4 are the same</li> <li>And there is more than one value for attribute5</li> <li>Give the percentage of times Attribute6 is populated</li> </ol> <p>Like so:</p> <pre><code>select attribute1, attribute2, attribute3, attribute4, -- 3. give percentage of times Attribute6 is populated -- Percentage is numerator * 100 over denominator -- 3.a. Numerator: Number of times attribute 6 is populated sum( case when attribute6 is null then 0 else 1 end) * 100 / -- 3.b. Denominator: Total number of attribute5 found count(attribute5) from Problem p -- 1. where attributes 1-4 are the same group by attribute1, attribute2, attribute3, attribute4 -- 2. And there is more than one value for attribute5 having count(distinct attribute5) &gt; 1 </code></pre> <p>You haven't been clear on the definiton of "attribute5 has more than one value" - I have assumed you mean more than one distinct value. If you just meant "not null" that is easy too - just replace the count(distinct) with the appropriate expression to get what you want.</p> <h3>Edit</h3> <p>With the added clarity that we are looking for a single number, which is the percentage of groups where there are multiple distinct values of Attribute5, which also have multiple values of attribute6.</p> <p>It's not clear how you want to handle nulls and empty strings, so I am assuming that there are no nulls and empty strings count as a normal value.</p> <p>Try the following:</p> <pre><code>select sum(nDistinct5) as nDemoninator, sum(nDistinct6) as nNumerator, sum(nDistinct6) * 100.0 / sum(nDistinct5) from ( select attribute1, attribute2, attribute3, attribute4, -- 3. give percentage of times Attribute6 is populated -- Percentage is numerator * 100 over denominator -- 3.a. Numerator: Number of times attribute 6 is populated count(distinct attribute6) as nDistinct6, -- 3.b. Denominator: Total number of attribute5 found sum(1) as nDistinct5 from Problem p -- 1. where attributes 1-4 are the same group by attribute1, attribute2, attribute3, attribute4 -- 2. And there is more than one value for attribute5 having count(distinct attribute5) &gt; 1 ) g </code></pre> <p>For eyeballing purposes, join the original data onto the subquery g so you can manually confirm the logic is correct.</p> <pre><code>select p.*, g.nDistinct6, g.nDistinct5 from ( select attribute1, attribute2, attribute3, attribute4, -- 3. give percentage of times Attribute6 is populated -- Percentage is numerator * 100 over denominator -- 3.a. Numerator: Number of times attribute 6 is populated count(distinct attribute6) as nDistinct6, -- 3.b. Denominator: Total number of attribute5 found sum(1) as nDistinct5 from Problem p -- 1. where attributes 1-4 are the same group by attribute1, attribute2, attribute3, attribute4 -- 2. And there is more than one value for attribute5 having count(distinct attribute5) &gt; 1 ) g right outer join Problem p on p.attribute1 = g.attribute1 and p.attribute2 = g.attribute2 and p.attribute3 = g.attribute3 and p.attribute4 = g.attribute4 order by p.attribute1, p.attribute2, p.attribute3, p.attribute4 </code></pre> <p>This displayes every row from <code>Problem</code>, and the corresponding group totals for number of distinct <code>Attribute6</code> and <code>Attribute5</code>, so you can validate that these are indeed the numbers you want to use. If there are too many rows and you just want to eyeball a few hundred, you can use <code>top</code></p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload