ctater
asked on
Microsoft access 2003 count distinct field with group by / sum
I am using access 2003 as the back end on a asp.net 2.0 project. I am using a grid view to display the data.
I am trying to get a count of the distinct client Ids who grouping by the service. Also sum direct hours, direct hours and grouping by month and year.
I am aware that Access isn't the ideal solution for web apps and also doesn't directly support count(distinct fieldname). However, I am restricted because I want non technical employees to be able if necessary be able to manipulate the data if the need should arise. I am aout to get grey hairs. I have tried different subqueries etc to pull the distinct ids but nothing seems to work; HELP!!!
the code follows:
SelectCommand = "select count(TblDailyHours.Client Id) as TotalClients, tblDailyHours.ServiceType, sum(tblDailyHours.HoursDir ect) as HoursDirect, sum(tblDailyHours.HoursInd irect) as HoursIndirect,
sum(tblDailyHours.HoursDir ect + tblDailyHours.HoursIndirec t) as tempTotalHours, month(tblDailyHours.HOSDat e) & '/' & year(tblDailyHours.HOSDate ) as tempMonthYear
from tblDailyHours
where HOSDate BETWEEN @StartDate AND @EndDate
group by TblDailyHours.ServiceType, month(tblDailyHours.HOSDat e) & '/' & year(TblDailyHours.HOSDate )
"
I am trying to get a count of the distinct client Ids who grouping by the service. Also sum direct hours, direct hours and grouping by month and year.
I am aware that Access isn't the ideal solution for web apps and also doesn't directly support count(distinct fieldname). However, I am restricted because I want non technical employees to be able if necessary be able to manipulate the data if the need should arise. I am aout to get grey hairs. I have tried different subqueries etc to pull the distinct ids but nothing seems to work; HELP!!!
the code follows:
SelectCommand = "select count(TblDailyHours.Client
sum(tblDailyHours.HoursDir
from tblDailyHours
where HOSDate BETWEEN @StartDate AND @EndDate
group by TblDailyHours.ServiceType,
"
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
I finally see the light. Thanks to your prompting I got it to finally work!!!!! I was just missing a small component.
Solution:
SelectCommand = "select count(tmpTblDailyHours.Cli entId) as TotalClients, tmptblDailyHours.tmpServic eType as ServiceType, sum(tmpTblDailyHours.Hours Direct) as HoursDirect, sum(tmpTblDailyHours.Hours Indirect) as HoursIndirect,
sum(tmpTblDailyHours.Hours Direct + tmpTblDailyHours.HoursIndi rect) as tempTotalHours, tmpTblDailyHours.tempMonth Year as tempMonthYear
from (select TblDailyHours.ClientId , tblDailyHours.ServiceType as tmpServiceType, sum(tblDailyHours.HoursDir ect) as HoursDirect, sum(tblDailyHours.HoursInd irect) as HoursIndirect,
sum(tblDailyHours.HoursDir ect + tblDailyHours.HoursIndirec t) as tempTotalHours, month(tblDailyHours.HOSDat e) & '/' & year(tblDailyHours.HOSDate ) as tempMonthYear
from tblDailyHours
where HOSDate BETWEEN @StartDate AND @EndDate
group by tblDailyHours.ClientId, TblDailyHours.ServiceType, month(tblDailyHours.HOSDat e) & '/' & year(TblDailyHours.HOSDate )
) tmpTblDailyHours
group by tmpTblDailyHours.tmpServic eType, tmpTblDailyHours.TempMonth Year
"
Solution:
SelectCommand = "select count(tmpTblDailyHours.Cli
sum(tmpTblDailyHours.Hours
from (select TblDailyHours.ClientId , tblDailyHours.ServiceType as tmpServiceType, sum(tblDailyHours.HoursDir
sum(tblDailyHours.HoursDir
from tblDailyHours
where HOSDate BETWEEN @StartDate AND @EndDate
group by tblDailyHours.ClientId, TblDailyHours.ServiceType,
) tmpTblDailyHours
group by tmpTblDailyHours.tmpServic
"
ASKER
Do you mean subquery?