Skip to content
This repository was archived by the owner on Apr 17, 2019. It is now read-only.
This repository was archived by the owner on Apr 17, 2019. It is now read-only.

Datatable::query with a query that has groupBy gives incorrect total row count. #253

Closed

Description

@timgws
        $query = DB::table('orders')
            ->orderBy('user_id');
            ->groupBy('user_id');

The row count will always be the total amount of orders for the first row returned.

The query that is built is:

select count(*) as aggregate from `orders` group by `user_id`
-- this returns a row for each user_id

However, because of the group by, the query that is built should be:

select count(DISTINCT user_id) as aggregate from `orders`
-- the following returns only one row. the total amount of users that have orders

I am trying to look into an easy way to fix this issue, but so far it looks like there might not be one. It looks like extending Query\Builder and exposing the protected $groups array might be the way to go.

Activity

  1. timgws commented on Mar 8, 2015

    @timgws
    CollaboratorAuthor

    Pull requests have been merged \o/

  2. bytestream commented on Sep 26, 2015

    @bytestream

    Can we get the same merged into the develop branch for Laravel 5?

    I noticed there's already a setNoGroupByOnCount() function in the QueryEngine (in the develop branch) - see #136. That being said, this does not work if you use Illuminate\Database\Eloquent\Builder (seems to be built directly for Illuminate\Database\Query\Builder)

  3. timgws commented on Sep 27, 2015

    @timgws
    CollaboratorAuthor

    @bytestream good catch, I will work on this a little later today.

  4. reopened this on Sep 27, 2015
  5. added this to the 5.0 milestone on Sep 27, 2015
  6. self-assigned this
    on Sep 27, 2015
  7. bytestream commented on Sep 29, 2015

    @bytestream

    @timgws any news on this? I'm happy to submit a pull request resolving the issue with the Eloquent Builder in the develop branch

  8. timgws commented on Sep 30, 2015

    @timgws
    CollaboratorAuthor

    @bytestream feel free to submit a pull request. I am currently focusing my efforts at the moment on a new branch, refactoring some of the code for a new version5 branch.

    If you don't do it today, I will try to get around to "soon" (I want to say after I finish work, but that might not be the case).

  9. added a commit that references this issue on Oct 6, 2015
    0b88e2e
  10. timgws commented on Oct 6, 2015

    @timgws
    CollaboratorAuthor

    This is almost on point, @bytestream, but I would recommend splitting out all of that code to a separate function. At the moment there is code in two places that does exactly the same thing.

  11. bytestream commented on Oct 6, 2015

    @bytestream

    Good point - I did this in a rush.

    I'll update the pull request before I leave in a few hours

    On 6 October 2015 at 14:52, Tim Groeneveld notifications@github.com wrote:

    This is almost on point, @bytestream https://github.com/bytestream, but
    I would recommend splitting out all of that code to a separate function. At
    the moment there is code in two places that does exactly the same thing.

    —
    Reply to this email directly or view it on GitHub
    #253 (comment).

  12. added a commit that references this issue on Oct 6, 2015
    cfe2783
  13. added a commit that references this issue on Oct 7, 2015
  14. timgws commented on Oct 7, 2015

    @timgws
    CollaboratorAuthor

    Thank you very much for this, @bytestream

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Projects

No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions