Doubt in the method of using Distinct and Count together!

Asked

Viewed 1,453 times

4

I have a requested table as the example below:

 Id | Cliente | Status
 1  | XPTO    | Proposta
 2  | ABCD    | Proposta
 3  | XPTO    | Venceu
 4  | XPTO    | Perdeu

And I want to present a result like this:

Cliente | Status    | Qtd
XPTO    | Proposta  | 02
XPTO    | Venceu    | 01
ABCD    | Proposta  | 01

I’ve made a few attempts and got nothing!

The most I got was the result below in Mysql Workbrench, but I could not play it in my code:

Status    | Qtd
Proposta  | 03
Venceu    | 01

Code executed in Mysql Workbrench:

SELECT distinct (status), count(cliente) as qtd_cliente 
FROM portal.pedidos 
group by status; 

And how to do this in Laravel? Below I’m putting the controller code:

class RelatorioController extends Controller
{
    public function index ()
    {
        $relatorio = \App\Pedido::join('clientes', 'cliente', '=', 'nome')
        ->get();

        $cliente = \App\Cliente::lists('nome');

        return view ('relatorio.index',compact('relatorio','cliente'));
    }

    public function status()
    {

//o código abaixo é exatamente onde tenho dúvida, por isto o mesmo não está correto!!! Estas foram tentativas mal sucedidas dos meus testes.

        $status = \App\Pedido::distinct('status')           
            ->groupby('cliente')
            ->get();    

        return view ('relatorio.status', compact ('status'));
    }
}
  • Why Question Tagged Laravel?

  • If there are several customers who have made proposals, which one you want to display in the column Cliente of response?

  • If the problem is on the PHP side post the problem code graft.

  • I don’t see the need to use distinct since you are already grouping in your group by status;

  • by chance, in 4 | XPTO | Perdeu would not be 4 | XPTO | Proposta?

  • 1

    To give the question as resolved just press the V on the right answer side.

Show 1 more comment

1 answer

3


You don’t need the distinct, the group by does all the work in this case:

SELECT cliente, status, count(cliente) as qtd_cliente 
FROM portal.pedidos 
group by cliente, status;

If you use distinct(status) as in your example, it will pick up status without repeating, omitting customers.

  • Perfect!!! Thank you so much for your help!!! You know how to do this in the Lockable???

  • 1

    @Danielanselmodealmeida Unfortunately I do not know but it should be very easy. Have a look at the Eloquent Query Builder documentation: https://laravel.com/docs/5.1/queries#Ordering-grouping-limit-and-offset

  • I see you’re new to Stack Overflow. If you think my answer solved your problem, please mark it as 'OK' by clicking the green check this way: http://i.stack.Imgur.com/Qpogp.png Thanks! Hug!

  • 1

    I managed to solve my problem!!! Below is the code that worked... $status = \App\Pedido::select('cliente', 'status', DB::raw('count(cliente) as qtd_cliente'))
 ->groupby('cliente','status')
 ->get();

Browser other questions tagged

You are not signed in. Login or sign up in order to post.