Joining on a group by - sql

Lets say that I have three tables, customers, orders and orderDetails.
I'm doing this:
SELECT orders.ordersId, sum(orderDetails.total)
FROM orders
LEFT OUTER JOIN orderDetails ON orders.ordersId = orderDetails.ordersId
GROUP BY orders.ordersId
But lets say the orders table contains customersId. How do I join on the customers table so that I can also add the customer's name to the fields selected?
Thanks,
Barry

SELECT orders.ordersId, sum(orderDetails.total), customer.name
FROM orders
LEFT OUTER JOIN orderDetails ON orders.ordersId = orderDetails.ordersId
LEFT OUTER JOIN customer on customer.customerid = orders.customerid
GROUP BY orders.ordersId , customer.name
Try that out or something similar.

you can do it this way, which will let you get more than customer name if needed:
SELECT o.ordersId, o.orderTotal, c.customername, c.(other customer data)
FROM
(
SELECT orders.ordersId
, sum(orderDetails.total) as orderTotal
, orders.customersid
FROM orders
LEFT OUTER JOIN orderDetails
ON orders.ordersId = orderDetails.ordersId
GROUP BY orders.ordersId, orders.customersid
) o
LEFT JOIN customers c
ON o.customersid = c.customersid

Related

how to join 4 tables together in sql

I have customers, products, orders orderdetails 4 table.
their relationship looks like this.relashions
my SQL code looks like this:
select customers.CUSTOMERNAME, customers.STATE, customers.creditlimit
from customers
INNER JOIN ORDERS ON customers.customernumber = ORDERS.CUSTOMERNUMBER;
select ORDERDETAILS.PRICEEACH, ORDERDETAILS.QUANTITYORDERED
from ORDERDETAILS
inner join orders on ORDERS.ORDERNUMBER = ORDERDETAILS.ordernumber;
select products.productname
from products
inner join orderdetails on orderdetails.productcode = products.productcode;
I want to join the 4 tables into one table with selected segment. how can I do that?
you already figured out your answer. Add all 3 queries together to make one. Please be mindful about joins and sequence of joins.
SELECT
customers.CUSTOMERNAME, customers.STATE, customers.creditlimit ,
ORDERDETAILS.PRICEEACH, ORDERDETAILS.QUANTITYORDERED ,
products.productname
FROM ORDERS
INNER JOIN customers ON customers.customernumber = ORDERS.CUSTOMERNUMBER
inner join orderdetails on ORDERS.ORDERNUMBER = ORDERDETAILS.ordernumber
inner join products on products.productcode=orderdetails.productcode
You can do the following
SELECT c.CUSTOMERNAME
, o.CUSTOMERNUMBER
, od.PRICEEACH
, p.productname
FROM customers c
INNER JOIN orders o
on c.customernumber = o.customernumber
INNER JOIN ORDERDETAILS od
on o.ORDERNUMBER = od.ORDERNUMBER
INNER JOIN products p
on od.productcode = p.productcode
Here's the way to join 4 tables together--
SELECT CUST.CUSTOMERNAME
,CUST.STATE
,CUST.CREDITLIMIT
,DET.PRICEEACH,
,DET.QUANTITYORDERED
,PRD.PRODUCTNAME
FROM CUSTOMERS CUST
JOIN ORDERS ORD
ON CUST.CUSTOMERNUMBER = ORD.CUSTOMERNUMBER
JOIN ORDERDETAILS DET
ON ORD.ORDERNUMBER = DET.ORDERNUMBER
JOIN PRODUCTS PRD
ON DET.PRODUCTCODE = PRD.PRODUCTCODE;

T-SQL How to select all items without relationship in many-to-many situation?

Let's say there are 3 tables:
How can I show which CustomerId did not make an order for particular SupplierId
For example: SupplierId 2 did not get orders from CustomerId 1 and 5
So far my idea is to create a Table of all possible combinations of CustomerId/SupplierId and rows that have a match in the Orders table.
Is there a better way?
You can cross join the two referrential tables to generate all possible combinations, and use not exists to filter on those that do not exists in the bridge table:
select c.customerId, s.supplierId
from customers c
cross join suppliers s
where not exists (
select 1
from orders o
where o.customerId = c.customerId and o.supplierId = s.supplierId
)
You can also do this with an anti-left join:
select c.customerId, s.supplierId
from customers c
cross join suppliers s
left join orders o
on o.customerId = c.customerId and o.supplierId = s.supplierId
where o.customerId is null

How do we find which customers placed orders with items made in USA refer to image

Which customers placed orders with items made inside the USA?
SELECT DISTINCT, WHERE, Temporary Table, Subquery
tables to refer
I would use exists with a correlated subquery that follows the relationships like customer > order > order_item > product > supplier and filters on US suppliers:
select c.*
from customer c
where exists (
select 1
from order o
inner join order_item oi on oi.order_id = o.id
inner join product p on p.id = oi.product_id
inner join supplier s on s.id = p.supplier_id
where o.customer_id = c.id and s.country = 'USA'
)

Insufficient output from SQL query

I'm using the northwind db: http://dev.assets.neo4j.com.s3.amazonaws.com/wp-content/uploads/Northwind_diagram.jpg
I have to output all orders placed by CustomerID ALFKI with more than one unique product. I get the correct orders out, but I can't figure out why it's only printing one product name per order.
My query:
SELECT a.OrderID, p.ProductName
FROM Products p
INNER JOIN 'Order Details' a
ON (p.ProductID = a.ProductID)
INNER JOIN Orders b
ON (a.OrderID = b.OrderID)
WHERE (b.CustomerID = 'ALFKI')
GROUP BY a.OrderID
HAVING COUNT(DISTINCT a.ProductID) > 1
You need the GROUP BY and HAVING to be part of a subquery, with your primary query selecting the detail using the list of OrderIDs returned from the subquery as filter criteria. Try the following syntax for T-SQL:
SELECT
a.OrderID,
p.ProductName
FROM
Products p
INNER JOIN [Order Details] a
ON (p.ProductID = a.ProductID)
INNER JOIN Orders b
ON (a.OrderID = b.OrderID)
WHERE
a.OrderID IN
(
SELECT a.OrderID
FROM [Order Details] a
INNER JOIN Orders b
ON (a.OrderID = b.OrderID)
WHERE (b.CustomerID = 'ALFKI')
GROUP BY a.OrderID
HAVING COUNT(DISTINCT a.ProductID) > 1
)

SQL nested SELECT with JOIN

I wasted all the day on one query without success , SOS I need a help :) with a given #CustomerId , I need to query all the Products that linked to customer seller can sell but not sold to him before , the Commissions table is indication of what products seller can sell
Thanks in advance
SELECT sellableProduct
FROM (SELECT Comissions.ProductId AS sellableProduct, Sellers.SellerId AS sellableSeller FROM Comissions INNER JOIN Sellers ON Comissions.SellerId=Sellers.SellerId INNER JOIN Customers ON Sellers.SellerId=Customers.SellerId WHERE Customers.CustomerId = #customerid) AS tblSellable
LEFT JOIN (SELECT Sales.ProductId AS soldProduct, Customers.SellerId as soldSeller FROM Customers INNER JOIN Sales ON Customers.CustomerId=Sales.CustomerId WHERE Customers.CustomerId = #customerid) AS tblSold
ON tblSellable.sellableProduct=tblSold.soldProduct AND tblSellable.sellableSeller=tblSold.soldSeller
WHERE tblSold.soldProduct IS NULL
this way you avoid time-consuming IN statements
If a Customer can only have one Seller, then you can omit the seller link:
SELECT sellableProduct
FROM (SELECT Comissions.ProductId AS sellableProduct FROM Comissions INNER JOIN Sellers ON Comissions.SellerId=Sellers.SellerId INNER JOIN Customers ON Sellers.SellerId=Customers.SellerId WHERE Customers.CustomerId = #customerid) AS tblSellable
LEFT JOIN (SELECT Sales.ProductId AS soldProduct FROM Sales WHERE Sales.CustomerId = #customerid) AS tblSold
ON tblSellable.sellableProduct=tblSold.soldProduct
WHERE tblSold.soldProduct IS NULL
Basically, you're looking for products that have a record in commissions, but not in sales. Using :id to denote the specific ID:
SELECT *
FROM products
WHERE productid IN (SELECT productid
FROM commissions
WHERE sellerid = :id) AND
productid NOT IN (SELECT productid
FROM sales
JOIN customers ON sales.customerid = cusomers.customerid
WHERE sellerid = :id)
Would this work?
SELECT sell.*, prod.* FROM
Sellers sell
INNER JOIN Customers cust ON cust.SellerId = sell.SellerId
LEFT JOIN Commissions comm ON sell.SellerId = comm.SellerId
LEFT JOIN Products prod ON prod.ProductId = comm.ProductId
WHERE prod.ProductId NOT IN (
SELECT ProductId
FROM Products p INNER JOIN
Sales s ON s.ProductId = p.ProductId
WHERE s.CustomerId = #CustomerId
It get all the sellers and the respective product from commission, where the product Id is NOT associated with any sale of the client