How to get two ties of maximum values in my query? - sql

I have a database of a music store and I need to extract the max value of purchases from a customer specific to each country. While using MAX function, I noticed that I have two ties of maximum values in 'United Kingdom'. So, I need my query to return both the customers for this country.
With t1 As (
Select i.CustomerId, c.FirstName, c.LastName,
i.BillingCountry Country, Sum(i.Total) Totals
From Invoice i
Join Customer c
On c.CustomerId = i.CustomerId
GROUP BY 1, 4)
Select CustomerId, FirstName, LastName, Country, Max(Totals) TotalSpent
From t1
Group By 4;
This is the output
This is what the output should be
I tried using TOP but apparently by workspace does not accept this function. So, Please suggest a solution that does not use this function.
Thanks in advance.

I would consider something like
With t1 As (
Select i.CustomerId, c.FirstName, c.LastName,
i.BillingCountry Country, Sum(i.Total) Totals
From Invoice i
Join Customer c
On c.CustomerId = i.CustomerId
GROUP BY 1, 4)
Select CustomerId, FirstName, LastName, Country, Totals TotalSpent
From t1
WHERE t1.Total = (SELECT MAX(Totals) FROM t1 t2 WHERE t1.Country = t2.Country)
Group By 4;
(I've changed MAX(Totals) to Totals in your main query's SELECT statement and added the WHERE clause)
or
With t1 As (
Select i.CustomerId, c.FirstName, c.LastName,
i.BillingCountry Country, Sum(i.Total) Totals
From Invoice i
Join Customer c
On c.CustomerId = i.CustomerId
GROUP BY 1, 4),
t2 as (
SELECT Country, MAX(Totals) as Totals
FROM t1
GROUP BY Country
)
Select t1.CustomerId, t1.FirstName, t1.LastName, t1.Country, t1.Totals TotalSpent
From t1 INNER JOIN t2
on t1.Country = t2.Country and t1.Totals = t2.Totals
Group By 4;
(I've added the t2 CTE, joined it into your main query, and adjusted the main SELECT accordingly)
In both cases, I'm trying to select all the customer information where that customer's total is equal to the maximum total for their country. In principle, this should work no matter how many ties there are.

Use window functions!
select CustomerId, FirstName, LastName,
Country, Totals
from (select i.CustomerId, c.FirstName, c.LastName,
i.BillingCountry as Country, sum(i.Total) as Totals,
rank() over (partition by i.BillingCountry over sum(i.Total) desc) as seqnum
from Invoice i join
Customer c
on c.CustomerId = i.CustomerId
group by i.CustomerId, c.FirstName, c.LastName,
i.BillingCountry
) ic
where seqnum = 1;

Related

How can i get all the MAX values from a certain column in a dataset in PostgreSQL

I'm asked to find the top user for different countries, however, one of the countries has 2 users with the same amount spent so they should both be the top users, but I can't get the max value for 2 values in this country.
Here is the code:
WITH t1 AS (
SELECT c.customerid,SUM(i.total) tot
FROM invoice i
JOIN customer c ON c.customerid = i.customerid
GROUP BY 1
ORDER BY 2 DESC
),
t2 AS (
SELECT c.customerid as CustomerId ,c.firstname as FirstName,c.lastname as LastName, i.billingcountry as Country,MAX(t1.tot) as TotalSpent
FROM t1
JOIN customer c
ON c.customerid = t1.customerid
JOIN invoice i ON i.customerid = c.customerid
GROUP BY 4
ORDER BY 4
)
SELECT *
FROM t2
BILLINGCOUNTRY is in Invoice, and it has the name of all the countries.
TOTAL is also in invoice and it shows how much is spent for each purchase by Customer (so there are different fees and taxes for each purchase and total shows the final price payed by the user at each time)
Customer has id,name,last name and from its' ID I'm extracting the total of each of his purchases
MAX was used after finding the sum for each Customer and it was GROUPED BY country so that i could find the max for each country, however I can't seem to find the max of the last country that had 2 max values
Use rank() or dense_rank():
SELECT c.*, i.tot
FROM (SELECT i.customerid, i.billingCountry, SUM(i.total) as tot,
RANK() OVER (PARTITION BY i.billingCountry ORDER BY SUM(i.total) DESC) as seqnum
FROM invoice i
GROUP BY 1, 2
) i JOIN
customer c
ON c.customerid = i.customerid
WHERE seqnum = 1;
The subquery finds the amount per customer in each country -- and importantly calculates a ranking for the combination with ties having the same rank. The outer query just brings in the additional customer information that you seem to want.
here is how it worked for me since i was restricted from using many Commands such RIGHT JOIN and RANK() (As what Gordon Linoff suggessted) so i had to create a 3rd case for the anamoly and join it using union. this solution works only on this case, the good solution is the one posted by Gordon Linoff:
WITH t1 AS (
SELECT c.customerid,SUM(i.total) tot
FROM invoice i
JOIN customer c ON c.customerid = i.customerid
GROUP BY 1
ORDER BY 2 DESC
),
t2 AS (
SELECT c.customerid as CustomerId ,c.firstname as FirstName,c.lastname as LastName, i.billingcountry as Country,MAX(t1.tot) as TotalSpent
FROM t1
JOIN customer c
ON c.customerid = t1.customerid
JOIN invoice i ON i.customerid = c.customerid
GROUP BY 4
ORDER BY 4
) ,
t3 AS (
SELECT DISTINCT c.customerid as CustomerId ,c.firstname as FirstName,c.lastname as LastName, i.billingcountry as Country,t1.tot as TotalSpent
FROM t1
JOIN customer c
ON c.customerid = t1.customerid
JOIN invoice i ON i.customerid = c.customerid
WHERE i.billingcountry = 'United Kingdom'
ORDER BY t1.tot DESC
LIMIT 2
)
SELECT *
FROM t2
UNION
SELECT * FROM t3
ORDER BY t2.country

I need a solution to this SQL Query I'm trying to solve

"Write a query that determines the customer that has spent the most on
music for each country. Write a query that returns the country along
with the top customer and how much they spent. For countries where the
top amount spent is shared, provide all customers who spent this
amount.
You should only need to use the Customer and Invoice tables.
Check Your Solution
Though there are only 24 countries, your query should return 25 rows
because the United Kingdom has 2 customers that share the maximum."
You can find the data set here
.
Here is the code I tried with the results
And here is the expected outcome
Generally, you should always GROUP BY anything in your SELECT that is not an aggregation function (e.g. SUM). Try this:
SELECT c.CustomerId, c.FirstName, c.LastName, c.Country,
SUM(i.Total) AS TotalSpent
FROM Customer c
JOIN Invoice i
ON i.CustomerId = c.CustomerId
GROUP BY c.CustomerId, c.FirstName, c.LastName, c.Country
ORDER BY c.Country
WITH tab1 AS ( SELECT c.CustomerId, c.FirstName, c.LastName, c.Country, SUM(i.Total) TotalSpent FROM Customer c JOIN Invoice i ON c.CustomerId = i.CustomerId GROUP BY c.CustomerId ) SELECT tab1.* FROM tab1 left JOIN ( SELECT CustomerId, FirstName, LastName, Country, MAX(TotalSpent) AS TotalSpent FROM tab1 GROUP BY Country ) tab2 ON tab1.Country = tab2.Country WHERE tab1.TotalSpent = tab2.TotalSpent ORDER BY Country;

How return two equal max values for the same country when the query is grouped by the country?

For example I have to write a query that shows the customer who had spent the most in each country but if a country has two customers with same max value i have to show them both in the output.
I have wrote the query that return the maximum value for each customer in each country but the last country in my example which is 'United Kingdom' has two customers with same maximum values and i couldn't show them both.
SELECT c1.CustomerId, c1.FirstName,c1.LastName,c1.Country,
MAX(c1.TotalSpent) as TotalSpent
FROM
(SELECT c.CustomerId,c.FirstName, c.LastName,i.BillingCountry
Country, SUM(i.Total) totalspent
FROM Customer c
JOIN Invoice i
ON c.CustomerId = i.CustomerId
GROUP BY 1
ORDER BY totalspent
) c1
GROUP BY 4
ORDER BY Country
Use window functions!:
SELECT c.*
FROM (SELECT c.CustomerId, c.FirstName, c.LastName, i.BillingCountry as Country,
SUM(i.Total) as totalspent,
DENSE_RANK() OVER (PARTITION BY i.BillingCountry ORDER BY SUM(i.Total) DESC) as seqnum
FROM Customer c JOIN
Invoice i
ON c.CustomerId = i.CustomerId
GROUP BY c.CustomerId, c.FirstName, c.LastName, i.BillingCountry
) c
WHERE seqnum = 1
ORDER BY Country;
This also fixes your GROUP BY clauses so they are consistent with the columns being selected.

how to preform a distinct on a column in the middle of the query

I'm trying to do a (distinct) or anything else that would do the same thing on the country column but it's not working this is the where I got stuck
with t1 as(select c.customerid cnumber , c.firstname fn, c.lastname ln , i.billingcountry country , sum(i.total) tot from
Invoice I join Customer c on
i.CustomerId=c.CustomerId
group by 1,2,3,4
order by 4)
select distinct cnumber, fn, ln, country, max( tot) from t1
group by 1,2,3,4
order by 4;
WITH t1 AS
(
SELECT c.Country, SUM(i.Total) TotalSpent, c.FirstName, c.LastName, c.CustomerId
FROM
Customer c
JOIN Invoice i ON c.CustomerId = i.CustomerId
GROUP BY c.CustomerId
)
SELECT t1.*
FROM t1
JOIN(
SELECT Country, MAX(TotalSpent) AS MaxTotalSpent, FirstName, LastName, CustomerId
FROM t1
GROUP BY Country
)t2
ON t1.Country = t2.Country
WHERE t1.TotalSpent = t2.MaxTotalSpent
ORDER BY Country;

Count row and get latest row by date from multiple tables

I have 2 tables, Customer and CustomerActivity as showed in the picture below:
I want to output a table that:
has all columns from Customer table where CustomerType = 'Existing Customer', plus 2 more columns:
totalActivity (count activityID) - shows total activity number of each customer.
latestActivity (max checkinTime) - shows the most recent activity datetime
So far I have these 2 queries but I don't know how to combine/join and filter them to get what I need. Anyone can help with 1 query (and some explanation would be perfect)
SELECT customerId, firstName, birthDate, customerType
FROM Customer
WHERE Customer.customerType = 'Existing Customer'
SELECT t1.activityId, t1.checkinTime, t1.customerId
FROM CustomerActivity t1
inner join (
SELECT customerId, max(checkinTime) as Lastest
FROM CustomerActivity
group by customerId
) t2 on t1.customerId = t2.customerId and t1.checkinTime = t2.Lastest
You're actually close. Here is what your query should look like:
SELECT
c.customerId,
c.firstName,
c.lastName,
c.birthDate,
c.customerType,
ca.totalActivity,
ca.latestActivity
FROM Customer c
INNER JOIN(
SELECT
customerId,
latestActivity = MAX(checkinTime),
totalActivity = COUNT(*)
FROM CustomerActivity
GROUP BY customerId
) ca
ON ca.customerId = c.customerId
WHERE
c.customerType = 'Existing Customer'
The subquery (inside the INNER JOIN) retrieves the total number of activities by using COUNT(*) and latest activity using MAX(checkinTime) of each customer. After that, you would want to join it to the Customer table on customerId. You then add a WHERE clause to filter for 'Existing Customer' only.
I haven't tested it against an actual schema, but something like this should work (this approach will show customers even if they have no activity, simply change the left join to an inner join if you only want customers with activity):
SELECT c.CustomerID
, c.FirstName
, c.BirthDate
, c.CustomerType
, COUNT(ca.ActivityID) AS TotalActivity
, MAX(ca.CheckinTime) AS MostRecentActivity
FROM Customer c
LEFT JOIN CustomerActivity ca ON c.CustomerID = ca.CustomerID
WHERE c.CustomerType = 'Existing Customer'
GROUP BY c.CustomerID
, c.FirstName
, c.BirthDate
, c.CustomerType
You can get what you want without group by, by using row_number() and window fu instead:
SELECT c.*, ca.numActivities, ca.activityId as LastActivity
FROM Customer c JOIN
(select ca.*,
count(*) over (partition by ca.CustomerId) as numActivities
row_number() over (partition by ca.CustomerId order by checkinTime desc) as seqnum
from CustomerActivity ca
) ca
on c.customerId = ca.customerId and ca.seqnum = 1
WHERE c.customerType = 'Existing Customer';
This version will let you get whatever columns you like from the most recent activity row.
EDIT:
In your original question, I thought you wanted the latest activity. If you just want the latest datetime, then aggregation works:
SELECT c.*, ca.numActivities, ca.lastActivityDateTime
FROM Customer c JOIN
(select ca.*,
count(*) as numActivities
max(checkinTime) as lastActivityDateTime
from CustomerActivity ca
) ca
on c.customerId = ca.customerId
WHERE c.customerType = 'Existing Customer';
Select c.customerId, c.firstName, c.lastName, c.birthDate, c.customerType, gca.latestCheckIn, gca.count
from customer as c,
(select ca.customerId, max(ca.checkInTime) as latestCheckIn, count(*) as checkinCount
from customerActivity as ca
group by ca.customerId) as gca
where gca.customerId = c.customerId AND c.customerType = 'Existing Customer'
If you clarify more about customer with no activity, one can change the query to using left join