How to add columns from different tables?

0

I'm trying to set up an SQL command for my virtual store that returns the total value of the request, an item table (requestitemaux), and an additional table for the items (requestitemadaux). I mounted a command, but the error is occurring in Mysql:

  

Every derived table must have its own alias.

Follow the SQL command:

SELECT 
    SUM(Total_P + Total_A) AS Total
FROM
    (SELECT 
        Pi.IdItem,
            SUM(Pi.Quantidade * Pi.ValorUnit) AS Total_P,
            0 AS Total_A
    FROM
        pedidoitemaux Pi
    GROUP BY Pi.IdItem UNION ALL SELECT 
        Pa.IdItem,
            0 AS Total_P,
            SUM(Pa.Quantidadead * Pa.Valorad) AS TotalA
    FROM
        pedidoitemadaux Pa
    GROUP BY Pa.IdItem)
    
asked by anonymous 20.11.2017 / 23:52

1 answer

1

By error message, you are only asking for an alias for the derived table:

SELECT 
    SUM(x.Total_P + x.Total_A) AS Total
FROM
    (SELECT 
        Pi.IdItem,
        SUM(Pi.Quantidade * Pi.ValorUnit) AS Total_P,
        0 AS Total_A
    FROM
        pedidoitemaux Pi
    GROUP BY Pi.IdItem 

    UNION ALL 

    SELECT 
       Pa.IdItem,
       0 AS Total_P,
       SUM(Pa.Quantidadead * Pa.Valorad) AS TotalA
    FROM
        pedidoitemadaux Pa
    GROUP BY Pa.IdItem) x
    
21.11.2017 / 00:13