代码之家  ›  专栏  ›  技术社区  ›  Jose

将Postgres中多列的分钟数与另一列的小时数和新列的小时总数相加

  •  -1
  • Jose  · 技术社区  · 2 年前

    我有一个查询,它是Postgres中多行的总和

    查询------------------------------------------------------------------

    
    select 
        ...
        sum(i.cantidad) as totalHoras, 
        sum(i.minutos) as totalMinutos,
        ...
    

    结果-------------------------------------------------------------------

    totalHoras  |   toltaMinutos
    
    152         |      450
    
    160         |      0
    
    163         |      90
    
    
    I could in the same statement, divide the minutes by 60 and add them at the same time to the hours, and put them in a new column called "total"
    
    152 + (450/60) = 159,5
    160 + (0/60)   = 160  <-- If the minutes are 0, check that it does not divide
    163 + (90/60)  = 164,5
    
    
    I use postgres version 12
    
        
    
    
    
    
    
        
    
    1 回复  |  直到 2 年前
        1
  •  1
  •   Zakaria    2 年前

    你有没有尝试过这样的东西:

    select 
        ...
        sum(i.cantidad) as totalHoras, 
        sum(i.minutos) as totalMinutos,
        sum(i.cantidad) + 1.0*sum(i.minutos)/60 as total,
        ...