PDA

View Full Version : SQL sever 2008, select multiple field and aggregate function


code L
04-27-2010, 07:17 AM
I have 2 tables DIRproject and DIRrequest.

In DIRproject i have: projectname, dateAdded, addedBy
In DIRrequest i have: projectname, NumOfDrawing

what i want is to populate a table of info with projectname, dateAdded, addedBy, NumOfRequest and totalqty.

below is my sql, i got an error that say "I am trying to include a variable that is not included in aggregate function".
how to solve this anyone! :confused:

SELECT DIRproject.projectname, DIRproject.dateAdded, DIRproject.addedBy, count(DIRrequest.projectname) as NumOfRequest, sum(DIRrequest.NumOfDrawing) as totalqty
FROM DIRproject INNER JOIN DIRrequest
ON(DIRproject.projectname=DIRrequest.projectname)
WHERE #now()# - DateRequested <= #pastDay#
GROUP BY DIRproject.projectname

accwebworks
05-20-2010, 05:23 PM
When working with aggregates you have to include columns that are not part of the aggregate in group by. In your case your "group by" should look like this

group by DIRproject.projectname, DIRproject.dateAdded, DIRproject.addedBy

Now, this will run and it may not give you records that you're after, but it will give you a starting point.