Get corrosponding record Of Row in Another Column of SQL Table

前端 未结 3 1436
傲寒
傲寒 2021-01-29 06:20

I am having SQL table where the records of the employee on daily basis are stored/saved I would like to get that result in tabular format

Consider a Table As Shown Below

相关标签:
3条回答
  • 2021-01-29 06:46

    Assuming that there is at most one row of each type for each person, you can do this using conditional aggregation:

    select name,
           max(case when action = 'INTIME' then dateandtime end) as intime,
           max(case when action = 'OUTTIME' then dateandtime end) as outtime,
           max(case when action = 'LUNCH' then dateandtime end) as lunch
    from t
    group by name;
    

    If you have multiple values for a given name, I would recommend that you ask a new question with details on how to handle that situation.

    0 讨论(0)
  • 2021-01-29 06:53

    I had used a CTE which is worked for me and is as follows

    WITH cte 
    AS (SELECT Row_number() 
    OVER( 
    partition BY [name] 
    ORDER BY [name]) AS rn, *FROM   table1)           
    SELECT a.[dateandtime]  AS [IN], 
    b.[dateandtime]  AS [OUT], 
    a.[action]  AS [myaction], 
       c.[dateandtime]  AS [lunchTIME] 
    FROM   (SELECT * 
    FROM   cte a 
    WHERE  rn = 1) a 
    JOIN (SELECT * 
    FROM   cte b 
    WHERE  rn = 2) b 
    ON a.[name] = b.[name] 
    LEFT JOIN (SELECT * 
    FROM   cte b 
    WHERE  rn = 3) c 
    ON a.[name] = c.[name]  
    
    0 讨论(0)
  • 2021-01-29 06:58

    Consider the following, which uses application code to handle the pivoting/rearrangement of an array...

    <?php
    
    /*
    DROP TABLE IF EXISTS my_table;
    
    CREATE TABLE my_table
    (id SERIAL PRIMARY KEY
    ,name VARCHAR(12) NOT NULL
    ,dt DATETIME NOT NULL
    ,action VARCHAR(12) NOT NULL
    );
    
    INSERT INTO my_table VALUES
    (1,'John'     ,'2019-07-07 10:00:00','in'),
    (2,'John'     ,'2019-07-07 18:00:00','out'),
    (3,'William'  ,'2019-07-07 10:02:00','in'),
    (4,'Catherine','2019-07-07 10:10:00','in'),
    (5,'John'     ,'2019-07-07 13:00:00','lunch'),
    (6,'Catherine','2019-07-07 18:30:00','out'),
    (7,'Catherine','2019-07-07 14:30:00','lunch'),
    (8,'William'  ,'2019-07-07 19:14:00','in');
    */
    
    require('path/to/connnection/stateme.nts');
    
    $query = "
    SELECT id
         , name
         , dt
         , action
      FROM my_table
     ORDER
        BY name
         , action;
    ";
    
    $result = mysqli_query($db,$query);
    
    $array = array();
    
    while($row = mysqli_fetch_assoc($result)){
    $array[] = $row;
    }
    
    foreach($array as $v){
    $new_array[$v['name']][$v['action']] = $v['dt'];
    }
    
    
    print_r($new_array);
    
    ?>
    

    Outputs:

    Array
    (
        [Catherine] => Array
            (
                [in] => 2019-07-07 10:10:00
                [lunch] => 2019-07-07 14:30:00
                [out] => 2019-07-07 18:30:00
            )
    
        [John] => Array
            (
                [in] => 2019-07-07 10:00:00
                [lunch] => 2019-07-07 13:00:00
                [out] => 2019-07-07 18:00:00
            )
    
        [William] => Array
            (
                [in] => 2019-07-07 19:14:00
            )
    
    )
    
    0 讨论(0)
提交回复
热议问题