Hi PRA,
Please refer below code
SQL
DECLARE @INFO AS TABLE(Id INT,FirstName VARCHAR(30), Name VARCHAR(30),Birthday DateTime,Jeans CHAR(6),Profession VARCHAR(15))
INSERT INTO @INFO VALUES(1,'Pulodov','Rustam','12.22.2000','male','pupil')
INSERT INTO @INFO VALUES(2,'Asrori','Yatim','02.06.2000','male','pupil')
INSERT INTO @INFO VALUES(3,'Sharipov','Sadriddin','12.08.1997','male','students')
INSERT INTO @INFO VALUES(4,'Soliev','Firuz','06.16.1989','male','worker')
INSERT INTO @INFO VALUES(5,'Malaev','Nurullo','03.18.1989','male','worker')
INSERT INTO @INFO VALUES(6,'Sayfurov','Karomatullo','05.07.1989','male','worker')
INSERT INTO @INFO VALUES(7,'Safarov','Muhiddin','03.18.1997','male','students')
INSERT INTO @INFO VALUES(8,'Safarova','Sabrina','10.18.1989','male','worker')
INSERT INTO @INFO VALUES(9,'Safarova','Munira','12.12.2000','female','pupil')
INSERT INTO @INFO VALUES(10,'Nazarova','Suman','11.11.1989','female','worker')
INSERT INTO @INFO VALUES(11,'Gaforova','Madina','12.12.1997','female','Students')
INSERT INTO @INFO VALUES(12,'Safarova','Munisa','12.12.2001','female','pupil')
INSERT INTO @INFO VALUES(13,'Safarov','Sarabek','12.12.2001','male','pupil')
SELECT CONVERT(VARCHAR,CONVERT(INT,DATEDIFF(YEAR,BIRTHDAY,GETDATE()))) AS Years
,COUNT( Id) AS [All]
,COUNT(CASE UPPER(JEANS) WHEN 'MALE' THEN 1 END)Male
,COUNT(CASE UPPER(JEANS) WHEN 'FEMALE' THEN 1 END) Female
,COUNT(CASE UPPER(PROFESSION) WHEN 'PUPIL' THEN 1 END) Pupil
,COUNT(CASE UPPER(PROFESSION) WHEN 'STUDENTS' THEN 1 END) Students
,COUNT(CASE UPPER(PROFESSION) WHEN 'WORKER' THEN 1 END)Worker
INTO #Result
FROM @INFO
GROUP BY CONVERT(INT,DATEDIFF(YEAR,BIRTHDAY,GETDATE()))
ORDER BY CONVERT(INT,DATEDIFF(YEAR,BIRTHDAY,GETDATE()))
SELECT * FROM #Result
UNION ALL
SELECT 'ALL'
,SUM([All])
,SUM(Male)
,SUM(Female)
,SUM(Pupil)
,SUM(Students)
,SUM(Worker)
FROM #Result
DROP TABLE #Result
Output
Years |
All |
Male |
Female |
Pupil |
Students |
Worker |
15 |
2 |
1 |
1 |
2 |
0 |
0 |
16 |
3 |
2 |
1 |
3 |
0 |
0 |
19 |
3 |
2 |
1 |
0 |
3 |
0 |
27 |
5 |
4 |
1 |
0 |
0 |
5 |
ALL |
13 |
9 |
4 |
5 |
3 |
5 |