T-SQL filter on external join - tsql

Using the schema below, write a query which lists the total value of all sales for customers who bought an item from a sales person with the same middle initial as them. Your query should return a single result.
The quantity and price columns are both ints.
The Sales - Employee link is on EmployeeID - SalesPersonID.

Something like this should work:
select sum(s.Quantity * p.Price) as TotalSales
from Sales s
join Products p on s.ProductID = p.ProductID
join Customers c on s.CustomerID = c.CustomerID
join Empleyees e on s.SalesPersonID = e.EmployeeID
where c.MiddleInitial = e.MiddleInitial

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

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?

Postgresql-Select one row from table where value in many table matches?

I have two tables, call them a and b, where a is related to b in a one-to-many relationship. I would like to select any rows from table a where any of the many related records in table b match a criteria. A basic join doesn't work, because that will return one result for each row in table b that matches - I just want one result for each row in table a with one or more related records matching.
For simplified example, say I have a table Departments and related table Employees, where each employee has one department, but each department obviously can have multiple employees. I want a query that will give me one row per department that has one or more employees matching a given criteria - say the departments that have one or more employees that have earned "employee of the month". How would I do this? Thanks.
SELECT * FROM department d
WHERE EXISTS (
SELECT * FROM employee e
JOIN badges b ON b.person_id = e.person_id AND b.badge = 'EotM'
WHERE e.dep_id = d.dep_id
AND e.gender = 'F'
);
select distinct on (d.id)
d.name
from
department d
inner join
employee e on d.id = e.department_id
where e.age between 60 and 65
How to order it by any column:
select *
from (
select distinct on (d.id)
d.*
from
department d
inner join
employee e on d.id = e.department_id
where e.age between 60 and 65
) s
order by name
Sounds like a job for a subquery. Something like: Select * from dept where id in (select deptID from Emp where wasEOTM = true); ought to do the job.

GROUP and SUM in Entity Framework

I want to select sum of all (paid) prices of an order item for each customer.
Here is SQL command:
SELECT c.name,SUM(oi.price * oi.count) from customer c
JOIN order o ON c.id=o.customer_id
JOIN order_item oi ON o.id=oi.order_id
JOIN bill b ON b.id=oi.bill_id
WHERE b.payment_id is NOT null
GROUP by c.name;
I don't know how to do this in EF.
Example result:
John Smith 1500,2
Allan Babel 202,0
Tina Crown 3500,78
(comma is used as decimal point..because price is decimal value)
Your example result doesn't seem to match your SQL command, but i think you are looking for something like this:
var query = from c in context.Customers
join o in context.Orders on c.id equals o.customer_id
join oi in context.OrderItems on o.id equals oi.order_id
join b in context.bill on oi.bill_id equals b.id
where b.payment_id != null
group oi by c.name into g
select new
{
Name = g.Key,
Sum = g.Sum(oi => oi.price * oi.count),
}

Using a subquery in an aggregate expression

Instead of this:
SELECT Customer,SUM(OrderPrice) FROM Orders
GROUP BY Customer
How do I get something like above but by producing a COUNT from a subquery.
SELECT Customer,Count(select * from Orders where o.idUser = u.idUser) FROM Orders o
inner join Users u on u.idOrder = o.idOrder
GROUP BY Customer
thx
This should do, I see no need to include Orders in the Topquery:
SELECT u.Customer, (SELECT COUNT(*) FROM Orders o WHERE o.idUser = u.idUser)
FROM Users u