如何根据类似条件插入表-如果同一表中没有数据

时间:2018-07-19 05:18:50

标签: sql snowflake-datawarehouse snowflake

下面是我的表格结构。我只想在TABLE2中只保留一行以获取最大日期和小时。

TABLE1 :
--------------------------------------
 col1   | col2  | col3 | date | hour |
--------------------------------------


TABLE2 :
-----------------
 date   | hour  |
-----------------

条件: 仅当TABLE2中没有数据时,才从TABLE1中插入TABLE2。

说明: 提取最大值(日期),并为该最大值日期从TABLE1中提取最大值(小时)。 并且唯一的条件是仅当TABLE2中没有行时,才将以上数据插入TABLE2中。

如何在Snowflake SQL中实现此目标?

2 个答案:

答案 0 :(得分:1)

我会这样:

    <?php
    function getLastNDays($days, $format = 'Y-m-d'){

        $retunData = array();

        $m = date("m"); $de = date("d")-1; $y= date("Y");
        $dateArray = array();
        for($i=0; $i<=$days-1; $i++){

            $dateArray[] = date($format, mktime(0,0,0,$m,($de-$i),$y)); 
        }
        //$dateArray[] = 'x';
        $resultArray =  array_reverse($dateArray);

        // After get the date Range find out Statrt & End date
        $start_date = $resultArray[0];
        $end_date = $resultArray[count($resultArray)-1];

        $from_date = new DateTime($start_date);
        $to_date = new DateTime($end_date);
        $interval = new DateInterval('P1D');
        $dateRange = new DatePeriod($from_date, $interval, $to_date);

        $week_number = 1;
        $weekArray = array();

        foreach ($dateRange as $key => $date) {
            $weekArray[$week_number][] = $date->format('Y-m-d');

            $mod = $key%7;
            if($mod == 6){
              $week_number++;
            }  

            /*
            if ($date->format('w') == 6) {
                $week_number++;
            }
            */
        }
        $retunData['weekArray'] = $weekArray;

        //Get the Start & End Date each week.   
        $weekrange = array_map(function($w) {
            return 'start: ' . array_shift($w) 
                . ', end: ' . array_pop($w); },
        $weekArray);

        $retunData['weekRange'] = $weekrange;

        return $retunData;
    }

    $retunData = getLastNDays(30);  // Call your custom function.

    echo '<pre>';
    print_r($retunData);
    exit;

    ?>

    Your Output will be as per the date range.



   Array
(
    [weekArray] => Array
        (
            [1] => Array
                (
                    [0] => 2018-06-20
                    [1] => 2018-06-21
                    [2] => 2018-06-22
                    [3] => 2018-06-23
                    [4] => 2018-06-24
                    [5] => 2018-06-25
                    [6] => 2018-06-26
                )

            [2] => Array
                (
                    [0] => 2018-06-27
                    [1] => 2018-06-28
                    [2] => 2018-06-29
                    [3] => 2018-06-30
                    [4] => 2018-07-01
                    [5] => 2018-07-02
                    [6] => 2018-07-03
                )

            [3] => Array
                (
                    [0] => 2018-07-04
                    [1] => 2018-07-05
                    [2] => 2018-07-06
                    [3] => 2018-07-07
                    [4] => 2018-07-08
                    [5] => 2018-07-09
                    [6] => 2018-07-10
                )

            [4] => Array
                (
                    [0] => 2018-07-11
                    [1] => 2018-07-12
                    [2] => 2018-07-13
                    [3] => 2018-07-14
                    [4] => 2018-07-15
                    [5] => 2018-07-16
                    [6] => 2018-07-17
                )

            [5] => Array
                (
                    [0] => 2018-07-18
                )

        )

    [weekRange] => Array
        (
            [1] => start: 2018-06-20, end: 2018-06-26
            [2] => start: 2018-06-27, end: 2018-07-03
            [3] => start: 2018-07-04, end: 2018-07-10
            [4] => start: 2018-07-11, end: 2018-07-17
            [5] => start: 2018-07-18, end: 
        )

)

答案 1 :(得分:0)

您可以使用以下策略:

insert into table2
   with rowcount as (select count(1) cnt from table2)
select mydate, hour from table1 
   join rowcount
   where rowcount.cnt = 0
   order by mydate desc, hour desc 
   limit 1;