I hava following sql query
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
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
Update
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
I am afread that you haven’t seen my last comment
Ok i simulate your table, i make this smart query (i guess 🙂 ):