It looks like you're dealing with polymorphic relations in SQL. Right now I don't have a set of tables to test with, but you should be able to do the join on the server-side like this (untested):
SELECT m.*, coalesce(sb.somecol, sd.somecol, tb.somecol, td.somecol) AS someco +l FROM KPI_Master m LEFT JOIN SDCCHBLOCKING sb ON m.NE = sb.NE AND m.KPI = 'SDCCHBLOCKING' LEFT JOIN SDCCHDROP sd ON m.NE = sd.NE AND m.KPI = 'SDCCHDROP' LEFT JOIN TCHBLOCKING tb ON m.NE = tb.NE AND m.KPI = 'TCHBLOCKING' LEFT JOIN TCHDROP td ON m.NE = td.NE AND m.KPI = 'TCHDROP'
This way you should only need to fetch n rows from the database -- where n is the amount of rows you have in KPI_Master -- with only a single round-trip. This should also simplify your code to something like this:
my $dbh = DBI->connect('dbi:ODBC:driver=Microsoft Access Driver (*.mdb +, *.accdb);dbq=D:\Project\Project\BSC_KPIMonitoring\CHN\E_BSC_KPIMoni +torDB.mdb', undef, undef, { RaiseError => 1}); my $query = <<EOF; SELECT m.BSC, m.KPI, m.THRESH, coalesce(sb.somecol, sd.somecol, tb.somecol, td.somecol) AS someco +l FROM KPI_Master m LEFT JOIN SDCCHBLOCKING sb ON m.NE = sb.NE AND m.KPI = 'SDCCHBLOCKING' LEFT JOIN SDCCHDROP sd ON m.NE = sd.NE AND m.KPI = 'SDCCHDROP' LEFT JOIN TCHBLOCKING tb ON m.NE = tb.NE AND m.KPI = 'TCHBLOCKING' LEFT JOIN TCHDROP td ON m.NE = td.NE AND m.KPI = 'TCHDROP' EOF my %kpis = (SDCCHBLOCKING => 'SB', SDCCHDROP => 'SD', TCHBLOCKING => 'TD', TCHDROP => 'TD'); my $sth = $dbh->prepare($query); $sth->execute; while (my @row = $sth->fetchrow_array()) { my ($bsc, $kpi, $thresh, $somecol) = @$row; if ($somecol > $thresh) { print join(",", $kpis{$kpi}, $bsc, $thresh, "/$somecol"); } }

Other points of interest for performance might be reducing the amount of columns you get: don't SELECT *, but SELECT theOneColumnYouNeed


In reply to Re: Perl DBI by Anonymous Monk
in thread Perl DBI by Arunkumarpro

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.