Click here to Skip to main content
15,885,876 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Given below is the database table(TableA).

HTML
SlNo  TeamName  WorkInfo SubmitDate
----  -------   -------  ---------
 1     Team1        0      10/10/2014
 2     Team1        0      10/10/2014
 3     Team1        1      11/10/2014
 4     Team2        1      12/15/2014
 5     Team2        1      10/10/2014
 6     Team3        0      10/10/2014
 7     Team3        1      10/10/2014


I have two text boxes for selecting start and end date. I want to display details in between that dates.
Given below code is used to find the date difference between start date and end date and displayed on another text box named as "`txtTotalDays`".

Code:

C#
DateTime d1 = txtStartDate.Text != string.Empty ? Convert.ToDateTime(txtStartDate.Text) : DateTime.MinValue;
DateTime d2 = txtEndDate.Text != string.Empty ? 
Convert.ToDateTime(txtEndDate.Text) : DateTime.MinValue; 
TimeSpan tspan = d2 - d1;
txtTotalDays.Text = tspan.TotalDays.ToString()


Given below query is giving the details and count based on the start date and end date.

SQL Query


SQL
SELECT TeamName,Count(DISTINCT CASE WHEN WorkInfo = 1 THEN SlNo end) AS Count1 , Count(DISTINCT CASE WHEN WorkInfo = 0 THEN SlNo end) AS Count0 FROM tableA
WHERE (SubmitDate BETWEEN @start AND @end) GROUP BY TeamName


The above query shows the out put like given below:

HTML
TeamName   Count1   Count0
--------   -----    ------
 Team1        1        2
 Team2        2        0
 Team3        1        1


I want to display the percentage based on the `count and date difference` . Help me to modify the above solution. `The expecting output sample is given below:`

I selected start date and end date. I got the date difference (txtTotalDays.Text) as 10. Based on that date selection I want get values like given below.

HTML
TeamName   Count1   Count0  Percentage1  Percentage0
--------   -----    ------  -----------  -----------
Team1        1        2        10%           20%
Team2        2        0        20%            0%
Team3        1        1        10%           10%



Help me to find a proper solution. Thank You.
Posted

Modify the query with below.

SQL
select t.TeamName,t.Count1,t.Count0,(t.Count1/(daysDifference * 100) as Percentage1,
(t.Count0/(daysDifference * 100) as Percentage0 from 
(SELECT TeamName,Count(DISTINCT CASE WHEN WorkInfo = 1 THEN SlNo end) AS Count1 , Count(DISTINCT CASE WHEN WorkInfo = 0 THEN SlNo end) AS Count0 FROM tableA
WHERE (SubmitDate BETWEEN @start AND @end) GROUP BY TeamName) as t


where daysDifference is your c# variable.
 
Share this answer
 
SQL
SELECT 
  TeamName,
  Count(DISTINCT CASE WHEN WorkInfo = 1 THEN SlNo end) AS Count1, 
  Count(DISTINCT CASE WHEN WorkInfo = 0 THEN SlNo end) AS Count0,
  Count(DISTINCT CASE WHEN WorkInfo = 1 THEN SlNo end) * 100.0 / (DATEDIFF(Day, @start, @end) + 1) AS Percentage1,
  Count(DISTINCT CASE WHEN WorkInfo = 0 THEN SlNo end) * 100.0 / (DATEDIFF(Day, @start, @end) + 1) AS Percentage0
FROM 
  tableA 
WHERE 
  (SubmitDate BETWEEN @start AND @end) 
GROUP BY 
  TeamName
 
Share this answer
 
Comments
Praveen Kumar Upadhyay 6-Jan-15 5:25am    
My solutions has optimized query. In your query
DISTINCT CASE WHEN WorkInfo = 1 THEN SlNo end
and
DISTINCT CASE WHEN WorkInfo = 0 THEN SlNo end
will exeucte two times. If your table size is huge then finding DISTINCT result multiple time will slow down your query and performance also.

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