Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>You have to repeat the whole case-statement in the group. You can't use the alias for that.</p> <p>If you are using SQL Server you can create a temporary named resultset using the <a href="http://msdn.microsoft.com/en-us/library/ms175972.aspx" rel="nofollow">WITH</a> clause and group by that.</p> <p>If not, you could alternatively create a View without the grouping and group that view by the relevant column.</p> <p>Version 1 (copy &amp; paste the computed column):</p> <pre><code>select hr_id, max(delivery_dt)as maxd, (CASE WHEN max(delivery_dt) &gt; '11-may-2010' then '&lt; 6 MO' WHEN max(delivery_dt) &gt; '11-may-2004' and max(delivery_dt) &lt; '11-may-2010' then '7 - 78 MO' WHEN max(delivery_dt) &gt; '11-nov-1999' and max(delivery_dt) &lt; '11-april-2004' then '79 - 132 MO' ELSE '&gt; 133 MO' END) D from INDIVIDUAL_VE_TB where hr_id in (select distinct hr_id from MODEL_SC_TB b where b.model_id='200') group by hr_id, (CASE WHEN max(delivery_dt) &gt; '11-may-2010' then '&lt; 6 MO' WHEN max(delivery_dt) &gt; '11-may-2004' and max(delivery_dt) &lt; '11-may-2010' then '7 - 78 MO' WHEN max(delivery_dt) &gt; '11-nov-1999' and max(delivery_dt) &lt; '11-april-2004' then '79 - 132 MO' ELSE '&gt; 133 MO' END) </code></pre> <p>Version 2 (WITH clause):</p> <pre><code>with temporary(hr_id, maxd, D) { select hr_id, max(delivery_dt)as maxd, (CASE WHEN max(delivery_dt) &gt; '11-may-2010' then '&lt; 6 MO' WHEN max(delivery_dt) &gt; '11-may-2004' and max(delivery_dt) &lt; '11-may-2010' then '7 - 78 MO' WHEN max(delivery_dt) &gt; '11-nov-1999' and max(delivery_dt) &lt; '11-april-2004' then '79 - 132 MO' ELSE '&gt; 133 MO' END) D from INDIVIDUAL_VE_TB where hr_id in (select distinct hr_id from MODEL_SC_TB b where b.model_id='200') group by hr_id } select hr_id, MAX(maxd) maxd, D from temporary group by hr_id, D </code></pre> <p>Version 3 is just version 2 in two parts.</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. This table or related slice is empty.
    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