I want to compare and count rows in the same datatabe.
myDataTable
hours_id(PK) | _person_id | _project_id | date_of_hour
1 | pe1 | pr1 | 13.10.2011
2 | pe22 | pr1 | 13.10.2011
3 | pe1 | pr1 | 15.10.2011 *
4 | pe1 | pr1 | 13.10.2011
5 | pe1 | pr1 | 13.10.2011
6 | pe22 | pr22 | 13.10.2011
7 | pe1 | pr22 | 15.10.2011 *
8 | pe1 | pr1 | 15.10.2011 *
9 | pe22 | pr22 | 13.10.2011
10 | pe22 | pr22 | 13.10.2011
11 | pe1 | pr1 | 16.10.2011 **
_project_id(GUID)
In my program I can choose _person_id_2 = pe1, date_of_begin = 13.10.2011 and date_of_end = 15.10.2011. I want to get this result:
pe1 pr1 13.10.2011 3 //(count 3 rows are the same)(ROWS 1,4,5)
pe1 pr1 15.10.2011 2 //(ROWS 3,8)
pe1 pr22 15.10.2011 1 //(ROW 7)
And when I choose _person_id_2 = pe22, date_of_begin = 13.10.2011 and date_of_end = 15.10.2011 I want to get this result:
pe22 pr1 13.10.2011 1 //(ROW 2)
pe22 pr22 13.10.2011 1 //(ROW 6)
pe22 pr22 15.10.2011 2 //(ROWS 9,10)
I’m using SQL Server 2008. Hope you understand what I’m trying to ask.
Do you need a group by clause + count aggregation function:
Notice: I understand that when you write ‘13.10.2011’ you are talking about a well formed date and date_of_hour is in date or datetime format. If is not the case, you should cast your strings and columns to a valid date.