O
O
Oleg Seledets2018-12-14 06:52:21
MySQL
Oleg Seledets, 2018-12-14 06:52:21

How to make such request in the temporary table?

As from such a table
5c1328ab8dcea540826701.png
It seems like I understand that you need to combine by name and year. but in calculating the amount and the difference between the current and the previous year, problems arise.
--------------------------------------
UPD1
did so far like this:

SELECT sudovlad, summa, years, (Sum(summa*100)/summa2) AS percent, summa2, years2 FROM (SELECT sudovlad, Sum(gruz_pass) AS summa, year(data_vih_p_ot) AS years, (Sum(gruz_pass)*100)/Sum(gruz_pass) AS percent, year(data_vih_p_ot) AS years2, Sum(gruz_pass) AS summa2 FROM pool_management.ships WHERE sudovlad = 'pere' GROUP by year(data_vih_p_ot) ) reservtable
WHERE years=2018

it turned out like this
5c15575d076a2178660818.png
But it is necessary that in the years2 field it is 2017, and in the summ2 field the sum of the values ​​​​for 2017

Answer the question

In order to leave comments, you need to log in

1 answer(s)
P
ponaehal, 2018-12-14
@ponaehal

This process is called transposition (see here ). It is done either by PIVOT/UNPIVOT expressions or by constructing a query like

select group_concat(if(v='a', c, null)) a, 
       group_concat(if(v='b', c, null)) b, 
       group_concat(if(v='c', c, null)) c
  from (select value v, Count(value) c
          from t1
        group by value ) temp

Well, maybe it's still possible...

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question