SQL Query question

Hi Experts,
I have the following query embedded in a store procedure
SELECT ''Active'' AS Category, COUNT(*) AS [Total #]
						FROM dbo.View_EmpStatisticsEmployeesTbl
						WHERE ' + @strFilter +
					  '	And EmployeeID in(
						Select EmployeeID from TovInfo where FirstDay is not null)

Open in new window

Now I need to add a column next to the count, the third column should display the percentage of column #2 out of the total (total meaning only including the condition of the strFilter, but not the second condition).

What is the most efficient way to accomplish it?
LVL 5
bfuchsAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Scott PletcherSenior DBACommented:
The most efficient way would be to compute both values in a single SELECT, like this:

SELECT ''Active'' AS Category, COUNT(ti.EmployeeID) AS [Total #],
    ISNULL(CAST(COUNT(ti.EmployeeID) * 100.0 / NULLIF(COUNT(ese.EmployeeID), 0) AS decimal(4, 1)), 0) AS Percentage
                                    FROM dbo.View_EmpStatisticsEmployeesTbl ese
                                    LEFT OUTER JOIN TovInfo ON ti where
                                        ti.EmployeeID = ese.EmployeeID and
                                        ti.FirstDay is not null
                                    WHERE ' + @strFilter
                                                --+ ' HAVING COUNT(ti.EmployeeID) > 0 ' --uncomment if you only want to list ese rows that find at least one matching employeeID in TovInfo
0
bfuchsAuthor Commented:
@Scott,
Trying the below in SSMS

SELECT 'Active' AS Category, COUNT(ti.EmployeeID) AS [Total #],
    ISNULL(CAST(COUNT(ti.EmployeeID) * 100.0 / NULLIF(COUNT(ese.EmployeeID), 0) AS decimal(4, 1)), 0) AS Percentage
                                    FROM dbo.View_EmpStatisticsEmployeesTbl ese
                                    LEFT OUTER JOIN TovInfo ON ti where 
                                        ti.EmployeeID = ese.EmployeeID and
                                        ti.FirstDay is not null
                                    WHERE State = 'nj'
                                    HAVING COUNT(ti.EmployeeID) > 0 

Open in new window


and getting the following error message
Msg 4145, Level 15, State 1, Line 4
An expression of non-boolean type specified in a context where a condition is expected, near 'where'.
0
Scott PletcherSenior DBACommented:
The "where" in the join is a typo left over from copy/paste, so we need to remove it:

SELECT 'Active' AS Category, COUNT(ti.EmployeeID) AS [Total #],
    ISNULL(CAST(COUNT(ti.EmployeeID) * 100.0 / NULLIF(COUNT(ese.EmployeeID), 0) AS decimal(4, 1)), 0) AS Percentage
                                    FROM dbo.View_EmpStatisticsEmployeesTbl ese
                                    LEFT OUTER JOIN TovInfo ti ON
                                        ti.EmployeeID = ese.EmployeeID and
                                        ti.FirstDay is not null
                                    WHERE State = 'nj'
                                    HAVING COUNT(ti.EmployeeID) > 0
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Ultimate Tool Kit for Technology Solution Provider

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy now.

bfuchsAuthor Commented:
Excellent!!
0
bfuchsAuthor Commented:
Hi Scott,
Perhaps you can also solve the following?
http://www.experts-exchange.com/questions/28671817/SQL-Query-question.html
0
bfuchsAuthor Commented:
Hi Scott,

Please advise what to do as is the same question just on a diff query.
http://www.experts-exchange.com/questions/28671817/SQL-Query-question.html#a40774198

(It does not seem like the expert there will ever finish resolving this issue..)

Thanks,
Ben
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft SQL Server

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.