Different queries but return values in the same table

Asked

Viewed 161 times

0

I have two different queries, so I have two variables with the result of each query, but I wanted to show everything in the same table, as I do in php?

$sql = "SELECT Product , SUM(Amount) AS 'Total 2017' FROM dbcentro.Registo WHERE YEAR(RegistrationDate) = YEAR(NOW()) GROUP BY Product;";
$sql1 = "SELECT Produto, CAST(Total/3 AS DECIMAL(15,1)) AS 'Média 1º Trimestre' FROM(SELECT Product AS Produto, SUM(Amount) AS Total FROM dbcentro.Registo WHERE MONTH(RegistrationDate) BETWEEN 1 AND 3 AND YEAR(RegistrationDate) = YEAR(NOW()) GROUP BY Product) Soma;";

$result = mysqli_query($conn, $sql);
$result1 = mysqli_query($conn, $sql1);

What I intended was for the Average column 1 quarter to appear here the result in the table as shown in the image below: inserir a descrição da imagem aqui

The first two columns are from the first query and now I want the result of the second query to appear in that column, but I still can’t get it.

I solved the problem by creating a while inside another while

  • 1

    Put in the answer a piece of code of how you’re doing it. Even with mistakes. So we can help you

  • If both queries return the same amount of columns I believe that Union would solve, or even the same query could fetch everything, but post the code you have for each query today to make it easier for us to help.

  • puts the two queries that have the same result

  • I don’t know is wrong in your code or just here but you put $result = mysqli_query($conn, $sql); twice (I believe that one of the two should be with the $sql1)

  • I have no errors in the code and I don’t want to join the two queries, what I want is to show the columns I want from one query and the other in the same table

  • What I intended was to notice in while how I show the product column and total 2017 of the first consultation and the column Average 1st Quarter of the second consultation within the same table that I create

  • I would suggest that you supplement your question with those comments, and try to clarify a little more so that we can try to help.

  • If you decided to post the solution in the place indicated for such

Show 3 more comments

2 answers

3


Based on the SQL that Walmir passed you works! and also da para fazer utilizando "IF":

SELECT 
    Product,
    SUM(Amount) AS 'Total 2017',
    CAST(SUM(if(MONTH(RegistrationDate) BETWEEN 1 AND 3 , Amount, 0)) / 3  AS DECIMAL (15 , 1 )) AS 'Média 1º Trimestre'
FROM
    dbcentro.Registo
WHERE
    YEAR(RegistrationDate) = YEAR(NOW())
GROUP BY Product;
  • Everaldo coast, I need to average for every quarter of this year, but for example in the quarter we meet, I wanted to divide only by the months we have this semester and not just by three, but I wanted to put it automatic, you can help me?

  • In Where put to ignore the current month, example: WHERE YEAR(Registrationdate) = YEAR(NOW()) AND MONTH(Registrationdate) != MONTH(())

  • And to calculate according to month quantity within quarter: SELECT Product, SUM(Amount) AS 'Total 2017', CAST(SUM(if(MONTH(Registrationdate) BETWEEN 1 AND 3 , Amount, 0)) / Count(MONTH(Registrationdate)) AS DECIMAL (15 , 1 )) AS 'Average 1st Quarter',

  • This example is not working, because it is not just dividing by three months, I think it is dividing by the months of 2017 all, which are 7 months

  • Check if you put this to count the right amount of months: Count(distinct if(MONTH(Registrationdate) BETWEEN 1 AND 3 , MONTH(Registrationdate), null))

  • 1

    It worked, thank you

Show 1 more comment

1

Try to put the 1st Quarter Average Calculation in just a select:

SELECT 
    Product , 
    SUM(Amount) AS 'Total 2017',
    CAST(SUM(case when MONTH(RegistrationDate) BETWEEN 1 AND 3  then Amount else 0 end)/3 as DECIMAL(15,1)) as 'Média 1º Trimestre'
    FROM dbcentro.Registo 
    WHERE YEAR(RegistrationDate) = YEAR(NOW()) 
        GROUP BY Product;
  • i do not want the union of the two queries, what I intend is to show after the columns I want of each query in the same table in the visual part of php

  • You need to call $result and $result1 via another function, https://secure.php.net/manual/en/mysqli-result.fetch-array.php . Normalize the AS name of your query without spaces and special characters. Exchange spaces for underline.

  • I edited my previous answer. Please see if it solves your problem...

  • Yes, this solution is better than the one I arrived at that was to put one while inside another, but now I needed to calculate the all quarters of the year 2017 but to divide in an automatic way, because for example now in this quarter that we are not yet can divide by 3 months and yes by two, but if it is manual is not the right way. I don’t know if I made myself clear

  • 1

    Test this function TIMESTAMPDIFF(MONTH, Registrationdate, NOW())

  • Can you set an example, please?

Show 1 more comment

Browser other questions tagged

You are not signed in. Login or sign up in order to post.