PostgreSQL syntax error at or near union on my query

35 Views Asked by At

Please help, I getting this error:

SQL Error [42601]: ERROR: each UNION query must have the same number of columns
  Position: 1090

This is my query :

SELECT ROW_NUMBER() OVER (ORDER BY ht.user_id) AS id, 
                ht.user_id AS user,
                COUNT(*) AS total_tickets,
                SUM(CASE WHEN ht.closed_date <= ht.sla_deadline THEN 1 ELSE 0 END) AS tickets_on_time,
                SUM(CASE WHEN ht.closed_date > ht.sla_deadline THEN 1 ELSE 0 END) AS tickets_late,
                (SUM(CASE WHEN ht.closed_date <= ht.sla_deadline THEN 1 ELSE 0 END)::float / COUNT(*)) * 100 AS value,
                (SUM(CASE WHEN ht.closed_date >= ht.sla_deadline THEN 1 ELSE 0 END)::float / COUNT(*)) * 100 AS value 
                FROM 
                helpdesk_ticket ht
            JOIN 
                helpdesk_ticket_stage hts ON ht.stage_id = hts.id
            JOIN 
                helpdesk_ticket_team htt ON ht.team_id = htt.id
            WHERE 
                LOWER(hts.name ->> 'en_US') IN ('closed', 'done') 
                AND ht.team_id IS NOT NULL 
                AND ht.sla_deadline IS NOT NULL
            GROUP BY
                ht.user_id
             union all
                SELECT 'performance_achieve' as type
                FROM helpdesk_sla_performances hsp
             union ALL
                SELECT 'performance_late' AS type
                FROM helpdesk_sla_performances hsp2 ;
            ORDER BY ht.user_id ;

thank u

I am using PostgreSQL. I can't figure out what the problem is. Any help would be appreciated.

union can work in this case

0

There are 0 best solutions below