I have a table :
IADATA
Id Studentid Mon Value Type
1 ABC1 1 12 1
1 ABC1 1 02 2
1 ABC1 1 18 4
1 ABC1 1 09 7
1 ABC1 1 12 8
1 ABC1 1 22 10
1 ABC2 2 12 1
1 ABC2 2 02 2
1 ABC2 2 18 4
1 ABC2 2 09 7
1 ABC2 2 12 10
1 ABC2 3 05 1
1 ABC2 3 02 2
1 ABC2 3 20 4
1 ABC2 3 09 7
1 ABC2 3 12 10
In the above table we have two Students ABC1 and ABC2 and their data.
Now the kind of result I want is as follows:
Id Studentid Mon Obtained Benefits Max Type
1 ABC1 1 12 02 18 I
2 ABC1 1 09 12 22 II
3 ABC2 2 12 02 18 I
4 ABC2 2 09 00 12 II
5 ABC2 3 05 02 20 I
6 ABC2 3 09 00 12 II
Now in the result you can see that I am placing the values according to the studentid and mon and the types in a certain order. if the type is I it should have obtained, then benefits then max and same with II. What can be the select query for it?
The type in previous tables are linked. When it has the value 1,2,4 it should be like obtained, benefits and maximum for new type I and when 7,8,10, it should be like obtained,benefits and maximum for new type II. And when there is no column available, it should be zero.
This should do it:
EXAMPLE ON SQL FIDDLE
Although it would make more sense to store type separately. i.e. have one column for “obtained”, “max” etc and another column for “I”, “II”
EDIT
With your revised data structure this should work:
EXAMPLE ON SQL FIDDLE