DECLARE
	@nip VARCHAR(6) = 'H02933',
	@kd_unit_org VARCHAR(20) = '',
	@dari DATE = '2019-12-01',
	@sampai DATE = '2019-12-28'

SELECT
	*
FROM
	SPG_REPORT_ABSENSI AS sra INNER JOIN
	SPG_DATA_CURRENT AS sdc ON sra.NIP = sdc.NIP
WHERE
	sra.NIP = @nip
	AND CAST(CAST(sra.TANGGAL_ABSEN AS VARCHAR(8)) AS DATE) >= @dari
	AND CAST(CAST(sra.TANGGAL_ABSEN AS VARCHAR(8)) AS DATE) <= @sampai
ORDER BY
	sra.TANGGAL_ABSEN

SELECT * FROM SPG_JNS_ABSENSI 
--WHERE ACTIVE = 1 
ORDER BY ORDER_BY

IF OBJECT_ID(N'tempdb..##hasil') IS NOT NULL
BEGIN
DROP TABLE ##hasil
END
GO

DECLARE
	@qry_create_tmp VARCHAR(max) = 'CREATE TABLE ##hasil  ( NIP VARCHAR(8), ';
SELECT @qry_create_tmp = @qry_create_tmp + Stuff(
  (SELECT ' SMALLINT, ' + KD_ABSENSI FROM SPG_JNS_ABSENSI WHERE ACTIVE = 1 AND KD_ABSENSI <> 'LBR' GROUP BY KD_ABSENSI ORDER BY MIN(ORDER_BY) FOR XML PATH(''),TYPE)
  .value('text()[1]','varchar(max)'),1,11,N'')
SET @qry_create_tmp = @qry_create_tmp + ' SMALLINT )';
PRINT @qry_create_tmp 
EXEC(@qry_create_tmp)

EXEC('SELECT * FROM ##hasil ')

CREATE TABLE #hasil  ( NIP VARCHAR(8), K SMALLINT, A SMALLINT, BAK SMALLINT, CB SMALLINT, CM SMALLINT, CMP SMALLINT, CS SMALLINT, CT SMALLINT, CX SMALLINT, CKIK SMALLINT, D SMALLINT, DP SMALLINT, DS SMALLINT, L SMALLINT, PN SMALLINT, PS SMALLINT, PT SMALLINT, S SMALLINT, SD SMALLINT, LL SMALLINT, AM SMALLINT, KM SMALLINT, CLT SMALLINT, CBR SMALLINT, DSL SMALLINT, TP SMALLINT, MNT_C SMALLINT, MNT_L SMALLINT )


DECLARE @sql2 nvarchar(1000)
SET @sql2 = 'UPDATE #hasil SET eligible_' + @eligible_kd_gol + ' = @eligible_jml WHERE kd_bidang = @eligible_kd_bid'
EXEC sp_executesql @sql2, N'@eligible_jml SMALLINT, @eligible_kd_bid VARCHAR(3)', @eligible_jml = @eligible_jml, @eligible_kd_bid = @eligible_kd_bid


	CREATE TABLE #hasil  (
						kd_bidang VARCHAR(3) NULL,
						bidang VARCHAR(100) NULL,
						lowjab_90 SMALLINT NULL,
						eligible_90 SMALLINT NULL,
						selisih_90 SMALLINT NULL,
						lowjab_80 SMALLINT NULL,
						eligible_80 SMALLINT NULL,
						selisih_80 SMALLINT NULL,
						lowjab_70 SMALLINT NULL,
						eligible_70 SMALLINT NULL,
						selisih_70 SMALLINT NULL,
						lowjab_60 SMALLINT NULL,
						eligible_60 SMALLINT NULL,
						selisih_60 SMALLINT NULL,
						lowjab_50 SMALLINT NULL,
						eligible_50 SMALLINT NULL,
						selisih_50 SMALLINT NULL,
						lowjab_40 SMALLINT NULL,
						eligible_40 SMALLINT NULL,
						selisih_40 SMALLINT NULL,
						lowjab_30 SMALLINT NULL,
						eligible_30 SMALLINT NULL,
						selisih_30 SMALLINT NULL
					)