I need to count the right amount of discount with the right country, how do i do this? - tsql

this is the code i have atm:
SELECT Discount,
country,
count(country) AS total
FROM OrderDetails,
Customers
WHERE discount != 0
GROUP BY Discount,
country
HAVING ( count(country) > 0 )
ORDER BY discount DESC
what i need to do now, and can't find anywhere, is to have the count(country) add up with the right Discount numbers. That way i know how much discount each country has gotten.
I have looked everywhere and i can't find anything. Even in my own papers i got from school.
so this is the code with the join, so it's a little more clear to look at:
select Discount, country, count(country) as total
from Orders O
join Customers C
on C.CustomerID = O.CustomerID
join OrderDetails OD
on OD.OrderID = O.OrderID
where discount != 0
group by Discount, country
having (count(country)>0)
order by discount DESC

select c.country, sum(od.discount) as totalDiscount
from orders o
inner join customers c
on c.customerID = o.customerID
inner join orderDetails od
on od.orderID = o.orderID
where od.discount != 0
group by c.country
order by sum(od.discount) desc

Related

How i can show sums and count of orders for domestic orders and non domastic orders separetly for each customers country?

I have 2 requests and want to combine them into 5 columns like Country_name - count_of_orders_for_domestic - sums_for_domectic - count_of_orders_for_non_domestic - sums_for_non_domestic
domestic count of orders and sums
SELECT co.name country,
count(cu.id) domestic_orders,
sum(o.price) domastic_sum
FROM countries co
JOIN cities c ON co.id=c.country_id
JOIN customers cu ON c.id=cu.city_id
JOIN orders o ON cu.id=o.customer_id
JOIN products AS p ON o.product_id=p.id
WHERE p.country_id=co.id
GROUP BY co.name
non domestic count of orders and sums
SELECT co1.name country,
count(cu1.id) non_domestic_orders,
sum(o1.price) non_domastic_sum
FROM countries co1
JOIN cities c1 ON co1.id=c1.country_id
JOIN customers cu1 ON c1.id=cu1.city_id
JOIN orders o1 ON cu1.id=o1.customer_id
JOIN products AS p1 ON o1.product_id=p1.id
WHERE p1.country_id<>co1.id
GROUP BY co1.name
I try Union but it makes 3 columns and I cant see the difference between domestic and nondomestic. Then I try to make a new alias but I have a problem with count and sums operations.
You can use filtered aggregation:
SELECT co.name as country,
count(cu.id) filter (where p.country_id = co.id) as domestic_orders,
sum(o.price) filter (where p.country_id = co.id) as domastic_sum,
count(cu.id) filter (where p.country_id <> co.id) as non_domestic_orders,
sum(o.price) filter (where p.country_id <> co.id) as non_domastic_sum
FROM countries co
JOIN cities c ON co.id=c.country_id
JOIN customers cu ON c.id=cu.city_id
JOIN orders o ON cu.id=o.customer_id
JOIN products AS p ON o.product_id=p.id
GROUP BY co.name

Subqueries and Combining Queries together

I have a problem I've been working on. I've broken it down to a couple of steps below. I have trouble combining all the queries together to solve the following:
Find members who have spent over $1000 in departments that have
brought in more than $10000 total ordered by the members' id.
Schema:
departments(id, name)
products (id, name, price)
members(id, name, number, email, city, street_name, street_address)
sales(id, department_id, product_id, member_id, transaction_date
Step 1)
I found the departments that have brought in more than 10,000$
select s.department_id
from sales s join products p on
s.product_id = p.id
group by s.department_id
having sum(price) > '10000'
Step 2) I found the members and the departments that they shop in
select *
from members m
join sales s
on m.id = s.member_id
join departments d
on d.id = s.department_id
Step 3) I combined 1 and 2 to find members taht shop in departments that have brought in more than 10,000
select *
from members m
join sales s
on m.id = s.member_id
join departments d
on d.id = s.department_id
where s.department_id in
(select s.department_id
from sales s join products p on
s.product_id = p.id
group by s.department_id
having sum(price) > '10000')
Step 4) I found members and their id, email, total_spending > 1,000$
select m.id, m.name, m.email, sum(price) as total_spending
from members m join sales s on
m.id = s.member_id
join products p on
p.id = s.product_id
group by m.id
having sum(price) > '1000'
Step 5)
All of the steps work individually but when I put them together in my attempt:
select m.id, m.name, m.email, sum(price) as total_spending
from members m join sales s on
m.id = s.member_id
join products p on
p.id = s.product_id
where m.id in (select distinct m.id
from members m
join sales s
on m.id = s.member_id
join departments d
on d.id = s.department_id
where s.department_id in
(select s.department_id
from sales s join products p on
s.product_id = p.id
group by s.department_id
having sum(price) > '10000'))
group by m.id
having sum(price) > '1000'
The output is wrong. (This is on CodeWars) If someone could point me in the right direction that would be really great! Thank you.
Try to group by member_id and department_id:
select s.member_id,s.department_id,sum(p.price) as total_spending
from members m
join sales s on m.id = s.member_id
join products p on p.id = s.product_id
where s.department_id in (
select s.department_id
from sales s
join products p on s.product_id = p.id
group by s.department_id
having sum(p.price) > 10000 -- the departments which brought in more than $10000 total
)
group by s.member_id,s.department_id
having sum(p.price) > 1000 -- who have spent over $1000 in one department
And if you need you will able to calc how much spent each of members:
select member_id,sum(total_spending) total
from
(
-- the first query is here
) q
group by member_id

Group by with left join

I need to to sum (quantity and amount) for each product sold in in the table "account_invoice_line"
SELECT seller.name
ipartner.name AS invoice
brand.name AS brand, pt.name AS product,
SUM(ail.quantity) AS quantity, SUM(ail.price_subtotal) as amount
FROM account_invoice_line ail
LEFT JOIN account_invoice i ON ail.invoice_id = i.id
LEFT JOIN res_partner AS ipartner on ipartner.id = i.partner_id
LEFT JOIN res_users u ON u.id = i.user_id
LEFT JOIN res_partner AS seller ON seller.id = u.partner_id
LEFT JOIN product_product pp ON pp.id = ail.product_id
LEFT JOIN product_template pt ON pt.id = pp.product_tmpl_id
LEFT JOIN brand ON brand.id = pp.brand_id
GROUP BY seller.name, invoice, brand, pt.name
ORDER BY seller.name, invoice, brand, pc.name, pt.name
Unfortunately, I get this king of result:
-
Seller, Invoice, Brand, Product, Quantity, Amount
...
Seller1, Customer1, Brand1, Product1, 5, 4.70
Seller1, Customer1, Brand1, Product1, 10, 9.30
...
I was expecting for this (summed quantity and amount for the same product):
Seller1, Customer1, Brand1, Product1, 15, 14.0
Are you sure you need left joins for all of the tables?
Having account_invoice_line and then add LEFT JOIN, could make that one of the fields is NULL on the other tables, and that might be a reason why you see 2 records instead of one (let's say partner id is NULL in one of them).
If u just group by one column name only (for example: brand), do u still get same result?

Postgres: Getting a total related count based on a condition from a related table

My sql-fu is not strong, and I'm sure I'm missing something simple in trying to get this working. I have a fairly standard group of tables:
users
-----
id
name
carts
-----
id
user_id
purchased_at
line_items
----------
id
cart_id
product_id
products
--------
id
permalink
I want to get a total count of purchased carts for each user, if that user has purchased a particular product. That is: if at least one of their purchased carts has a product with a particular permalink, I'd like a count of the total number of purchased carts, regardless of their contents.
The definition a purchased cart is when carts.purchased_at is not null.
select
u.id,
count(c2.*) as purchased_carts
from users u
inner join carts c on u.id = c.user_id
inner join line_items li on c.id = li.cart_id
inner join products p on p.id = li.product_id
left join carts c2 on u.id = c2.user_id
where
c.purchased_at is not NULL
and
c2.purchased_at is not NULL
and
p.permalink = 'product-name'
group by 1
order by 2 desc
The numbers that are coming up for purchased_carts are strangely high, possibly related to the total number of line items multiplied by the number of carts? Maybe? I'm pretty stumped at the result. Any help would be greatly appreciated.
This ought to help:
select u.id,
count(*)
from users u join
carts c on c.user_id = u.id
where c.purchased_at is not NULL and
exists (
select null
from carts c2
join line_items l on l.cart_id = c2.id
join products p on p.id = l.product_id
where c2.user_id = u.id and
c2.purchased_at is not NULL
p.permalink = 'product-name')
group by u.id
order by count(*) desc;
The exists predicate is a semi-join.
bool_or is what you need
select
u.id,
count(distinct c.id) as purchased_carts
from
users u
inner join
carts c on u.id = c.user_id
inner join
line_items li on c.id = li.cart_id
inner join
products p on p.id = li.product_id
where c.purchased_at is not NULL
group by u.id
having bool_or (p.permalink = 'product-name')
order by 2 desc

How to get the Customer Detail + whether he has (an) order or not

I have 2 tables. Customers and Orders.
My requirement is...
I would like to get the result like the following
Customer Detail + HasOrders + Count(Orders)
I wrote
SELECT Customers.*
, CASE WHEN o.CustomerID IS NOT NULL THEN 1 ELSE 0 END HasOrders
FROM Customers c
LEFT JOIN Orders o
ON c.CustomerID = o.CustomersID
But it returns many rows. If the customer has 5 orders, it returns 5 rows for each Customer.
Could you please advise me? Thanks.
You need to do the counting in derived table.
SELECT c.*
, case when o.CustomerID is not null
then 1
else 0
end HasOrders
, o.NumberOfOrders
FROM Customers c
LEFT JOIN
(
SELECT CustomerID
, count(*) NumberOfOrders
FROM Orders
GROUP BY CustomerID
) o
ON c.CustomerID = o.CustomersID