Okay
  Public Ticket #2939534
SQL with UNION
Closed

Comments

  • Rene started the conversation

    Hi
    I would like to combine a query over 2 tables and add the same columns. I found a solution with UNION, unfortunately it does not work with WP Data Table. Is there an alternative? Here is the query:


    SELECT t.masch_nr,
           t.Monat,
           SUM(t.SOLLRüsten) AS SOLLRüsten,
           SUM(t.SOLLProduktion) AS SOLLProduktion,
           SUM(t.SOLLZuschlag) AS SOLLZuschlag,
           SUM(t.ISTRüsten) AS ISTRüsten,
           SUM(t.ISTProduktion) AS ISTProduktion
    FROM  
    (SELECT p.masch_nr
            ,SUM(b.ruest_zeit) as SOLLRüsten
            ,SUM(b.bearb_zeit) as SOLLProduktion
            ,SUM(b.ruest_zeit_zuschl) as SOLLZuschlag
            ,SUM(p.bmk_07) as ISTRüsten
            ,SUM(p.bmk_11) as ISTProduktion 
            ,MONTH(p.anmeld_dat) as Monat
    FROM [hydra1].[hydadm].[ade_protokoll] p
    JOIN [hydra1].[hydadm].[auftrags_bestand] b
      ON p.auftrag_nr = b.auftrag_nr
    WHERE p.masch_nr = 'GEORG'
         AND satz_art = 'E'
    GROUP BY p.masch_nr, MONTH(anmeld_dat)
    UNION 
    SELECT p.masch_nr
           ,SUM(b.ruest_zeit) as SOLLRüsten
           ,SUM(b.bearb_zeit) as SOLLProduktion
           ,SUM(b.ruest_zeit_zuschl) as SOLLZuschlag
           ,SUM(p.bmk_07) as ISTRüsten
           ,SUM(p.bmk_11) as ISTProduktion 
           ,MONTH(p.anmeld_dat) as Monat
    FROM [hydra1].[hydadm].[v_ade_protokoll] p
    JOIN [hydra1].[hydadm].[v_auftrags_bestand] b
      ON p.auftrag_nr = b.auftrag_nr
    WHERE p.masch_nr = 'GEORG'
          AND satz_art = 'E'
    GROUP BY p.masch_nr, MONTH(anmeld_dat)
    ) t
    GROUP BY t.masch_nr, t.Monat
    ORDER BY t.Monat


  •  2,572
    Aleksandar replied

    Hello Rene

    Our logic is based on a PHP SQL parser which has full support for the SQL dialect for the following statement types

    SELECT, INSERT, UPDATE, DELETE, REPLACE, RENAME, SHOW, SET, DROP, CREATE INDEX, CREATE TABLE, EXPLAIN and DESCRIBE.

    Some of them are disabled for security reasons.

    Filtering, sorting, and search may not work properly if you include:

    • Accent graves ( ` ) around the table name
    • JOIN functions
    • UNION functions
    • CONCAT functions
    • sub-queries

    You can try preparing a MySQL view (which will return the data that you need, call it e.g. “view1” and then build a wpDataTables based on a simple query like "SELECT * FROM view1″.

    Kind Regards, 

    Aleksandar Vuković
    [email protected]

    Rate my support

    wpDataTables: FAQ | Facebook | Twitter | InstagramFront-end and back-end demo | Docs

    Amelia: FAQ | Facebook | Twitter | InstagramAmelia demo sites | Docs | Discord Community

    You can try wpDataTables add-ons before purchasing on these sandbox sites:

    Powerful Filters | Gravity Forms Integration for wpDataTables | Formidable Forms Integration for wpDataTables | Master-Detail Tables