Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 SQL Server 2005 Forums
 Transact-SQL (2005)
 Performing Statistics in SELECT Statement

Author  Topic 

chunkydrew
Starting Member

3 Posts

Posted - 2008-05-30 : 13:59:53
I have listed below a sql statement generated by a MS Access query. The Access is the frontend, using a SQL Server 2005 View as the backend. I have already corrected the obvious differences between Access and SQL Server syntax, such as replacing UCase$ with UPPER,
replacing '_' with '.' between the db owner and view name, replacing IIF with IF, and replacing "D" with 'D' and "E" with 'E', but it still generates syntax errors (of course, with no explanation). As you can see, it is SUMing a field based on whether the value is 'D' or 'E', then using those calculated values to calculate a percentage. Can anyone out there let me know what I'm doing wrong?

SELECT dbo_vwDisplayUserList.DEPT_DESC, Sum(IIf(UCase$([Essential_Code])="D",1,0)) AS Department_Essential, Sum(IIf(UCase$([Essential_Code])="E",1,0)) AS EOC_Essential, Count(dbo_vwDisplayUserList.UserID) AS Total_Employees, Int([Department_Essential]/[Total_Employees]*100) AS [%Department_Essential], 100-Int([Department_Essential]/[Total_Employees]*100) AS [%EOC_Essential]
FROM dbo_vwDisplayUserList
GROUP BY dbo_vwDisplayUserList.DEPT_DESC
ORDER BY dbo_vwDisplayUserList.DEPT_DESC

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2008-05-30 : 14:05:19
May be this:-
SELECT dbo_vwDisplayUserList.DEPT_DESC, 
Sum(CASE WHEN UPPER([Essential_Code])="D" THEN 1 ELSE 0 END) AS Department_Essential,
Sum(CASE WHEN UPPER([Essential_Code])="E" THEN 1 ELSE 0 END) AS EOC_Essential,
Count(dbo_vwDisplayUserList.UserID) AS Total_Employees, [Department_Essential]*100/[Total_Employees] AS [%Department_Essential],
(100-[Department_Essential])*100/[Total_Employees] AS [%EOC_Essential]
FROM dbo_vwDisplayUserList
GROUP BY dbo_vwDisplayUserList.DEPT_DESC
ORDER BY dbo_vwDisplayUserList.DEPT_DESC
Go to Top of Page
   

- Advertisement -