Click here to Skip to main content
15,886,199 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi All
This post may seem repetitive to one of my previous questions.But I thought to post freshly. Following is the query:-
SQL
SELECT
    JOB_NAME,
    SUM(CASE Status WHEN 'Failure' THEN 1 ELSE 0 END) As Fail_Count,
    COUNT(JOB_NAME) As OverallCount
FROM
    COMP_HIS_TBL
GROUP BY
    JOB_NAME

I need to add an additional field called "FailPercent" based on the formula :(Fail_Count/Overall_Count)*100 in the result.

What I have tried:

I tried the following queries but its throwing error:(
SQL
SELECT
    JOB_NAME,
    SUM(CASE Status WHEN 'Failure' THEN 1 ELSE 0 END) As Fail_Count,
    COUNT(JOB_NAME) As OverallCount,
	(Fail_Count/OverallCount)*100 as Fail_Percent
FROM
    COMP_HIS_TBL
GROUP BY
    JOB_NAME)

2)
SQL
Select JOB_NAME,T1.Fail_Percent  from
(SELECT
    JOB_NAME,
    SUM(CASE Status WHEN 'Failure' THEN 1 ELSE 0 END) As Fail_Count,
    COUNT(JOB_NAME) As OverallCount,
	(Fail_Count/OverallCount)*100 as Fail_Percent
FROM
    COMP_HIS_TBL
GROUP BY
    JOB_NAME)as T1

3)
SQL
Select JOB_NAME,(Count(Fail_Count)/Count(OverallCount))*100 as Fail_percent,STATUS from COMP_HIS_TBL 
where Fail_Count,OverallCount in (SELECT
JOB_NAME,
SUM(CASE Status WHEN 'Failure' THEN 1 ELSE 0 END) As Fail_Count,
COUNT(JOB_NAME) As OverallCount
FROM
COMP_HIS_TBL
GROUP BY
JOB_NAME) 

Please provide a solution to this:(
Thank you
Posted
Updated 10-May-16 3:11am
v5
Comments
ZurdoDev 10-May-16 9:03am    
What's the error?
mousau 10-May-16 9:11am    
Hi Ryan
For the 1st query the error is :-
Msg 102, Level 15, State 1, Line 9
Incorrect syntax near ')'

2nd query:-

Msg 207, Level 16, State 1, Line 7
Invalid column name 'Fail_Count'.
Msg 207, Level 16, State 1, Line 7
Invalid column name 'OverallCount'.

3rd Query:-
Msg 4145, Level 15, State 1, Line 2
An expression of non-boolean type specified in a context where a condition is expected, near ','.

ZurdoDev 10-May-16 9:18am    
So, stick with what you think is right and fix the syntax errors.
mousau 10-May-16 9:32am    
Hi Ryan
Thanks for your time

1 solution

You need to read up on SQL's Windows Functions - How to Use Microsoft SQL Server 2012's Window Functions, Part 1 | SQL Server 2012 content from SQL Server Pro[^]

Try this query to see if it meets your requirements:
SQL
SELECT
    JOB_NAME, [Status],
    COUNT(*) OVER (PARTITION BY [Status]) As Fail_Count,
    COUNT(JOB_NAME) OVER() As OverallCount,
    100.0 * COUNT(*) OVER (PARTITION BY [Status]) / COUNT(JOB_NAME) OVER() as FailPercent

FROM
    COMP_HIS_TBL
If you just want to look at the failures then use it as a sub-query
SQL
SELECT * FROM
(
	SELECT
		JOB_NAME, [Status],
		COUNT(*) OVER (PARTITION BY [Status]) As Fail_Count,
		COUNT(JOB_NAME) OVER() As OverallCount,
		100.0 * COUNT(*) OVER (PARTITION BY [Status]) / COUNT(JOB_NAME) OVER() as FailPercent

	FROM
		COMP_HIS_TBL
) src
WHERE [Status] = 'Failure'
 
Share this answer
 
Comments
mousau 10-May-16 9:31am    
Hi CHill60
Thanks a lot for the solution.It worked.Thanks again
Thank you
CHill60 10-May-16 9:37am    
No problem!

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900