O
O
ozornick2015-12-29 12:25:13
MySQL
ozornick, 2015-12-29 12:25:13

How to set the desired grouping in MySQL?

Hello. I have no idea how to select.
Let's say there is a table tbl1 and tbl2. In the first, as it were, extended data, in the second, you need to save the summarized result.
That is, tbl1 contains statistics, for example, nick, time, summa, id:
vovka1 1451353490 112.6 239
Jaguar 1451353504 112.7 239
vovka1 1451353518 112.8 239 Jaguar
1451353533 112.9 239
To get something, nick, summa, id, count (the number of all entries in the nick and id fields):
Jaguar 1451353533 112.9 239 2
vovka1 1451353518 112.8 239 2

Answer the question

In order to leave comments, you need to log in

3 answer(s)
D
Dmitry Kovalsky, 2015-12-29
@ozornick

INSERT INTO tbl2(nick, summa, id, count )
SELECT nick,MAX(summa),id,COUNT(1) FROM tbl1
GROUP BY id,nick

Try SELECT first. If it works, do an insert. But if there is already some data in tbl2, then the request will need to be finalized.

A
Alexey Ukolov, 2015-12-29
@alexey-m-ukolov

select tbl1.nick tbl1.summa, tbl1.id, tmp2.cnt
from tbl1
inner join (
  select nick, max(summa) as summa from tbl1 group by nick
) as tmp using (nick, summa)
inner join (
  select nick, id, count(*) as cnt from tbl1 group by nick, id
) as tmp2 using (nick, id)

O
ozornick, 2015-12-29
@ozornick

"select nick, id, max(time) as lasttime, MAX(price) as lastprice, count(1) as count from tbl1 group by nick, id"

Thanks guys. Worked out as needed. It was important to understand the condition. Through PHP inserted lines.
Alexey Ukolov, too cluttered

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question