select different data from several tables - sql

How could I select
| Customer_name | Num_of_AllTime_Orders | Num_of_different_Products_That_Customer_Ordered |
as a table when each data is from a different table?
I tried to count customers, join tables and group by... but no luck.
SELECT CompanyName, count(Customer_ID) from dbo.CUSTOMERS join dbo.ORDERS on CUSTOMERS.Customer_ID=ORDERS.CustomerID
Customers_Table:
CustomerID | Customer_Name
Orders_Table:
OrderID| Customer_ID
Order_Detail_Table:
OrderID| Product_ID |
I expect the output of a table which has these columns :
1. Customer_Name
2. Num_of_AllTime_Orders - how many orders that costumer made?
3. Num_of_different_Products_That_Customer_Ordered - how many different products that customer ordered?

Select CUSTOMERS.Customer_Name
,count(distinct ORDERS.OrderID) as Num_of_AllTime_Orders
,count(distinct Order_Detail_Table.Product_ID) as Num_of_different_Products_That_Customer_Ordered
from dbo.CUSTOMERS
join dbo.ORDERS on CUSTOMERS.CustomerID = ORDERS.Customer_ID
join dbo.Order_Detail_Table on ORDERS.OrderID = Order_Detail_Table.OrderID
group by CUSTOMERS.Customer_Name

SELECT Company_Name = C.CompanyName, NumOfOrders = COUNT (DISTINCT O.OrderID), NumOfProducts = COUNT (DISTINCT OD.ProductID)
FROM dbo.CUSTOMERS AS C JOIN ORDERS AS O ON C.Customer_ID=O.CustomerID JOIN ORDERDETAILS AS OD ON OD.OrderID=O.OrderID
GROUP BY C.CompanyName
ORDER BY 1

Related

Count between multiple tables

I have these 3 tables with the following structures:
Products
product_id | category_id | product_name
Customers
customer_id | customer_name
Orders
order_id | customer_id | product_id
I need to write a SQL query that will print the top customers regarding the number of categories they have bought products from (the best customer is the one that has bought from the most categories).
Can anyone show me how to do that?
I tried like this, but I get the following error "not a GROUP BY expression":
select
(select customer_name
from customers
where customers.customer_id = orders.customer_id) as name,
(select count(category_id)
from products
where products.product_id = orders.product_id)
from
orders
group by
customer_id
order by
count(customer_id) desc;
I managed to make the top regarding how many products the customers bought. The solution I used is:
select
(select customer_name from customers
where customers.customer_id = orders.customer_id) as name,
count(product_id) as "number of ordered products"
from
orders
group by
customer_id
order by
count(product_id) desc;
Nested queries for this? Sheesh...
Just join them already.
And then order by the unique categories
SELECT c.customer_name
, COUNT(DISTINCT p.category_id) AS TotalOrderedCategories
FROM Orders o
LEFT JOIN Customers c ON c.customer_id = o.customer_id
LEFT JOIN Products p ON p.product_id = o.product_id
GROUP BY c.customer_name
ORDER BY COUNT(DISTINCT p.category_id) DESC
Then depending on your RDBMS flavor, add a TOP or a LIMIT.

Distinct one column on two tables SQL Server 2008

I have two different tables and I want to use distinct on only one column. I want to get recent records only. what query should I write in SQL Server 2008? I want to use distinct one only CustomerID not any other column.
Table1: Customer
Columns: CustomerID, CustomerName
Table2: Order
Columns: OrderID, CustomerID, OrderName
I tried two SQL queries both not working
First query
select Distinct on (CustomerID) CustomerID, CustomerName, OrderID, OrderName
from Customer
left join Order on Customer.CustomerID = Order.CustomerID
Second query:
select Max(Distinct ID)
CustomerID, CustomerName, OrderID, OrderName
from
Customer
left join
Order on Customer.CustomerID = Order.CustomerID
Assuming the higher OrderID is latest, this should work if I understand your requirement righly:
select
c.CustomerID,
c.CustomerName,
o.OrderID,
o.OrderName
from Customer as c
inner join (select
CustomerID,
max(update_date) as max_update_date
from Customer
group by CustomerID) as mc
on mc.CustomerID = c.CustomerID
and mc.max_update_date = c.update_date
inner join Order as o
on o.CustomerID = c.CustomerID
inner join (select
CustomerID,
max(OrderID) as max_OrderID
from Order
group by CustomerID) as m
on m.CustomerID = c.CustomerID
and m.max_OrderID = o.OrderID

Sql Query for finding Orders having same order lines

How I can write a query that can find me orders having same order lines (details)?
Sample Data
Table: Order
ORDER_ID
--------
A
B
C
D
Table: OrderDetails
OrderID ProductID
------------------
A ProductX
A ProductY
A ProductZ
B ProductX
B ProductY
C ProductZ
D ProductX
D ProductY
D ProductZ
Now I want to pass ProductX,ProductY,ProductZ and get back A and D.
Can this be done in one query?
Maybe something like this is what you want?
SELECT DISTINCT Orders.OrderID
FROM Orders
INNER JOIN OrderDetails ON Orders.OrderID = OrderDetails.OrderID
WHERE OrderDetails.ProductID IN ('ProductX', 'ProductY', 'ProductZ')
GROUP BY Orders.OrderID
HAVING COUNT(*) = 3
Also note that Order is a reserved keyword and should not be used as a table name.
SELECT OrderId FROM
(SELECT DISTINCT o.OrderId, p.Product
FROM Orders o
INNER JOIN OrderDetails p
ON o.OrderId = p .OrderId
WHERE p.Product IN ('ProductX', 'ProductY', 'ProductZ') ) tab
GROUP BY OrderId
HAVING COUNT(*) = 3
I have done the required thing without using the Order table..
SELECT id from ProductT
group by id
having count(*)=3;
SQL Fiddle
Try this
SELECT P.OrderID
FROM Order1 P JOIN OrderDetails D ON
P.OrderID = D.OrderID
WHERE P.OrderID IN (SELECT OrderID FROM OrderDetails WHERE ProductID IN ('ProductX', 'ProductY', 'ProductZ'))
Having Count(P.OrderID)=3
GROUP BY P.OrderID
FIDDLE DEMO

Compare a table with a count result from another table and add the names that have a zero count

I am counting how many times a company has ordered. Then I am only showing if a company has ordered less than 5 times. I am then checking it against the table with all company names to see what company has not ordered, which would not show up on the order table, then add their name on the displayed list.
What I have tried:
Select Orders.CustomerID, Count(Orders.CustomerID) AS OrderCount
From Orders Left Join Customers
On Orders.CustomerID = Customers.CustomerID
Group By Orders.CustomerID
Having Count(Orders.CustomerID) <5
This is totally wrong:
Select CustomerID
From Customers
Where EXISTS
(Select CustomerID, Count(CustomerID) AS 'OrderCount'
From Orders
Group BY CustomerID
Having Count(Orders.CustomerID) < 5)
I need to somehow compare the list of names before I ask it to see which ones have ordered less than 5 times.
Thanks
If you want to use LEFT JOIN, interchange the table names since you want to show values from Customers, otherwise use RIGHT JOIN instead.
SELECT Customers.CustomerID,
COUNT(Orders.CustomerID) AS OrderCount
FROM Customers
LEFT JOIN Orders
ON Orders.CustomerID = Customers.CustomerID
GROUP BY Customers.CustomerID
HAVING COUNT(Orders.CustomerID) < 5
using EXISTS()
SELECT CustomerID
FROM Customers c
WHERE EXISTS
(
SELECT 1
FROM Orders o
WHERE o.CustomerID = c.CustomerID
GROUP BY CustomerID
HAVING COUNT(CustomerID) < 5
)
Try this:
SELECT C.CustomerID, C.CustomerName, COUNT(O.CustomerID) AS OrderCount
FROM Customers C
LEFT JOIN Orders O ON O.CustomerID = C.CustomerID
GROUP BY C.CustomerID
HAVING OrderCount < 5
ORDER BY OrderCount, C.CustomerName

SQL Query for counting number of orders per customer and Total Dollar amount

I have two tables
Order with columns:
OrderID,OrderDate,CID,EmployeeID
And OrderItem with columns:
OrderID,ItemID,Quantity,SalePrice
I need to return the CustomerID(CID), number of orders per customer, and each customers total amount for all orders.
So far I have two separate queries. One gives me the count of customer orders....
SELECT CID, Count(Order.OrderID) AS TotalOrders
FROM [Order]
Where CID = CID
GROUP BY CID
Order BY Count(Order.OrderID) DESC;
And the other gives me the total sales. I'm having trouble combining them...
SELECT CID, Sum(OrderItem.Quantity*OrderItem.SalePrice) AS TotalDollarAmount
FROM OrderItem, [Order]
WHERE OrderItem.OrderID = [Order].OrderID
GROUP BY CID
I'm doing this in Access 2010.
You would use COUNT(DISTINCT ...) in other SQL engines:
SELECT CID,
Count(DISTINCT O.OrderID) AS TotalOrders,
Sum(OI.Quantity*OI.SalePrice) AS TotalDollarAmount
FROM [Order] O
INNER JOIN [OrderItem] OI
ON O.OrderID = OI.OrderID
GROUP BY CID
Order BY Count(DISTINCT O.OrderID) DESC
Which Access unfortunately does not support. Instead you can first get the Order dollar amounts and then join them before figuring the order counts:
SELECT CID,
COUNT(Orders.OrderID) AS TotalOrders,
SUM(OrderAmounts.DollarAmount) AS TotalDollarAmount
FROM [Orders]
INNER JOIN (SELECT OrderID, Sum(Quantity*SalePrice) AS DollarAmount
FROM OrderItems GROUP BY OrderID) AS OrderAmounts
ON Orders.OrderID = OrderAmounts.OrderID
GROUP BY CID
ORDER BY Count(Orders.OrderID) DESC
If you need to include Customers that have orders with no items (unusual but possible), change INNER JOIN to LEFT OUTER JOIN.
Create a query which uses your 2 existing queries as subqueriers, and join the 2 subqueries on CID. Define your ORDER BY in the parent query instead of in a subquery.
SELECT
sub1.CID,
sub1.TotalOrders,
sub2.TotalDollarAmount
FROM
(
SELECT
CID,
Count(Order.OrderID) AS TotalOrders
FROM [Order]
GROUP BY CID
) AS sub1
INNER JOIN
(
SELECT
CID,
Sum(OrderItem.Quantity*OrderItem.SalePrice)
AS TotalDollarAmount
FROM OrderItem INNER JOIN [Order]
ON OrderItem.OrderID = [Order].OrderID
GROUP BY CID
) AS sub2
ON sub1.CID = sub2.CID
ORDER BY sub1.TotalOrders DESC;