Can we use aggregate functions within group by?

|
stef_kkk 2020-06-17 22:08:59
MasterZiv 2020-06-17 22:06:17
These are duplicates, you must manually provide guarantee that there is no duplicate data in the destination database, that is, it must be empty.

but how?( –overwrite-tables didn’t help (

MasterZiv 2020-06-17 22:10:13
stef_kkk 2020-06-17 22:08:59
but how?( –overwrite-tables didn’t help (

I don’t know this particular tool, possibly you use it wrong or you misunderstand the options.
re-check the manual.

MasterZiv 2020-06-17 22:14:00
stef_kkk 2020-06-17 19:44:07
Help, please, to understand what is going on:(
I’m using docker image percona:5.7
Have the next problem, I’m dumping db’s correctly:

#!/bin/bash

for db in mysql aff_profits_test
do
time
mydumper
–host=127.0.0.1
–user=root
–password=pass
–outputdir=/home/ec2-user/full/$db
–rows=10000
-G -E -R
–compress
–build-empty-files
–threads=1
–verbose=3
–compress-protocol
–database=$db
done
Dumping proccess was successful and without any errors

Then I tried to restore mysql and aff_profits_test db’s for example:

#!/bin/bash

for db in mysql aff_profits_test
do
time
myloader
–host=127.0.0.1
–user=root
–password=pass
–directory=/home/ec2-user/full/$db
–threads=1
–verbose=3
–queries-per-transaction=100
–compress-protocol
–overwrite-tables
–database=$db
done
During restoring, I’m getting some errors:

** Message: 16:30:03.145: Thread 1 restoring `mysql`.`columns_priv` part 0
** Message: 16:30:03.145: Thread 1 restoring `mysql`.`db` part 0
** Message: 16:30:03.146: Thread 1 restoring `mysql`.`event` part 0
** Message: 16:30:03.146: Thread 1 restoring `mysql`.`func` part 0
** Message: 16:30:03.146: Thread 1 restoring `mysql`.`ndb_binlog_index` part 0
** Message: 16:30:03.146: Thread 1 restoring `mysql`.`proc` part 0

** (myloader:22186): CRITICAL **: 16:30:03.151: Error restoring mysql.proc from file mysql.proc.sql.gz: Table ‘mysql.proc’ doesn’t exist
Creating table `mysql`.`plugin`
** Message: 16:30:02.994: Dropping table or view (if exists) `mysql`.`proc`
** Message: 16:30:02.995: Creating table `mysql`.`proc`

** (myloader:22186): CRITICAL **: 16:30:02.996: Error restoring mysql.proc from file mysql.proc-schema.sql.gz: Invalid default value for ‘modified’

When restoring my own db, get the next errors:

Error restoring aff_profits_test.default_rate_shave_history from file aff_profits_test.default_rate_shave_history.sql.gz: Duplicate entry ‘1’ for key ‘PRIMARY’
** Message: 12:50:08.363: Thread 1 restoring `aff_profits_test`.`department_countries` part 0
** Message: 12:50:08.363: Thread 1 restoring `aff_profits_test`.`department_rate_tables` part 0
** Message: 12:50:08.363: Thread 1 restoring `aff_profits_test`.`department_rates` part 0
** Message: 12:50:08.364: Thread 1 restoring `aff_profits_test`.`department_site_alias` part 0
** Message: 12:50:08.364: Thread 1 restoring `aff_profits_test`.`department_sites` part 0
** Message: 12:50:08.364: Thread 1 restoring `aff_profits_test`.`details` part 0

* (myloader:3844): CRITICAL *: 12:50:08.426: Error restoring aff_profits_test.details from file aff_profits_test.details.sql.gz: Duplicate entry ’59eef12677075′ for key ‘PRIMARY’
** Message: 12:50:08.426: Thread 1 restoring `aff_profits_test`.`emails` part 0

* (myloader:3844): CRITICAL *: 12:50:08.447: Error restoring aff_profits_test.emails from file aff_profits_test.emails.sql.gz: Duplicate entry ‘1’ for key ‘PRIMARY’
** Message: 12:50:08.447: Thread 1 restoring `aff_profits_test`.`emails_parts` part 0
** Message: 12:50:08.447: Thread 1 restoring `aff_profits_test`.`errors` part 0
** Message: 12:50:08.447: Thread 1 restoring `aff_profits_test`.`faq_items` part 0

This is this param:

–overwrite-tables, -o
Drop any existing tables when restoring schemas

probably, you don’t restore schema.
Anyway, the param is NOT about erasing data, but about dropping tables.

h6lla_h66la 2020-06-17 23:35:26
Download mysql and mongodb are confusing this noon i tried to downlosd mysql
h6lla_h66la 2020-06-17 23:36:01
For windows looks like i downloaded something else but still mysql i guess
adinath_kamode 2020-06-18 08:09:33
Team, question, Does PREPARE and EXECUTE triggers implicit commit?
MasterZiv 2020-06-18 09:05:03
adinath_kamode 2020-06-18 08:09:33
Team, question, Does PREPARE and EXECUTE triggers implicit commit?

Afaik, no.

Ajay 2020-06-18 21:28:48
suppose I’ve a table with columns: Emp_id, E_name, Dept, Salary

and i want to find the maximum salary branch wise.

Ajay 2020-06-18 21:28:49
Select salary from table_name group by Salary having salary = max(salary);

ps: Select salary from table_name group by dept having salary = max(salary);

Ajay 2020-06-18 21:29:02
Is this right?
button_idli 2020-06-18 22:06:40
Ajay 2020-06-18 21:28:49
Select salary from table_name group by Salary having salary = max(salary);

ps: Select salary from table_name group by dept having salary = max(salary);

Brother branchwise?

MasterZiv 2020-06-18 22:07:18
Ajay 2020-06-18 21:29:02
Is this right?

no

huiuneed 2020-06-19 01:30:51
select …, max(salary) from .. group by …
Ajay 2020-06-19 06:00:40
MasterZiv 2020-06-18 22:07:18
no

ps: I wrote it wrong. Is this correct ->Select salary from table_name group by dept having salary = max(salary);
Kindly point out why, Too.

brijeshJoshiii 2020-06-19 07:02:52
Ajay 2020-06-19 06:00:40
ps: I wrote it wrong. Is this correct ->Select salary from table_name group by dept having salary = max(salary);
Kindly point out why, Too.

I think cuz having clause can only be used to filter out the group…so in this case max(salary) is an aggregate function which works on whole the group and we can’t extract salary of a single row alone… So max(salary) can only be compared with something else like..>10000 etc

Ajay 2020-06-19 07:25:33
brijeshJoshiii 2020-06-19 07:02:52
I think cuz having clause can only be used to filter out the group…so in this case max(salary) is an aggregate function which works on whole the group and we can’t extract salary of a single row alone… So max(salary) can only be compared with something else like..>10000 etc

ok sorry, i wrote it wrong, is this right then Select salary from table_name group by dept having salary = max(salary);

Master Yoda 2020-06-19 07:33:55
Ajay 2020-06-19 07:25:33
ok sorry, i wrote it wrong, is this right then Select salary from table_name group by dept having salary = max(salary);

Why don’t you run this query and see the result?

Ajay 2020-06-19 07:35:27
Master Yoda 2020-06-19 07:33:55
Why don’t you run this query and see the result?

I want to know the reason behind not working.

brijeshJoshiii 2020-06-19 07:53:11
Ajay 2020-06-19 07:35:27
I want to know the reason behind not working.

Oh man I have written the reason….when using group by clause you can only use column as a whole in having clause . a single row can’t be used….like u have used salary

Ajay 2020-06-19 07:54:02
brijeshJoshiii 2020-06-19 07:53:11
Oh man I have written the reason….when using group by clause you can only use column as a whole in having clause . a single row can’t be used….like u have used salary

Didn’t get the meaning of using column as a whole. Can you elaborate this?

Ajay 2020-06-19 07:54:20
And also the single row thing that you are mentioning.
brijeshJoshiii 2020-06-19 07:56:01
Like when u r using salary=max(salary)…u wanna compare each salary column (or cell) with max(salary)
Ajay 2020-06-19 07:57:12
brijeshJoshiii 2020-06-19 07:56:01
Like when u r using salary=max(salary)…u wanna compare each salary column (or cell) with max(salary)

yeah. So salary here means each particular row salary within a group. Am I wrong and the max(salary) means maximum within each group.

brijeshJoshiii 2020-06-19 07:57:41
Ajay 2020-06-19 07:57:12
yeah. So salary here means each particular row salary within a group. Am I wrong and the max(salary) means maximum within each group.

Ya

Ajay 2020-06-19 07:58:00
brijeshJoshiii 2020-06-19 07:57:41
Ya

So, what’s wrong. Don’t they make sense?

MasterZiv 2020-06-19 09:03:58
Ajay 2020-06-19 06:00:40
ps: I wrote it wrong. Is this correct ->Select salary from table_name group by dept having salary = max(salary);
Kindly point out why, Too.

No, you can’t aggregate a field and is it without aggregation at the same time in a quiery

MasterZiv 2020-06-19 09:05:39
Ajay 2020-06-18 21:28:48
suppose I’ve a table with columns: Emp_id, E_name, Dept, Salary

and i want to find the maximum salary branch wise.

What at all should this mean,
“the maximum salary branch wise” ?

MasterZiv 2020-06-19 09:07:15
Ajay 2020-06-19 06:00:40
ps: I wrote it wrong. Is this correct ->Select salary from table_name group by dept having salary = max(salary);
Kindly point out why, Too.

Select dept, max(salary) as max_salary
from table_name
group by dept

Is this what you need?

MasterZiv 2020-06-19 09:07:42
brijeshJoshiii 2020-06-19 07:02:52
I think cuz having clause can only be used to filter out the group…so in this case max(salary) is an aggregate function which works on whole the group and we can’t extract salary of a single row alone… So max(salary) can only be compared with something else like..>10000 etc

This is correct

MasterZiv 2020-06-19 09:08:56
Ajay 2020-06-19 07:35:27
I want to know the reason behind not working.

The deep reason is you can’t write SQL queries, you sold learn it first.

MasterZiv 2020-06-19 09:13:10
Ajay 2020-06-19 07:54:02
Didn’t get the meaning of using column as a whole. Can you elaborate this?

Each field in a row is EITHER being aggregated OR used as group dimension. You cannot do both things at the same time. Either this or that.

mySQL actually allows this, but this is wrong to do so.

MasterZiv 2020-06-19 09:17:32
yeah, GROUP by dept will not sum salaries. it will leave only one entry (first, ordered by salary in dept (dept,salary)
MasterZiv 2020-06-19 09:17:32
I understand that till the order by dept, salary desc, it groups the table into departments and then sorts this resultant table by salaries of people. I wanted salary of only 1 person from each department.
MasterZiv 2020-06-19 09:17:32
other way is SELECT dept, MAX(salary) FROM … GROUP by DEPT… but im not sure.. need to try
MasterZiv 2020-06-19 09:17:32
here is double ORDER dept ASC, salary DESC
MasterZiv 2020-06-19 09:17:32
no, you will have first row
MasterZiv 2020-06-19 09:17:32
doesn’t your output give salary=10 for dept=1?
MasterZiv 2020-06-19 09:17:32
after group you will take maximum salary in each dept
MasterZiv 2020-06-19 09:17:32
now I want maximum salary for each branch
MasterZiv 2020-06-19 09:17:32
with order DEPT than SALARY

DEPT | SALARY
1 20
1 10
2 30
2 20

MasterZiv 2020-06-19 09:17:32
mmm.. but I can’t see how will that give maximum salary?
MasterZiv 2020-06-19 09:17:32
hmm i think
select dept,salary from … group by dept order by dept, salary DESC
MasterZiv 2020-06-19 09:17:32
But then i want max from each department
MasterZiv 2020-06-19 09:17:32
Select * from … order by salary DESC limit 1
MasterZiv 2020-06-19 09:17:32
suppose I’ve a table with columns: Emp_id, E_name, Dept, Salary

and i want to find the maximum salary branch wise.

Select salary from table_name group by Salary having salary = max(salary);

Is this right? Kindly point out the mistake !!

MasterZiv 2020-06-19 09:17:33
actually it’s a wrong query, but I wanted to know why
MasterZiv 2020-06-19 09:17:33
i don’t know.. this query outputs all rows
MasterZiv 2020-06-19 09:17:33
Like this – > Select salary from table_name group by Salary having salary = max(salary); ?
MasterZiv 2020-06-19 09:17:33
Can we use aggregate functions within group by?
MasterZiv 2020-06-19 09:17:33
no this does not work in all configurations of mysql
|