Subquery - Group By - SQLite

1

Ok, I have two Queries, the first one I ask for the names of the clients and I order them alphabetically:

SELECT Firstname,
       Lastname
  FROM customers
 ORDER BY Firstname

In the second I request the total of purchases:

Select Customerid, Sum (Total) As Total
From invoices
Group by CustomerId

My goal is to get a table where instead of the CustomerId appearing, I can get FirstName and LastName.

According to me it would have to be something like this:

SELECT Firstname,
       Lastname
       (
       Select Customerid, Sum (Total) As Total
       From invoices
       Group by CustomerId
       ) As Total
  FROM customers
 ORDER BY Firstname

But it does not give me any results so I ask for your help.

Thank you!

    
asked by René Martínez 13.02.2018 в 05:14
source

1 answer

0

You need to make a join

SELECT Firstname,Lastname, Sum(i.Total) as Total 
FROM customers c Inner Join invoices i ON c.id=i.CustomerId 
Group By c.id ORDER BY c.Firstname
    
answered by 13.02.2018 / 05:31
source