我没有得到正确的结果

时间:2015-02-27 06:25:09

标签: mysql

我是MySQL的新手,请原谅。

我正在尝试报告所有SAN LUN(逻辑磁盘)以及它们是新的还是已在2个日期之间扩展。我在下面提供了我的代码。我的逻辑有些不对劲。我很乐意为你提供任何帮助。先感谢您。

   #OCTOBER SAN CHANGES
    USE ditinfo_devel;

    DROP TABLE IF EXISTS tempdb_1;
    DROP TABLE IF EXISTS tempdb_2;
    DROP TABLE IF EXISTS tempdb_3;

    # LAST LOG FOR PREVIOUS MONTH
    SET @month1start = '2014-08-10 00:00:00'; 
    SET @month1end = '2014-08-10 23:59:59'; 

    # LAST LOG FOR THIS MONTH
    SET @month2start = '2014-10-12 00:00:00'; 
    SET @month2end = '2014-10-12 23:59:59'; 

    CREATE TABLE tempdb_1( 
    lun_name VARCHAR( 50 ) NOT NULL ,
    report_date DATE NOT NULL ,
    san VARCHAR( 50 ) NOT NULL ,
    lun_raid VARCHAR( 50 ) NOT NULL ,
    type_of_change VARCHAR( 50 ) NOT NULL ,
    lun_size FLOAT( 10, 2 ) 
    );

    CREATE TABLE tempdb_2( 
    lun_name VARCHAR( 50 ) NOT NULL ,
    report_date DATE NOT NULL ,
    san VARCHAR( 50 ) NOT NULL ,
    lun_raid VARCHAR( 50 ) NOT NULL ,
    type_of_change VARCHAR( 50 ) NOT NULL ,
    lun_size FLOAT( 10, 2 ) 
    );

    CREATE TABLE tempdb_3( 
    sumWaAllocatedStorage_GB INT(11),
    sumBaAllocatedStorage_GB INT(11),
    sumAllocatedStorage_GB INT(11),
    lun_name VARCHAR( 50 ) NOT NULL ,
    report_date DATE NOT NULL ,
    san VARCHAR( 50 ) NOT NULL ,
    lun_raid VARCHAR( 50 ) NOT NULL ,
    type_of_change VARCHAR( 50 ) NOT NULL ,
    oldlun_size_GB FLOAT( 10, 2 ), 
    new_lun_size_GB FLOAT( 10, 2 ) 
    );

    **# My original code
    INSERT INTO tempdb_1
    # using distinct in case of old luns called the same name and no longer used.
    SELECT DISTINCT(lun_name), report_date, san, lun_raid, "Extended", lun_size
    FROM tbl_san_log
    WHERE date( report_date ) >= @month1start
    AND date( report_date ) <= @month1end
    # Need this in the query
    GROUP BY lun_name;**

    #------------------------------------------------------------------------------
    # I have tried the following code which I copied and made change to from this site but now my results are not showing any new lun_name(S) that have been created. 

    INSERT INTO tempdb_1
    SELECT tt.lun_name, tt.report_date, tt.san, tt.lun_raid, "Extended", tt.lun_size
    FROM tbl_san_log tt

    INNER JOIN
    (
    SELECT lun_name, MAX(lun_size) AS MaxLunSize
        FROM tbl_san_log
        GROUP BY lun_name
    ) groupedtt


    ON tt.lun_name = groupedtt.lun_name
    AND tt.lun_size = groupedtt.MaxLunSize
    WHERE date( tt.report_date ) >= @month1start
    AND date( tt.report_date ) <= @month1end;

    #------------------------------------------------------------------------------

    **# my original code
    INSERT INTO tempdb_2
    # using distinct in case of old luns called the same name and no longer used.
    SELECT DISTINCT(lun_name), report_date, san, lun_raid, "Extended", lun_size
    FROM tbl_san_log
    WHERE date( report_date ) >= @month2start
    AND date( report_date ) <= @month2end
    # Need this in the query
    GROUP BY lun_name;**


    #---------------------------------------------------------------------------
    # I have tried the following code which I copied and made change to from this site but now my results are not showing any new lun_name(S) that have been created. 

    INSERT INTO tempdb_2
    SELECT tt.lun_name, tt.report_date, tt.san, tt.lun_raid, "Extended", tt.lun_size
    FROM tbl_san_log tt

    INNER JOIN
    (
    SELECT lun_name, MAX(lun_size) AS MaxLunSize
        FROM tbl_san_log
        GROUP BY lun_name
    ) groupedtt


    ON tt.lun_name = groupedtt.lun_name
    AND tt.lun_size = groupedtt.MaxLunSize
    WHERE date( tt.report_date ) >= @month2start
    AND date( tt.report_date ) <= @month2end;

    #---------------------------------------------------------------------------


    SELECT SUM(lun_size) INTO @sumWaAllocatedStorage from tempdb_2 
    WHERE tempdb_2.san LIKE  '%wagga%' ;

    SELECT SUM(lun_size) INTO @sumBaAllocatedStorage from tempdb_2 
    WHERE tempdb_2.san LIKE  '%bathurst%';

    SELECT SUM(lun_size) INTO @sumAllocatedStorage from tempdb_2 
    WHERE tempdb_2.san NOT LIKE  'nas%';

    INSERT INTO tempdb_3
# EXTENDED LUNS 
   SELECT @sumWaAllocatedStorage, @sumBaAllocatedStorage, @sumAllocatedStorage, tempdb_2.lun_name, tempdb_2.report_date, tempdb_1.san, tempdb_1.lun_raid, tempdb_1.type_of_change, tempdb_1.lun_size, tempdb_2.lun_size
    FROM tempdb_2
    LEFT JOIN tempdb_1 ON tempdb_2.lun_name = tempdb_1.lun_name
    WHERE tempdb_1.lun_size  <>  tempdb_2.lun_size;

    INSERT INTO tempdb_3
# NEW LUNS    
SELECT @sumWaAllocatedStorage, @sumBaAllocatedStorage, @sumAllocatedStorage, tempdb_2.lun_name, tempdb_2.report_date, tempdb_2.san, tempdb_2.lun_raid, "New", 0, tempdb_2.lun_size
    FROM tempdb_2
    LEFT JOIN tempdb_1 ON tempdb_2.lun_name = tempdb_1.lun_name
    WHERE tempdb_1.lun_name is NULL;

    DROP TABLE IF EXISTS tempdb_1;
    DROP TABLE IF EXISTS tempdb_2;

    SELECT * FROM tempdb_3

0 个答案:

没有答案