Question

Is there any easy way to get the sum of each columns for the selected number of rows. If not a simple mysql-only query any php/perl-mysql combination would also do.

select sum(col-1),sum(col-2)....sum(col-N) from 4gc_1h_atch_enb where EVENT_TIME between '2014-04-15 00:00:00' and '2014-04-15 23:00:00' and ENB='310-120-401409(M)';

Table has 50+ columns and also i have to repeat this for multiple(30+) such tables.

Was it helpful?

Solution

You will want to do a CONCAT with INFORMATION_SCHEMA views for Columns and Tables and then a GROUP_CONCAT with that. A basic structure looks like this:

SELECT CONCAT(GROUP_CONCAT(CONCAT('SUM(',COLUMN_NAME,')')),',',(SELECT GROUP_CONCAT(COLUMN_NAME) 
FROM INFORMATION_SCHEMA.COLUMNS
WHERE table_name = '4gc_1h_atch_enb'
AND DATA_TYPE <> 'int'))
FROM INFORMATION_SCHEMA.COLUMNS
WHERE table_name = '4gc_1h_atch_enb'
AND DATA_TYPE = 'int'

That will give you the "sum(col-1),sum(col-2)....sum(col-N)" part. You can do something similar with tables. The WHERE part is also part of the CONCAT. In the end you will have a script to execute.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top