Access SQL- Count distinct months from Table 1 by City in Table 2 - sql

I have a table (Orders) featuring CustomerID, Product, Sales, and Date (yyyy-mm-dd).
CustomerID is linked to the Customer table at CustomerName. The Customer table has fields Customername, City, State, Country.
I would like to count the number of unique months for the calendar year which an ordered occurred by each City.
Unfortunately, so for I've only been able to count distinct months across all Customers and Dates. How can I tie it to each city in the Customer table?
SELECT Count(*) AS CountOfMonths
FROM (
SELECT DISTINCT Month(Date), Year(Date)
FROM Orders );
Thanks!

Something like this:
select country, city, state,
count(*) as NumDistinctMonths
from (select c.country, c.state, c.city,
year(o.date) as yyyy, month(o.date) as mm, count(*) as cnt
from orders as o inner join
customers as c
on o.CustomerID = c.CustomerId
group by c.country, c.state, c.city, year(o.date), month(o.date)
) as ccsym
group by country, city, state;

Simply join the tables and aggregate by year and city and count distinct months:
select c.country, c.state, c.city, year(o.date), count(distinct month(o.date))
from orders o
join customers c on c.customername = o.customerid
group by c.country, c.state, c.city, year(o.date)
order by c.country, c.state, c.city, year(o.date);
Add a WHERE clause, if you want to see one year only.
UPDATE: MS Access doesn't support COUNT(DISTINCT expresssion). So you need a distinct subquery there:
select c.country, c.state, c.city, order_year, count(*)
from
(
select distinct c.country, c.state, c.city, year(o.date) as order_year, month(o.date)
from orders o
join customers c on c.customername = o.customerid
) as dist_months
group by country, state, city, order_year
order by country, state, city, order_year;

Related

What customers who bought the biggest amount of a product in one order?

I have 2 tables:
CUSTOMERS (ID, FIRSTNAME, LASTNAME, ADDRESS);
ORDERS (ID, PRODUCT_NAME, PRODUCT_PRICE, DATE_ORDER DATE, ID_CUSTOMER, AMOUNT);
Get the first and last names of the customers who bought the biggest amount of a product in one order.
The orders without customer should not be considered. Please sort by FIRSTNAME and LASTNAME
SELECT firstname, lastname
FROM customers
LEFT JOIN orders ON customers.id = orders.id_customer
GROUP BY customers.firstname
HAVING (SELECT MAX(orders.amount) FROM orders);
What I'm doing wrong? thx
If you want one customer with the largest value of amount, then simplest method is order by and then fetching only one row:
select c.firstname, c.lastname
from customers c join
orders o
on c.id = o.id_customer
order by o.amount desc
fetch first 1 row only;
If you want the biggest amount per product, then window functions are the right approach
select pc.*
from (select o.product, c.firstname, c.lastname, o.amount,
rank() over (partition by o.product order by o.amount desc) as seqnum
from customers c join
orders o
on c.id = o.id_customer
) pc
where seqnum = 1;

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 get two ties of maximum values in my query?

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;

SQL Query to show highest sales in a City for EACH State

SELECT c.STATE, c.CITY, SUM(s.QTY * s.SALEPRICE)
FROM DWSALE s
INNER JOIN DWCUST c
ON s.dwcustid = c.dwcustid
GROUP BY c.STATE, c.CITY
ORDER BY c.STATE;
This current query above shows the TOTAL (SUM) amount of sales made (sale = qty * saleprice) for each city while also showing the state it resides in.
Clients (found in DWCUST) live in Cities and States.
Sales are recorded in DWSALE and hold the sales info.
I need the query to show the cities with the HIGHEST sales for EACH of their respective states.
I am not sure what to do.
Any help is appreciated!
You can use ROW_NUMBER() or RANK() to get what you want:
SELECT state, city, total
FROM (SELECT c.STATE, c.CITY, SUM(s.QTY * s.SALEPRICE) as total,
ROW_NUMBER() OVER (PARTITION BY c.STATE ORDER BY SUM(s.QTY * s.SALEPRICE)) as seqnum
FROM DWSALE s INNER JOIN
DWCUST c
ON s.dwcustid = c.dwcustid
GROUP BY c.STATE, c.CITY
) sc
WHERE seqnum = 1
ORDER BY STATE;
If you want ties (multiple rows for the same state), then use RANK() instead of ROW_NUMBER().
with statecitytotal as
(SELECT c.STATE, c.CITY, SUM(s.QTY * s.SALEPRICE) total
FROM DWSALE s
INNER JOIN DWCUST c
ON s.dwcustid = c.dwcustid
GROUP BY c.STATE, c.CITY
ORDER BY c.STATE)
select * from (select state, city, total, rank() over(partition by state order by total desc) rnk from statecitytotal) where rnk=1
but, if total for 2 cities is the same then both are shown