Click here to Skip to main content
15,884,177 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
SQL
DECLARE @maxcount1 float 
DECLARE @maxcount0 float 
DECLARE @start datetime 
SET @start = getdate() - 2 
DECLARE @end datetime 
SET @end= getdate() + 2

SELECT @maxcount1 = max(OnTimeCount),@maxcount0 = max(DueTimeCount)

FROM  (SELECT DISTINCT COUNT(DISTINCT CASE WHEN ontime = 1 THEN sl_no  END)  AS OnTimeCount,  COUNT(DISTINCT CASE WHEN ontime = 0 THEN  sl_no END) AS DueTimeCount FROM tbl_splited_details 

WHERE (uploaded_date  BETWEEN  @start  AND @end) GROUP BY Site_ID) AS s

SELECT  DISTINCT Site_ID, OnTimeCount, DueTimeCount, 
round((cast((OnTimeCount*100) AS float)/@maxcount1), 2)AS percentage1 , round((cast((DueTimeCount *100)AS float)/@maxcount1),  2) AS percentage0

FROM (SELECT DISTINCT  Site_ID, COUNT(DISTINCT CASE  WHEN ontime  =   1  THEN  sl_no  END)  AS OnTimeCount, 
COUNT(DISTINCT CASE WHEN ontime  =  0 THEN sl_no END) AS  DueTimeCount  

FROM  tbl_splited_details  WHERE (uploaded_date BETWEEN  @start  AND    @end)   GROUP BY    Site_ID) AS s                                  
Posted
Comments
RedDk 18-Jan-15 18:57pm    
I doubt the title is correct.

Also, try adding the structure of the table "tbl_splited_details" with some sample data. Q&A helpers will respond to SQL questions more readily when they see they don't have to deal with it by creating it de novo.

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

  Print Answers RSS
Top Experts
Last 24hrsThis month


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