in reply to Re^7: using system command in regex
in thread using system command in regex

Hey Programatic you are also doing the same but you are passing all the column names once by using join & map. Good thought. But in place holders If i place table name its showing a error.

DBD::mysql::st execute failed: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ''CDR' where Date = '13-10-2015' and Hour like '00:%'' at line 1 at DB_Test.pl line 25.

So I replaced my '?' with table name. Then its working good. Even though I used your logic I need to maintain all my column names in array. I want to change that. Here I am writing all columns in program into big arrays which is looking odd. Can I get that coloumn names from db???

Sample (Tested)

#!/usr/bin/perl use strict; use warnings; use DBI; my $db_date=$ARGV[0]; #04-10-2015 my $hour = "00"; my $db_name = "ravi"; my $table_name = "CDR"; my $host = "xxx.xx.x.xxx"; my @CDR_array = ("Total_MO","Total_MO_sucess","MO_userdep_error","MO_s +ubcrib_error","MO_system_error","Total_MO_success_rate","Total_AO","T +otal_AO_sucess","AO_userdep_error","AO_subcrib_error","AO_system_erro +r","Total_AO_success_rate","Total_MT_P2P","Total_MT_sucess_P2P","MT_P +2P_userdep_error","MT_P2P_subcrib_error","MT_P2P_system_error","Total +_MT_P2P_success_rate","Total_MT_A2P","Total_MT_sucess_A2P","MT_A2P_us +erdep_error","MT_A2P_subcrib_error","MT_A2P_system_error","Total_MT_A +2P_success_rate","Total_AT","Total_AT_sucess","AT_ESMEdep_error","AT_ +network_error","AT_system_error","Total_AT_success_rate","P2P_FDA_cou +nt","P2P_FDA_success_count","P2P_FDA_success_rate","A2P_FDA_count","A +2P_FDA_success_count","A2P_FDA_success_rate","Total_Count","Total_TPS +","Greatest_TPS"); my $dbh = DBI->connect("DBI:mysql:database=$db_name;host=$host;mysql_s +ocket=/opt/lampstack-5.5.27-0/mysql/tmp/mysql.sock","root","",{'Raise +Error' => 1}); my $sql = "select ".join ",", map { "sum($_)" } @CDR_array; $sql .= " from CDR where Date = ? and Hour like ?"; my $sth = $dbh->prepare( $sql ) or die "Cant get prepare"; $sth->execute($db_date, "$hour:%") or die $DBI::errstr; my @my_array = $sth->fetchrow_array(); print $my_array[1],$/; print @my_array,$/;

Replies are listed 'Best First'.
Re^9: using system command in regex
by shmem (Chancellor) on Oct 16, 2015 at 07:27 UTC
    Can I get that coloumn names from db???

    You can get table and column names from the db using queries:

    my (@tables, %map); my $q = $dbh->prepare("show tables"); $q->execute or die "Error listing tables"; while ( my ($table) = $q->fetchrow_array ) { push @tables, $table; } $q->finish; for my $table (@tables) { $q = $dbh->prepare("describe $table"); $q->execute or die "Error describing table $table"; while ( my $hr = $q->fetchrow_hashref ) { push @{ $map{$table} }, $hr->{Field}; } $q->finish; }

    Now you have a hash %map keyed on table names, whose values are anonymous arrays containing column names. Use this array to build your query:

    my $table_name = "CDR"; my $sql = "select ".join ",", map { "sum($_)" } @{$map{$table_name}}; $sql .= " from $table_name where Date = ? and Hour like ?";
    perl -le'print map{pack c,($-++?1:13)+ord}split//,ESEL'

      Sorry. Its my mistake to not saying for you clearly. Here if I am using join & map its returning all the sum of the columns. but I have percentages in the middle. The percentages also adding and its returning like "590.04%". After seeing this result I changed my program to

      my $sql = "select sum(Total_MO),sum(Total_MO_sucess),sum(MO_userdep_er +ror),sum(MO_subcrib_error),sum(MO_system_error),sum(Total_MO_success_ +rate)/(select count(Total_MO_success_rate) from CDR where Date = '13- +10-2015' and Hour like '00:%'),sum(Total_AO),sum(Total_AO_sucess),sum +(AO_userdep_error),sum(AO_subcrib_error),sum(AO_system_error),sum(Tot +al_AO_success_rate)/(select count(Total_AO_success_rate) from CDR whe +re Date = '13-10-2015' and Hour like '00:%'),sum(Total_MT_P2P),sum(To +tal_MT_sucess_P2P),sum(MT_P2P_userdep_error),sum(MT_P2P_subcrib_error +),sum(MT_P2P_system_error),sum(Total_MT_P2P_success_rate)/(select cou +nt(Total_AO_success_rate) from CDR where Date = '13-10-2015' and Hour + like '00:%'),sum(Total_MT_A2P),sum(Total_MT_sucess_A2P),sum(MT_A2P_u +serdep_error),sum(MT_A2P_subcrib_error),sum(MT_A2P_system_error),sum( +Total_MT_A2P_success_rate)/(select count(Total_AO_success_rate) from +CDR where Date = '13-10-2015' and Hour like '00:%'),sum(Total_AT),sum +(Total_AT_sucess),sum(AT_ESMEdep_error),sum(AT_network_error),sum(AT_ +system_error),sum(Total_AT_success_rate)/(select count(Total_AO_succe +ss_rate) from CDR where Date = '13-10-2015' and Hour like '00:%'),sum +(P2P_FDA_count),sum(P2P_FDA_success_count),sum(P2P_FDA_success_rate)/ +(select count(Total_AO_success_rate) from CDR where Date = '13-10-201 +5' and Hour like '00:%'),sum(A2P_FDA_count),sum(A2P_FDA_success_count +),sum(A2P_FDA_success_rate)/(select count(Total_AO_success_rate) from + CDR where Date = '13-10-2015' and Hour like '00:%'),sum(Total_Count) +,sum(Total_TPS),max(Greatest_TPS) from CDR where Date = ? and Hour li +ke ?"; foreach my $hour ("00","01","02","03","04","05","06","07","08","09",10 +..23) { $sth->execute($date, "$hour:%") or die $DBI::errstr; my @my_array = (); push(@my_array,$hour); push(@my_array,$sth->fetchrow_array()); my $col = 0; print @my_array,$/; foreach my $value (@my_array) { $sheet->AddCell( $row, $col, $value ); ++$col; } $row++; }

      Its too ugly I know. But have a look. Here I have Percentages & the last column is Greatest tps. To achieve this

      sum(A2P_FDA_success_rate)/(select count(Total_AO_success_rate) from CD +R where Date = '13-10-2015' and Hour like '00:%') max(Greatest_TPS)

      I need to write like this. Now show me a shortcut please.