ID M1 M2 M3 M4 DateM2 DateM3 DateM4
1 2 NULL NULL NULL NULL NULL NULL
2 NULL 2 1 1 11.11.2011 15.11.2011 20.11.2011
3 1 NULL NULL NULL NULL NULL NULL
4 2 3 1 2 11.12.2012 12.12.2012 13.12.2012
5 1 1 1 1 11.10.2012 12.10.2012 13.10.2012
6 2 2 2 2 12.9.2012 12.12.2012 1.2.2013
i want to produce the following table which will have the ID column and a new Column
ID M1+M2+M3+M4
1 2
2 4
3 1
4 8
5 4
6 8
the new column will be M1+M2+M3+M4 with the following rules
- M1 added always to the sum and if its null i set it to 0
- M2 is added to the sum if DateM2 wasnt NULL AND DATE1 < DateM2 < DATE2
- M3 is added to the sum if DateM3 wasnt NULL AND DATE1 < DateM3 < DATE2
- M4 is added to the sum if DateM4 wasnt NULL AND DATE1 < DateM4 < DATE2
DATE1 and DATE2 are variables specified in the query, now my problem with this i am not sure what to use for the performance i have tens of thausands of records, should i use select case, you are welcome to help the way you can!
Something like this: