SQL SERVER GROUP BY -> is used to aggregate data.
For ex: Aggregate data on List of Orders generated in a perticular Day,Month,Year etc.,
or List of Employees in each department.
etc.,
Group By Year
select Datepart(year,dob) "Year",count(*) "Count" from [profiledb].[dbo].Person
group by Datepart(year,dob)
Group By Year and Then By Month
select Datepart(year,dob) "Year",
DateName(month,dob) "Month",
count(*) "Count" from
[profiledb].[dbo].Person
group by Datepart(year,dob),
DateName(month,dob)
OUTPUT
Note: Groups Data on Year and then groups by month.
Tags:SQL SERVER GROUP BY on DATETIME COLUMN,SQL Server Group By DatePart,
DatePart year,Group By on Year,Group by on Month, Group By year,month.
For ex: Aggregate data on List of Orders generated in a perticular Day,Month,Year etc.,
or List of Employees in each department.
etc.,
Group By on DateTime Column
Person Table following Data.
| id | Name | Date of Birth |
|---|---|---|
| 1 | Peter | 1997-12-12 00:00:00.000 |
| 2 | Daniel | 2000-01-12 00:00:00.000 |
| 2 | shyam purru | 2000-01-12 00:00:00.000 |
| 3 | Jhon | 2002-11-01 00:00:00.000 |
| 4 | Jhony | 1960-08-30 00:00:00.000 |
| 5 | pearson | 2013-04-21 00:19:32.257 |
Group By Year
select Datepart(year,dob) "Year",count(*) "Count" from [profiledb].[dbo].Person
group by Datepart(year,dob)
| Year | Count | |
|---|---|---|
| 1960 | 1 | |
| 1997 | 1 | |
| 2000 | 2 | |
| 2002 | 1 | |
| 2013 | 1 |
Group By Year and Then By Month
select Datepart(year,dob) "Year",
DateName(month,dob) "Month",
count(*) "Count" from
[profiledb].[dbo].Person
group by Datepart(year,dob),
DateName(month,dob)
OUTPUT
| Year | Month | Count | |
|---|---|---|---|
| 2013 | April | 1 | |
| 1960 | August | 1 | |
| 1997 | December | 1 | |
| 2000 | January | 2 | |
| 2002 | November | 1 |
Note: Groups Data on Year and then groups by month.
Tags:SQL SERVER GROUP BY on DATETIME COLUMN,SQL Server Group By DatePart,
DatePart year,Group By on Year,Group by on Month, Group By year,month.
No comments:
Post a Comment