We have patients who are assigned insurance that changes on occasion. I'm trying to get the number of patients in all insurances by week, but of course when they change insurance they end up in two groups in that week. I want to default them to the insurance they were in the most. Since all weeks will have 7 days it would always be 6:1 or 5:2 OR 4:3.(Not sure how SQL server deals with the first and last week of the year. If January 1 falls on a Wednesday will Week 1 also contain some days in December? I don't think this will be an issue as our small facility is usually a ghost town between Christmas and New Years)The code below creates a table with two patients. One was assigned the same insurance all 7 days in the week and the other is split with 4 days in one insurance (hp_1) and 3 days in another (hp_2). What I would like to see is each patient having one row and any patients who are split would go to the insurance they spent the most time in that week. In this case, patient 1001 should end up being assigned to hp_1.I'm stumped as to a simple way to do this. Any help is appreciated.Gregdeclare @tbl_hp_assign TABLE ([patient_id] [int] NOT NULL,[hp_name] varchar(30) NOT NULL,[week_num] int NOT NULL)insert into @tbl_hp_assignvalues(1001, 'hp_1', 1)insert into @tbl_hp_assignvalues(1001, 'hp_1', 1)insert into @tbl_hp_assignvalues(1001, 'hp_1', 1)insert into @tbl_hp_assignvalues(1001, 'hp_1', 1)insert into @tbl_hp_assignvalues(1001, 'hp_2', 1)insert into @tbl_hp_assignvalues(1001, 'hp_2', 1)insert into @tbl_hp_assignvalues(1001, 'hp_2', 1)insert into @tbl_hp_assignvalues(1002, 'hp_1', 1)insert into @tbl_hp_assignvalues(1002, 'hp_1', 1)insert into @tbl_hp_assignvalues(1002, 'hp_1', 1)insert into @tbl_hp_assignvalues(1002, 'hp_1', 1)insert into @tbl_hp_assignvalues(1002, 'hp_1', 1)insert into @tbl_hp_assignvalues(1002, 'hp_1', 1)insert into @tbl_hp_assignvalues(1002, 'hp_1', 1)select [patient_id], [hp_name], COUNT(week_num) DayCount from @tbl_hp_assigngroup by [patient_id], [hp_name]order by [patient_id]