Query to get total by month

Last Post 10 Mar 2009 08:21 AM by nosepicker. 2 Replies.
AddThis - Bookmarking and Sharing Button Printer Friendly
  •  
  •  
  •  
  •  
  •  
Sort:
PrevPrev NextNext
You are not authorized to post a reply.
Author Messages
JASBRYDEN
New Member
New Member

--
09 Mar 2009 08:41 AM

Hi

I am trying to run a query where I would get the month and the total for each month

e.g.

2 15894.23
3 8825.20
4 87587.21

Below is the query that I am running but I am getting the following error

"Msg 145, Level 15, State 1, Line 2
ORDER BY items must appear in the select list if SELECT DISTINCT is specified."



use data1
select distinct month(transdatetime)as month,sum(sales)as total from btheader
where warehouseno='501'
group by transdatetime,sales,warehouseno
order by transdatetime,sales,warehouseno



What is it that I am doing wrong ?
JASBRYDEN
New Member
New Member

--
10 Mar 2009 07:18 AM
Thanks guys

It is working nice.
nosepicker
New Member
New Member

--
10 Mar 2009 08:21 AM
And just in case your data spans more than one year, you might want to select the year too:

select year(transdatetime) as year, month(transdatetime) as month, sum(sales)as total from btheader
where warehouseno='501'
group by year(transdatetime), month(transdatetime)
order by year, month
You are not authorized to post a reply.

Acceptable Use Policy