Click here to Skip to main content
15,895,557 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
----  -------   -------
1     Team1        0
2     Team1        0
3     Team1        1
4     Team2        1
5     Team2        1
6     Team2        1
7     Team3        0
8     Team3        1



I have two text boxes for selecting start and end date. I want to display details in between that dates.

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

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        3        0
 Team3        1        1


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

Here in `Count1` 3 is the greates value.Based on that I have to find the percentage of Count1 values. And in `Count0` 2 is the greatest value.Based on that I have to find the percentage of Count0 values.

HTML
TeamName   Count1   Count0  Percentage1  Percentage0
--------   -----    ------  -----------  -----------
Team1        1        2        33.33%         100%
Team2        3        0        100%           0%
Team3        1        1        33.33%         50%



Help me to find a proper solution. Thank You.
Posted
Comments
Member 11042100 6-Jan-15 2:24am    
I tried the given below query. But it shows one error. Error is : "Can't perform an aggregate function on an expression containing an aggregate or a sub query.

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 = 0 THEN SlNo end) * 100.0 / MAX(COUNT(DISTINCT CASE WHEN WorkInfo = 0 THEN SlNo end) + 1) AS Percentage1, COUNT(DISTINCT CASE WHEN ontime = 0 THEN sl_no END)* 100.0 / MAX(COUNT(DISTINCT CASE WHEN WorkInfo = 0 THEN SlNo end) + 1) AS Percentage0
FROM tableA
WHERE (SubmitDate BETWEEN @start AND @end) GROUP BY TeamName

1 solution

This is the same question as you have posted twice already:
How to get count and percentage using SQL Query?[^]
How get count for specific value[^]

And you have had good answers both times. So this time (and in future) start looking at the solutions you have and thinking about how they work and use that information to try and solve this yourself. We cannot and will not do your work for you each time you need a minor tweak: we are not here for that. And it's quicker for you in the long run to understand what you are doing than have to ask us each and every time!
 
Share this answer
 
Comments
Member 11042100 6-Jan-15 2:41am    
All question have some difference.
OriginalGriff 6-Jan-15 2:57am    
Yes - a trivial difference, which if you had understood what you had been told for the previous ones would have given you the solution. We are hear to help, and educate: not to do you work for you. We do expect you to be capable of thinking, and to extrapolate from the basics to the slightly more complex cases - that is what development is all about!

If we solve every minor change for you, you aren't learning anything: and you may as well send us your salary / qualification as you aren't earning it, we are! :laugh:
Member 11042100 6-Jan-15 3:26am    
I am just a beginner, that's why I made all these mistakes.Can you please give me a solution for this. I searched so much, but I can't find out a proper solution.
OriginalGriff 6-Jan-15 3:35am    
All the more reason to try it yourself! :laugh:
You learn best by doing, not reading - so look at the existing answers, and try to understand them. When you do, the solution to this should be obvious.

But just getting the solution gets you out of a "homework hole" but digs you a deeper one because the next question - and the next subject - is based on this one and expects you to understand what you are doing. If you don't, then you will get increasingly more confused - and getting your homework done is nothing if you don't understand it because you will fail the examinations (and thus the course).

I'm really not being nasty here: I'm looking to the longer term than just helping you out of a current (trivial) 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