Запрос сводной таблицы MySQL с несколькими значениями динамических столбцов - PullRequest
0 голосов
/ 27 ноября 2018

Я хочу создать запрос для отображения отчета о трудозатратах с часами и столбцами Jam_Borongan_Hasil и Jumlah_Hasil.

Мои данные выглядят так:

SELECT * FROM `form_employee`

результат:

IDForm  IDEmployee
1809/J010592055/392452/100-80   J010594000
1809/J010592055/392452/100-80   J010593000
1810/J0000_tes1/397272/100-80   J010592055
...

.

SELECT * FROM `form

результат:

IDForm  IDEmployee  OrderNumber     IDBusiness_Unit     User_App    TanggalPeriode  DateUpdated     TimeUpdated
1810/J0000_tes1/397272/100-80   J0000_tes1  397272  11001-30-80     OVIE    2018-10-02  0000-00-00  00:00:00
1810/J0000_tes1/397276/100-80   J0000_tes1  397276  11001-30-80     admin   2018-10-02  0000-00-00  00:00:00
1810/J010593000/395269/100-80   J010593000  395269  11001-30-80     admin   2018-10-02  0000-00-00  00:00:00
...

.

SELECT * FROM `master_employee`

результат:

IDEmployee  Nama    TTL     BusinessUnit    Koordinator     UserID  DateCreated     TimeCreated     DateUpdated     TimeUpdated
021118 JJJJJJJTEST  TEST021118  TRENGGALEK, 09 NOPEMBER 2018    11007   0   OVIE    2018-11-02  13:20:09    0000-00-00  00:00:00
J100151018  WINARTI (MADIUN)    MADIUN, 4 JUNI 1983     11001   0   OVIE    2018-10-09  15:04:40    NULL    NULL
J100169001  AGUS RIYAT MUDI     BANYUWANGI, 9 MEI 1972  11001   0   OVIE    2018-10-09  15:04:40    NULL    NULL
...

.

SELECT * FROM `master_wo`

результат:

OrderNumber     OrderType   SO  SOType  Line    ParentWO    Description     BusinessUnit    Company     WOStatus    ShortItemNo     2ndItemNumber   QuantityOrdered     UM  OrderDate   LastBackup
378735  WO  18112204    SO  1.000   378735  BANK CENTRAL ASIA   11001   11000   45  26575   B0021010901     215920  BK  2018-05-02  2018-11-27 07:53:00
381085  WO  18112204    SO  1.000   378735  TINTA   11001   11000   45  37514   B0021010909     215920  BK  2018-05-02  2018-11-27 07:53:00
381086  WO  18112204    SO  1.000   378735  CEK NON PERSO PLY 1     11001   11000   45  27374   B0021010902     215920  BK  2018-05-02  2018-11-27 07:53:00
...

.

SELECT * FROM `master_business_unit`

результат:

IDBusiness_Unit     Description     Pekerjaan   Proses  Keterangan  AddressNumber   UserID  DateCreated     TimeCreated     DateUpdated     TimeUpdated     Status
11001-30-02     Dimuken 01              11001   OVIE    2018-10-09  14:42:51    NULL    NULL    Active
11001-30-03     Encoder                 11001   OVIE    2018-10-09  14:42:51    NULL    NULL    Active
11001-30-04     Epson               11001   OVIE    2018-10-09  14:42:51    NULL    NULL    Active
...

.

SELECT * FROM `form_productivity`

результат:

IDForm  IDEmployee  Tanggal     Shift   Jam_Borongan_Hasil  Jumlah_Hasil    Jam_Borongan_Jam    Status_ACC
1809/J010592055/392452/100-80   J010594000  2018-09-21      NULL        NULL    0
1809/J010592055/392452/100-80   J010594000  2018-09-22      NULL        NULL    0
1809/J010592055/392452/100-80   J010594000  2018-09-23      NULL        NULL    0
...

Я пробовал этот запрос, но все же мне нужно снова зациклить его в интерфейсе

SELECT fe.IDForm, fe.IDEmployee, me.Nama, mw.OrderNumber, mw.OrderType, bu.IDBusiness_Unit, mw.Description, mw.QuantityOrdered, mw.UM, fp.Tanggal, fp.Shift, fp.Jam_Borongan_Hasil, fp.Jumlah_Hasil, fp.Status_ACC, mw.BusinessUnit AS Branch
FROM `db_produktivitas`.`form_employee` fe
JOIN `db_produktivitas`.`form` f ON fe.IDForm = f.IDForm
JOIN `db_produktivitas`.`master_employee` me ON fe.IDEmployee = me.IDEmployee
JOIN `db_produktivitas`.`master_wo` mw ON f.OrderNumber = mw.OrderNumber
JOIN `db_produktivitas`.`master_business_unit` bu ON f.IDBusiness_Unit = bu.IDBusiness_Unit
JOIN `db_produktivitas`.`form_productivity` fp ON fe.IDEmployee = fp.IDEmployee
AND fe.IDForm = fp.IDForm
WHERE fp.Tanggal
BETWEEN '2018-10-11'
AND (
'2018-10-11' + INTERVAL 31 DAY
)
AND fp.Status_ACC = '3'
ORDER BY mw.Description ASC,  me.Nama asc, fp.Tanggal asc

Мне нужно отобразить вот так, динамические столбцы

отчет

Я пробовал сводный запрос

SET @sql = NULL;
SELECT
  GROUP_CONCAT(DISTINCT
    CONCAT(
      'MAX(IF(fp.Tanggal = ''',
      Tanggal,
      ''', fp.Jumlah_Hasil, NULL)) AS ',
      Tanggal
    )
  ) INTO @sql
FROM `db_produktivitas`.`form_productivity`
WHERE IDForm in('1810/J444444441/394224/101145','1811/J444444441/407976/11001-30-54');

SET @sql = CONCAT("SELECT fe.IDForm, fe.IDEmployee, me.Nama, mw.OrderNumber, mw.OrderType, bu.IDBusiness_Unit, mw.Description, mw.QuantityOrdered, mw.UM, ', @sql, ' 
    FROM `db_produktivitas`.`form_employee` fe
    JOIN `db_produktivitas`.`form` f ON fe.IDForm = f.IDForm
    JOIN `db_produktivitas`.`master_employee` me ON fe.IDEmployee = me.IDEmployee
    JOIN `db_produktivitas`.`master_wo` mw ON f.OrderNumber = mw.OrderNumber
    JOIN `db_produktivitas`.`master_business_unit` bu ON f.IDBusiness_Unit = bu.IDBusiness_Unit
    JOIN `db_produktivitas`.`form_productivity` fp ON fe.IDEmployee = fp.IDEmployee
    AND fe.IDForm = fp.IDForm
    WHERE fe.IDForm in('1810/J444444441/394224/101145','1811/J444444441/407976/11001-30-54')
    AND fp.Status_ACC = '3'
    GROUP BY fe.IDForm, fe.IDEmployee");

PREPARE stmt FROM @sql;
EXECUTE stmt;

Показывает

SET @sql = NULL;# MySQL returned an empty result set (i.e. zero rows).
...