Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/mysql/63.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Mysql 用groupby对laravel的雄辩质疑_Mysql_Laravel_Eloquent - Fatal编程技术网

Mysql 用groupby对laravel的雄辩质疑

Mysql 用groupby对laravel的雄辩质疑,mysql,laravel,eloquent,Mysql,Laravel,Eloquent,我有这张桌子: +-------+-------------+-----------------+---------+--------+---------+------------+-------------+---------------------+---------------------+--------------+--------------+--------+------+------+----------------------+-------------------------

我有这张桌子:

+-------+-------------+-----------------+---------+--------+---------+------------+-------------+---------------------+---------------------+--------------+--------------+--------+------+------+----------------------+----------------------------+----------------------------+------------+---------------------+-----------------+-----------------+-------------------------------+
| id    | customer_id | web_provider_id | slot_id | tot_in | tot_out | partial_in | partial_out | created_at          | updated_at          | reading_date | gross_income | tax_id | preu | aams | web_provider_cost_id | customer_web_provider_cost | operator_web_provider_cost | net_income | income_partition_id | customer_income | operator_income | tot_in_for_next_cycle_closing |
+-------+-------------+-----------------+---------+--------+---------+------------+-------------+---------------------+---------------------+--------------+--------------+--------+------+------+----------------------+----------------------------+----------------------------+------------+---------------------+-----------------+-----------------+-------------------------------+
| 45048 |        2244 |               1 |    2843 |  21462 |   14325 |          0 |           0 | 2021-05-30 08:35:25 | 2021-05-30 08:35:25 | 2020-10-04   |            0 |      1 |    0 |    0 |                 2239 |                          0 |                          0 |          0 |                2236 |               0 |               0 |                         28000 |
| 45049 |        2244 |               1 |    2826 |  31249 |   20305 |          0 |           0 | 2021-05-30 08:35:25 | 2021-05-30 08:35:25 | 2020-10-04   |            0 |      1 |    0 |    0 |                 2239 |                          0 |                          0 |          0 |                2236 |               0 |               0 |                         60000 |
| 45050 |        2248 |              11 |    2915 |  21547 |   14227 |          0 |           0 | 2021-05-30 08:35:25 | 2021-05-30 08:35:25 | 2020-10-05   |            0 |      1 |    0 |    0 |                 2243 |                          0 |                          0 |          0 |                2240 |               0 |               0 |                         30000 |
+-------+-------------+-----------------+---------+--------+---------+------------+-------------+---------------------+---------------------+--------------+--------------+--------+------+------+----------------------+----------------------------+----------------------------+------------+---------------------+-----------------+-----------------+-------------------------------+

我必须执行一个查询以在yajra数据表中使用,我需要得到:

每个插槽id的1个结果(分组依据)

  • 只有最新的“阅读日期”
  • 客户id、网络提供商id、网络提供商id、成本id、税收id、收入分配id、总收入、, 仅在最近的“阅读日期”进行汇总
  • 我需要部分收入、部分收入、总收入、preu、aams、净收入的总和, 客户收入和运营商收入
如何在执行查询时不遇到“完整”组错误??我花了将近两天的时间来回答这个问题

我编写和重写了数千次查询,我想,在结束时,我返回了以下代码:

public function query(DefinitiveReading $model)
    {
return $model->newQuery()
                ->with('customer')
                ->with('webProvider')
                ->with('slot')
                ->with('tax')
                ->with('webProviderCost')
                ->with('incomePartition')
                ->with('tax')
                ->select([
                    'definitive_readings.slot_id',
                    'definitive_readings.customer_id',
                    'definitive_readings.slot_id as slot_code',
                    DB::raw('SUM(definitive_readings.partial_in) as sum_partial_in')
                ])
                ->groupBy('definitive_readings.slot_id')
                ; 
}

谢谢

更新06-01-2021

使用下面的查询,有很多子查询,我得到了我需要的,但是性能下降太多。 查询8000条记录需要35/40秒。有一种方法可以更好地编写查询并提高性能

SELECT
    s.web_provider_id AS web_provider_id,
    def1.slot_id AS slot_id,
    def1.customer_id AS customer_id,
    (SELECT MAX(def2.reading_date) FROM definitive_readings AS def2 WHERE def2.slot_id = def1.slot_id AND def2.customer_id = def1.customer_id) AS last_reading_date,
    (SELECT def2.tot_in FROM definitive_readings AS def2 WHERE def2.slot_id = def1.slot_id AND def2.reading_date = last_reading_date AND def2.customer_id = def1.customer_id) AS last_tot_in,
    (SELECT def2.tot_out FROM definitive_readings AS def2 WHERE def2.slot_id = def1.slot_id AND def2.reading_date = last_reading_date AND def2.customer_id = def1.customer_id) AS last_tot_out,
    (SELECT SUM(def2.partial_in) FROM definitive_readings AS def2 WHERE def2.slot_id = def1.slot_id AND def2.customer_id = def1.customer_id) AS sum_partial_in,
    (SELECT SUM(def2.partial_out) FROM definitive_readings AS def2 WHERE def2.slot_id = def1.slot_id AND def2.customer_id = def1.customer_id) AS sum_partial_out,
    DATEDIFF((SELECT MAX(def2.reading_date) FROM definitive_readings AS def2 WHERE def2.slot_id = def1.slot_id AND def2.customer_id = def1.customer_id),
             (SELECT MIN(def2.reading_date) FROM definitive_readings AS def2 WHERE def2.slot_id = def1.slot_id AND def2.customer_id = def1.customer_id)) AS 'diff_days',
    (SELECT SUM(def2.partial_in) FROM definitive_readings AS def2 WHERE def2.slot_id = def1.slot_id AND def2.customer_id = def1.customer_id) / DATEDIFF((SELECT MAX(def2.reading_date) FROM definitive_readings AS def2 WHERE def2.slot_id = def1.slot_id),
                                    (SELECT MIN(def2.reading_date) FROM definitive_readings AS def2 WHERE def2.slot_id = def1.slot_id)) as avg_partial_in,
    SUM(def1.gross_income) AS sum_gross_income,
    (SELECT def2.tax_id FROM definitive_readings AS def2 WHERE def2.slot_id = def1.slot_id AND def2.customer_id = def1.customer_id AND def2.reading_date = last_reading_date) AS last_tax_id,
    (SELECT SUM(def2.preu) FROM definitive_readings AS def2 WHERE def2.slot_id = def1.slot_id AND def2.customer_id = def1.customer_id) AS sum_preu,
    (SELECT SUM(def2.aams) FROM definitive_readings AS def2 WHERE def2.slot_id = def1.slot_id AND def2.customer_id = def1.customer_id) AS sum_aams,
    (SELECT SUM(def2.net_income) FROM definitive_readings AS def2 WHERE def2.slot_id = def1.slot_id AND def2.customer_id = def1.customer_id) AS sum_net_income,
    (SELECT def2.web_provider_cost_id FROM definitive_readings AS def2 WHERE def2.slot_id = def1.slot_id AND def2.customer_id = def1.customer_id AND def2.reading_date = last_reading_date) AS last_web_provider_cost_id,
    (SELECT SUM(def2.customer_web_provider_cost) FROM definitive_readings AS def2 WHERE def2.slot_id = def1.slot_id AND def2.customer_id = def1.customer_id) AS sum_customer_web_provider_cost,
    (SELECT SUM(def2.operator_web_provider_cost) FROM definitive_readings AS def2 WHERE def2.slot_id = def1.slot_id AND def2.customer_id = def1.customer_id) AS sum_operator_web_provider_cost,
    (SELECT def2.income_partition_id FROM definitive_readings AS def2 WHERE def2.slot_id = def1.slot_id AND def2.customer_id = def1.customer_id AND def2.reading_date = last_reading_date) AS last_income_partition_id,
    (SELECT SUM(def2.customer_income) FROM definitive_readings AS def2 WHERE def2.slot_id = def1.slot_id AND def2.customer_id = def1.customer_id) AS sum_customer_income,
    (SELECT SUM(def2.operator_income) FROM definitive_readings AS def2 WHERE def2.slot_id = def1.slot_id AND def2.customer_id = def1.customer_id) AS sum_operator_income,
    MAX(c.id) AS last_cycle_id,
    (SELECT def2.tot_in_for_next_cycle_closing FROM definitive_readings AS def2 WHERE def2.slot_id = def1.slot_id AND def2.customer_id = def1.customer_id AND def2.reading_date = last_reading_date) AS last_next_cycle,
    IF((SELECT customer_id FROM definitive_readings WHERE definitive_readings.slot_id = def1.slot_id ORDER BY definitive_readings.reading_date DESC LIMIT 1) = def1.customer_id, true, false) AS installed
FROM definitive_readings AS def1
         LEFT JOIN cycles c ON def1.slot_id = c.slot_id
         LEFT JOIN slots s ON def1.slot_id = s.id
GROUP BY def1.slot_id, def1.customer_id
;

您还需要将
customer\u id
slot\u code
添加到
groupby
中,以避免出现错误。@Rwd是的,我添加了它们,但有很多组合可能导致错误。我必须使用很多子查询来避免错误并得到我需要的。但性能下降太多