获取最近2天的生产数据

时间:2014-10-09 10:19:38

标签: php sql oracle

我有一个关于从两天前获取制造数据的问题。我的代码有效,但我认为它对我的实现效率低下。我拥有的就是这个,

// YESTERDAYS FABRICATION
            $yesterdaySql = "SELECT SUM(CURRENT_WEIGHT) AS TOTALYESTERDAYWEIGHT FROM FABRICATION_HIST 
                                WHERE TO_CHAR (FAB_ENTRY_DATE,'MM/DD/YYYY') = TO_CHAR(SYSDATE - 1, 'MM/DD/YYYY') 
                                AND PROJECT_NAME = :projName";
            $yesterdayParse = oci_parse($conn, $yesterdaySql);
            oci_bind_by_name($yesterdayParse, ":projName", $_SESSION['cd-dropdown']);            
            oci_define_by_name($yesterdayParse, "TOTALYESTERDAYWEIGHT", $sumWeightYesterday);
            oci_execute($yesterdayParse);

            // TWO DAYS AGO FABRICATION
            $twoDaysSql = "SELECT SUM(CURRENT_WEIGHT) AS TOTALTWODAYSAGOWEIGHT FROM FABRICATION_HIST 
                                WHERE TO_CHAR (FAB_ENTRY_DATE,'MM/DD/YYYY') = TO_CHAR(SYSDATE - 2, 'MM/DD/YYYY') 
                                AND PROJECT_NAME = :projName";
            $twoDaysParse = oci_parse($conn, $twoDaysSql);
            oci_bind_by_name($twoDaysParse, ":projName", $_SESSION['cd-dropdown']);            
            oci_define_by_name($twoDaysParse, "TOTALTWODAYSAGOWEIGHT", $sumWeightTwoDays);
            oci_execute($twoDaysParse);

我知道这是非常低效的。任何人都可以建议我,以便我可以在一个查询中获取两天查询?

提前致谢

1 个答案:

答案 0 :(得分:0)

你在......

SELECT
    SUM(CASE WHEN TRUNC(FAB_ENTRY_DATE) = TRUNC(SYSDATE) - 1 THEN CURRENT_WEIGHT END) AS TOTALYESTERDAYWEIGHT,
    SUM(CASE WHEN TRUNC(FAB_ENTRY_DATE) = TRUNC(SYSDATE) - 2 THEN CURRENT_WEIGHT END) AS TOTALTWODAYSAGOWEIGHT
FROM FABRICATION_HIST 
WHERE FAB_ENTRY_DATE >= TRUNC(SYSDATE) - 2
    AND FAB_ENTRY_DATE < TRUNC(SYSDATE)
    AND PROJECT_NAME = :projName
;

......它甚至可以通过以下方式编入索引:

create index i_fabrication_hist_something
on t_fabrication_hist (project_name, fab_entry_date)
compress 1
compute statistics
;