Quantcast
Channel: Transact-SQL forum
Viewing all articles
Browse latest Browse all 23857

Group by clause based on condition

$
0
0

tblScore contains score for each problem

id     problemID      score
------------------------
1          1             10
2          2             30

tblSubmission contains problem submissions for each user

id     user         problemID      accepted
-----------------------------------------------
1     UserA             1                 0
2     UserA             1                 0
3     UserA             1                 1
4     UserA             2                 1
5     UserB             1                 0
6     UserB             1                 1
7     UserB             2                 1

Explanation  :
For UserA :
 -  For problemID 1
      ->  submitted three times
      ->  rejected for first two submission and accepted on third submission.
 - For problemID 2
      -> submitted one time
      -> accepted on first submission

For UserB :
 -  For problemID 1
      ->  submitted two times
      ->  rejected for first submission and accepted on second submission.
 - For problemID 2
      -> submitted one time
      -> accepted on first submission

Now I would like to process the table and want to get the following result :

user              Score
--------------------------
UserA             36 (6 + 30)
UserB             38 (8 + 30)

Explanation :
- For each rejected submission, a -2 point penalty.
- UserA have submitted probelmID 1
  - > score of problemID 1 is 10.
  - > first two times rejectd
  - > third time accepted.
  -> score = 10 - 4 = 6
- UserA have submitted problemID 2
  - > score of problemID 2 is 30
  - > first time accepted. No penalty will be counted
  - > score = 30

so final score for UserA = 30 + 6 = 36
Similar for UserB.

I know all I am doing is explanations. I have tried a lot with gorup by and other stuffs. May be my lack of knowledge on SQL is the problem.

I would really appreciate if someone help me on this.

Thank You


Viewing all articles
Browse latest Browse all 23857

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>