oke lanjut, misalkan perusahaan kita mempunyai jumlah karyawan 1000 orang,dan kita hanya mempunyai informasi data sebagai berikut :
table karyawan :
Nah bagaimana penyelesaianya di SQL Server ?
Nih saya berikan contohnya untuk kasus diatas :
Pertama buat Query untuk mengetahui umur dari setiap karyawan berdasarkan tanggal lahir :
select nama_karyawan,tgllahir,datediff(year,tgllahir,GETDATE()) umur, case
when datediff(year,tgllahir,GETDATE()) between 18 and 35 then '[18-35]'
when datediff(year,tgllahir,GETDATE()) between 36 and 40 then '[36-40]'
when datediff(year,tgllahir,GETDATE()) between 41 and 45 then '[41-45]'
when datediff(year,tgllahir,GETDATE()) between 46 and 50 then '[46-50]'
when datediff(year,tgllahir,GETDATE()) between 51 and 55 then '[51-55]'
when datediff(year,tgllahir,GETDATE()) between 56 and 60 then '[56-60]'
when datediff(year,tgllahir,GETDATE()) between 61 and 65 then '[61-65]'
when datediff(year,tgllahir,GETDATE()) between 61 and 65 then '[66-69]'
end range_umur
FROM tbl_karyawan A
Maka hasilnya akan seperti ini
dari Query tersebut kita modifikasi sedikit seperti ini :
select range_umur,count(*) As Jumlah from (select NAMA_KARYAWAN,tgllahir,datediff(year,tgllahir,GETDATE()) umur, case
when datediff(year,tgllahir,GETDATE()) between 18 and 35 then '[18-35]'
when datediff(year,tgllahir,GETDATE()) between 36 and 40 then '[36-40]'
when datediff(year,tgllahir,GETDATE()) between 41 and 45 then '[41-45]'
when datediff(year,tgllahir,GETDATE()) between 46 and 50 then '[46-50]'
when datediff(year,tgllahir,GETDATE()) between 51 and 55 then '[51-55]'
when datediff(year,tgllahir,GETDATE()) between 56 and 60 then '[56-60]'
when datediff(year,tgllahir,GETDATE()) between 61 and 65 then '[61-65]'
when datediff(year,tgllahir,GETDATE()) between 61 and 65 then '[66-69]'
end range_umur
FROM tbl_karyawan A ) x
group by range_umur
Sehingga hasilnya seperti ini :
Gimana, mudah bukan ?
MEmunculkan dalam tabel laporan bagaimana gan?
ReplyDeleteMEmunculkan dalam tabel laporan bagaimana gan?
Delete