【问题标题】:How to make sql query for adding calculated values group by id?如何进行 sql 查询以按 id 添加计算值组?
【发布时间】:2019-09-08 08:44:58
【问题描述】:

我有一张表,上面有名字、negeri 以及开始服务和结束服务的年份。我想得到一个人在一个州服务的年数:

  ID | Name     | State   |   yearstart     |    yearend
  -----------------------------------------------------------
  1  | Tom      | Kedah   |      2001       |      2002
  1  | Tom      | Kedah   |      2003       |      2007
  2  | Anne     | Melaka  |      2008       |      2012
  2  | Anne     | Melaka  |      2013       |      2018
  3  | Bill     | KL      |      2000       |      2001

我已经试过了。但它只计算年数并列出所有人(姓名和状态重复)。

   $query = DB::table('itemregistrations')
                ->join('state', 'itemregistrations.StateID', '=', 'state.StateID')
                ->select('itemregistrations.name');

  if($request->input('negeri_perkhidmatan') != '') {
            $query->join('itemregistrationpangkat', 'itemregistrationpangkat.itemRegistrationID', '=', 'itemregistrations.itemRegistrationID')
                ->where('itemregistrationpangkat.StateID', $request->input('negeri_perkhidmatan'));           
        }

  if(request('tempoh_negeri')) {
            $query->whereRaw('yearend - yearstart >= ?', [request('tempoh_negeri')]);  
        }

结果显示:

  ID | Name     | State   |    years   
  ---------------------------------------
  1  | Tom      | Kedah   |      1      
  1  | Tom      | Kedah   |      4     
  2  | Anne     | Melaka  |      4       
  2  | Anne     | Melaka  |      5      
  3  | Bill     | KL      |      1     

结果应该是:

  ID | Name     | State   |    years   
  ---------------------------------------
  1  | Tom      | Kedah   |      5      
  2  | Anne     | Melaka  |      9            
  3  | Bill     | KL      |      1 

浏览器中显示的结果是使用 ajax 获取的。我这里就不放了。

【问题讨论】:

  • 只需将groupBy('ID') 添加到您的查询中
  • 我不能简单地使用 groupby 并且我之前已经尝试过,但它失败了。因为它不计算但选择具有不同值的列。查看当前的sql代码。
  • 你的代码是不是像这样$query->whereRaw('yearend - yearstart >= ?', [request('tempoh_negeri')]).groupBy('ID');
  • $query->whereRaw('yearend - yearstart >= ?', [request('tempoh_negeri')])->groupBy('itemregistrations.ItemRegistrationID');
  • 怎么样$query->whereRaw('yearend - yearstart >= ?', [request('tempoh_negeri')])->groupBy('ID')->select('ID,Name,State,SUM(years) as years');

标签: mysql sql laravel sum laravel-query-builder


【解决方案1】:
SELECT 
    m.userid,
    m.name,
    m.state,
    SUM(m.yearend - m.yearstart) AS duration
FROM
    mytable m
GROUP BY m.userid , m.state , m.name;

我认为这应该可以解决您的问题。

【讨论】:

    【解决方案2】:
    select name, state (max(yearend) - min(yearstart)) as years from state group by name;
    
    
    +------+--------+------+
    | name | state  | years |
    +------+--------+------+
    | Anne | Melaka |   10 |
    | Bill | KL     |    1 |
    | Tom  | Kedah  |    6 |
    +------+--------+------+
    

    【讨论】:

      【解决方案3】:

      这将起作用:

      select m.id,m.name,sum(m.duration) from (select ID,Name,yearend-yearstart 
                            as duration from Table1)m group by  m.id,m.name;
      

      检查 :http://sqlfiddle.com/#!9/9029438/4

      【讨论】:

      • 你的 sql 工作得很好,但我需要适应 laravel 格式..我需要时间来转换它
      • 我接受您的回答,因为您的 sql 解决了我的查询问题。我会找到一种将它们转换为 laravel 格式的方法。感谢您的帮助
      【解决方案4】:

      years 列上尝试 SUM(),然后按 IDNameState 进行 GROUP BY。

      这将聚合您试图避免的“重复”行。

      【讨论】:

      • 请理解,表格在上面而不是下面。请先理解问题..
      【解决方案5】:

      试试这个!
      按 ID 从表组中选择 id,name,state,sum(years) 年

      【讨论】:

      • 如果你理解我的问题,那就没有那么简单了。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-11-30
      • 2023-02-26
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多