To qroup queries day name we must use datename function in sql. This function gets two parameter. The first one is interval;for day names we should write dw, for month names we shoul write m or mm. The second parameter is the date parameter. We will write examples on library database.
TO DOWNLOAD THE SAMPLE LİBRARY DATABASE CLICK
Example 1 :
List the days of week and the student count born in that day;
1 2 3 |
Select DATENAME(dw,birthdate) as Days,count(*) as Count from students group by DATENAME(dw,birthdate) |
Result:
Example 2:
List the days of week and the taken book count in that day
1 2 3 |
Select DATENAME(dw,takenDate) as Days,count(*) as Count from borrows group by DATENAME(dw,takenDate) |
Example 3: Find the day of the most taken book by all students
1 2 3 4 |
Select top 1 DATENAME(dw,takenDate) as Days,count(*) as Count from borrows group by DATENAME(dw,takenDate) order by Count desc |
Result: