|
AVG(TIMEDIFF()) почему то не работает
|
|||
---|---|---|---|
#18+
То ли я дурак, то ли лыжи не едут...конечно первое. Есть такой запрос, который дает верный результат в плане TIMEDIFF (на картинке) SELECT advertisement_id, users.username AS никнэйм, TIMEDIFF(trades.created_at, advertisements.created_at) AS принятие_сделки, TIMEDIFF(trades.paid_created, trades.created_at) AS оплата_сделки, TIMEDIFF(trades.updated_at, trades.paid_created) AS заключение_сделки FROM users LEFT JOIN advertisements ON users.id = advertisements.user_id LEFT JOIN trades ON advertisements.id = trades.advertisement_id WHERE trades.status = 'closed' ORDER BY 2 но мне надо высчитать среднее время по всем трем пунктам, я добавил AVG и group by SELECT advertisement_id, users.username AS никнэйм, AVG(TIMEDIFF(trades.created_at, advertisements.created_at)) AS принятие_сделки, AVG(TIMEDIFF(trades.paid_created, trades.created_at)) AS оплата_сделки, AVG(TIMEDIFF(trades.updated_at, trades.paid_created)) AS заключение_сделки FROM users LEFT JOIN advertisements ON users.id = advertisements.user_id LEFT JOIN trades ON advertisements.id = trades.advertisement_id WHERE trades.status = 'closed' GROUP BY 2 ORDER BY 2 но результат совсем не тот, не понимаю, что я делаю не так ... |
|||
:
Нравится:
Не нравится:
|
|||
16.11.2021, 16:10 |
|
AVG(TIMEDIFF()) почему то не работает
|
|||
---|---|---|---|
#18+
Вот то, что я получаю при добавлении AVG ... |
|||
:
Нравится:
Не нравится:
|
|||
16.11.2021, 16:14 |
|
AVG(TIMEDIFF()) почему то не работает
|
|||
---|---|---|---|
#18+
Время на самом деле хранится в формате Float и AVG float и вернула. Чтобы опять получить время надо сделать соответствующий каст. ... |
|||
:
Нравится:
Не нравится:
|
|||
16.11.2021, 17:07 |
|
AVG(TIMEDIFF()) почему то не работает
|
|||
---|---|---|---|
#18+
Не все так просто: https://dev.mysql.com/doc/refman/8.0/en/aggregate-functions.html The SUM() and AVG() aggregate functions do not work with temporal values. (They convert the values to numbers, losing everything after the first nonnumeric character.) To work around this problem, convert to numeric units, perform the aggregate operation, and convert back to a temporal value. Examples: SELECT SEC_TO_TIME(SUM(TIME_TO_SEC(time_col))) FROM tbl_name; SELECT FROM_DAYS(SUM(TO_DAYS(date_col))) FROM tbl_name; ... |
|||
:
Нравится:
Не нравится:
|
|||
16.11.2021, 23:03 |
|
AVG(TIMEDIFF()) почему то не работает
|
|||
---|---|---|---|
#18+
miksoft Не все так просто: https://dev.mysql.com/doc/refman/8.0/en/aggregate-functions.html The SUM() and AVG() aggregate functions do not work with temporal values. (They convert the values to numbers, losing everything after the first nonnumeric character.) To work around this problem, convert to numeric units, perform the aggregate operation, and convert back to a temporal value. Examples: SELECT SEC_TO_TIME(SUM(TIME_TO_SEC(time_col))) FROM tbl_name; SELECT FROM_DAYS(SUM(TO_DAYS(date_col))) FROM tbl_name; Премного благодарен. Заработало. А мне говорили, что достаточно освоить select, from, Where и считай знаешь sql 🤦🏻♂️ ... |
|||
:
Нравится:
Не нравится:
|
|||
17.11.2021, 01:00 |
|
|
start [/forum/topic.php?fid=47&fpage=4&tid=1827878]: |
0ms |
get settings: |
9ms |
get forum list: |
14ms |
check forum access: |
3ms |
check topic access: |
3ms |
track hit: |
37ms |
get topic data: |
11ms |
get forum data: |
2ms |
get page messages: |
43ms |
get tp. blocked users: |
2ms |
others: | 291ms |
total: | 415ms |
0 / 0 |