How can I Find in MS-SQL table values that is max on 3 columns, plus max on a single column.
I know to look at the three columns to get them, and I know to do a self join to get the single value - but how can I combine the two.
Normally when I add a new value to this I have all the other data and as I add new ones I have all the values handy. But for this special case I don't have this. I have the survey_id values and that is it. I need to find the next question ID for that survey, then find the last position in the survey. They may not be the same thing.
I'll but doing this is vb.net, not that it makes any difference.
I need to find for each survey_id the highest question_id AND the highest chapter, subchapter, question_number - that is chapter 2, sub 1, question 1 is greater than chapter 1, sub 99, question 99.
given a table that looks like this (survey_id and question_id form unique pair)
survey_id | question_id | chapter | subchapter | question_number
================================================================
505 | 1 | 1 | 1 | 1
505 | 2 | 1 | 1 | 3
505 | 3 | 1 | 1 | 2
5858 | 1 | 1 | 1 | 1
5858 | 2 | 1 | 1 | 2
5858 | 3 | 1 | 1 | 2
5858 | 47 | 1 | 1 | 4
5858 | 45 | 2 | 1 | 1
5858 | 46 | 2 | 1 | 2
6060 | 1 | 1 | 1 | 1
6060 | 2 | 1 | 1 | 2
6060 | 3 | 1 | 1 | 2
6060 | 47 | 1 | 1 | 4
6060 | 45 | 2 | 1 | 1
6060 | 46 | 2 | 1 | 2
My result should be
survey_id | suveyMAXquestion_id | Maxchapter | Maxsubchapter | Maxquestion_number
=================================================================================
505 | 2 | 1 | 1 | 3
5858 | 47 | 2 | 1 | 2
6060 | 47 | 2 | 1 | 2
What I will end up doing is putting a new value into the table with survey_id, question_id +1 and chapter,subchapter, question_number+1
My data the will be insert in the table will (after updating the other columns in table that I have not shown) be:
survey_id | question_id | chapter | subchapter | question_number
=================================================================
505 | 3 | 1 | 1 | 4
5858 | 48 | 2 | 1 | 3
6060 | 48 | 2 | 1 | 3