-- Function untuk menghitung rata-rata IPK mahasiswa aktif
CREATE OR REPLACE FUNCTION get_ipk_rata_aktif()
RETURNS TABLE(rata_rata_ipk_aktif NUMERIC) AS $$
BEGIN
  RETURN QUERY
  SELECT ROUND(AVG(m.ipk), 2) AS rata_rata_ipk_aktif
  FROM status_mahasiswa sm
  JOIN mahasiswa m ON sm.nim = m.nim
  WHERE sm.status_kuliah = 'Aktif' AND m.ipk IS NOT NULL;
END;
$$ LANGUAGE plpgsql;

-- Function untuk menghitung rata-rata IPK mahasiswa lulus
CREATE OR REPLACE FUNCTION get_ipk_rata_lulus()
RETURNS TABLE(rata_rata_ipk_lulus NUMERIC) AS $$
BEGIN
  RETURN QUERY
  SELECT ROUND(AVG(m.ipk), 2) AS rata_rata_ipk_lulus
  FROM status_mahasiswa sm
  JOIN mahasiswa m ON sm.nim = m.nim
  WHERE sm.status_kuliah = 'Lulus' AND m.ipk IS NOT NULL;
END;
$$ LANGUAGE plpgsql; 