Click here to Skip to main content
15,898,035 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi All
i have the following queries which needs to be joined on JOB_NAME key field:-
Query1:-


SQL
Select JOB_NAME,STATUS,Count(JOB_NAME) as Fail_Count from COMP_HIS_TBL A where STATUS = 'Failure' Group By JOB_NAME,STATUS


Query2:-

SQL
Select JOB_NAME,STATUS,Count(JOB_NAME) as OverallCount from COMP_HIS_TBL 
Group By JOB_NAME,STATUS


Both Fail_Count and OverallCount are the custom added fields.Need to join the above two tables,used the following join statement:-

What I have tried:

SQL
Select A.JOB_NAME,A.STATUS,Count(A.JOB_NAME) as Fail_Count from COMP_HIS_TBL A  JOIN
(Select JOB_NAME,STATUS,Count(JOB_NAME) as OverallCount from COMP_HIS_TBL 
Group By JOB_NAME,STATUS) B
On 
A.JOB_NAME=B.JOB_NAME
Where A.STATUS = 'Failure'
Group By A.JOB_NAME,A.STATUS 



However the OverallCount is missing from my output?Need help regarding this:(
Posted
Updated 9-May-16 4:14am
v2
Comments
F-ES Sitecore 9-May-16 10:02am    
Consult on-line documentation for SELECT syntax. You do your table joins first and then your group by statements. You don't group by on a join

select fields from table1
join table2 on ...
join table3 on ...
where field1 = somevalue
group by field1, field2

https://msdn.microsoft.com/en-us/library/ms189499.aspx
mousau 9-May-16 10:17am    
Hi
Thanks a lot.I saw and modified the syntax acccordinlgy.
Thank you

1 solution

In this case, you don't even need a JOIN:
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
;
 
Share this answer
 
Comments
mousau 9-May-16 10:16am    
Hi Richard
Thanks a lot for your solution.Really thanks.Its working. After this I tried using the following query but its showing incorrect syntax:(

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)

Moumita

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