Note that there are some explanatory texts on larger screens.

plurals
  1. POuse group by in subquery
    primarykey
    data
    text
    <p>I hava following sql query</p> <pre><code>SELECT animal_code, ISNULL(SUM(calve_milk + morning + evening), 0) / 30 AS [Sep 2011], (SELECT ISNULL(SUM(calve_milk + morning + evening), 0) / 31 AS Expr1 FROM dbo.Status WHERE (m_date BETWEEN '10/1/2011' AND '10/31/2011')) AS [Oct 2011] FROM dbo.Status AS Status_1 WHERE (m_date BETWEEN '9/1/2011' AND '9/30/2011') GROUP BY animal_code ORDER BY animal_code </code></pre> <p>Through this query I have calculated a sum between a date range and named it as [Sep 2011] and now i have to calculate the [oct 2011] sum grouping it by animal code but i think the fixed column is only possible in subquery but in subquery i am unable to group the sum by animal code please any one help me out to get that sum group by animal_code in subquery</p> <p><strong>Update</strong></p> <pre><code>select a.animal_code,a.Sep2011,b.Oct2011 from (SELECT DISTINCT Images.animal_code, REPLACE(REPLACE(ROUND(ISNULL(SUM(Status.calve_milk + Status.morning + Status.evening), 0) / 30, 0), '.', ''), '0', '') Sep2011 FROM Images,Status,animal_Status where Images.animal_code=Status.animal_code and Images.status_id=animal_status.status_id and status.m_date between '9/1/2011' and '9/30/2011' and animal_status.status_id=8 group by animal_code) a, (SELECT DISTINCT Images.animal_code, REPLACE(REPLACE(ROUND(ISNULL(SUM(Status.calve_milk + Status.morning + Status.evening), 0) / 31, 0), '.', ''), '0', '') Oct2011 FROM Images,Status,animal_Status where Images.animal_code=Status.animal_code and Images.status_id=animal_status.status_id and status.m_date between '10/1/2011' and '10/31/2011' and animal_status.status_id=8 group by animal_code) b where a.animal_code=b.animal_code </code></pre>
    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.
 

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