I have two tables and I want to find which orders have two rows in the line_items table and also where one of those rows has the sku of BALL. Basically trying to find all the customers who ordered only a ball, regardless of the qty of the BALL row. There needs to be two rows per order at least, because one of the rows will always be for the SHIPPING sku.
In the below data, john and sam would be the only valid orders returned because the only skus their orders have in the line_items table are BALL and SHIPPING (regardless of BALL qty). I'd like the tables joined so that the returned data would have the order_id, customer, date_placed, and qty.
TABLE orders
id customer date_placed
===========================
0 john 1/1/2000
1 bill 2/1/2000
2 sam 2/5/2000
TABLE line_items
id order_id qty sku
=========================
0 0 1 BALL
1 0 1 SHIPPING
2 1 1 BALL
3 1 1 ROPE
4 1 1 SHIPPING
5 2 3 BALL
6 2 1 SHIPPING
Thank you so much!
If I understand correctly, you should be able to write it like this:
SELECT o.id, customer, date_placed, li.qty
FROM orders o
INNER JOIN line_items li
ON o.id=li.order_id AND li.sku='BALL'
WHERE EXISTS(
SELECT order_id
FROM line_items tli
WHERE o.id=tli.order_id
GROUP BY order_id
HAVING count(*)=2)
Related
I'm looking for help for one of my previous coding challenge, so I have two table, the first one is Sales and the second one is Product. Which look something like this :
Sales table:
ord_id item_id cust_id status qty total_price order_date
---------------------------------------------------------------------------------
1 A308 CS209 Sold 1 30000 10-10-21
2 A405 CS209 Cancel 4 44000 10-18-21
3 B476 CS890 Sold 8 6400 10-29-21
and Product table
item_id item_category item_price item_name date_created date_updated
----------------------------------------------------------------------------------------
A308 Electronic 30000 TV 08-10-21 10-10-21
A405 Furniture 11000 Chair 12-25-20 10-18-21
B476 Misc 800 Cutter 05-11-21 10-29-21
I was told to write a query to get a result set like this:
order_date detail total_customer total_price
-------------------------------------------------------------------------------------------
10-10-21 {"item_id":"A308","item_category":...} 1 30000
10-29-21 {"item_id":"B476","item_category":...} 1 6400
The other parts are easy but I'm still confused on how to turn certain columns from the Product table into JSON and inserting that json into the Detail column as shown above.
I've done something like this to get the first part :
SELECT order_date, COUNT(cust_id) as total_customer, SUM(total_price) as total_price
FROM Sales
WHERE status = "sold"
GROUP BY order_date
Can anyone help me how to do this in a single query?
I've two tables 'orders' and 'assigned_orders'. When I create an order it get entered in orders table with certain quantity. I assign this orders to different vendors with some quantity. so let say in orders table I've 500 quantity in total where I'm assigning 300 quantity to vendor 'A' and 100 quantity to vendor 'B'. Now, I'm left with 100 quantity unassigned.
Now I want to get a record from orders and assigned_orders table which will show like:
Orders
id
customer_name
quantity
1
cust 1
500
2
cust 2
700
assigned_orders
id
order_id
assigned_quantity
1
1
300
2
1
100
Expected result
id
order_id
quantity
assigned_quantity
1
1
500
400
use join and aggregation
select o.id,ao.order_id,
sum(ao.assigned_quantity) as assigned_quantity,
sum(o.quantity) as quantity
from assigned_orders ao join
Orders o on o.id=ao.order_id
group by o.order_id,o.id
I have two tables:
Table "products"
ID
Name
Price
Quantity
1
Product A
10
10
2
Product B
20
10
Table "promotions"
ID
Product ID
Type
Price
Quantity
1
1
discount
5
3
2
1
discount
8
2
3
1
outlet
10
3
4
2
outlet
10
0
From tables above I can see that:
I have 10 pieces of Product A with regular price 10$
I have 3 pieces of Product A with discounted price 5$
I have 2 pieces of Product A with discounted price 8$
I have 3 pieces of Product A with outlet price 3$
I have 10 pieces of Product B with regular price 20$
I have 0 pieces of Product B with outlet price 3$.
What is the most efficient way to get all combinations of available products?
My solution was with three UNION SQL querys, for example:
SELECT * FROM (
(<<all products in reular prices>>)
UNION ALL
(<<all products in discount prices>>)
UNION ALL
(<<all products in outlet prices>>)
) as tmp
Everything works fine, I am just wondering if this is the best way or is it better to use LEFT or RIGHT JOIN. Of course this is just an example, my tables have 20.000+ lines so I am looking for fastest way to list results.
One option could be:
first apply a UNION between "Products" and "Promotions" on matching columns
then JOIN back with "Products" to retrieve the products name
SELECT prices.ProductID,
Products.Name,
prices.Type,
prices.Price,
prices.Quantity
FROM (SELECT ID AS ProductID,
'regular' AS Type,
Price,
Quantity
FROM Products
UNION
SELECT ProductID,
Type,
Price,
Quantity
FROM Promotions ) prices
INNER JOIN Products
ON prices.ProductID = Products.ID
Check the demo here.
I am fairly new to SQL and I am struggling with generating a table with multiple aggregates based on certain filters.
My temporary able has 3 columns where an individual is unique by Category ID AND Customer ID (i.e. there can be multiple identical Customer IDs across categories).
Category ID
Customer ID
Number of Purchases
X
A
4
X
B
10
X
C
2
Y
A
2
Y
E
6
Z
A
10
Z
C
5
Z
D
7
The output table I am looking for is basically 4 columns: grouped by category ID identifying total number of customers who have at least 3 purchases, customers who have at least 5 purchases as well as total purchases across all customers within that category. The output table would look like this:
Category_ID
Total_Customers_3
Total_Customers_5
Total_Purchases
X
2
1
16
Y
1
1
8
Z
3
3
22
When I input the following code, I get the correct total purchases column per category but the total number of customers who had at least 3 purchases per category is incorrect as the aggregates are identical across all categories.
My code:
SELECT table.categoryID AS Category_ID
(SELECT COUNT (table.customerID)
FROM table
WHERE table.purchases >=3) AS Total_Customers_3,
(SELECT COUNT (table.customerID)
FROM table
WHERE table.purchases >=5) AS Total_Customers_5,
SUM (table.purchases) AS Total_Purchases,
FROM table
GROUP BY Category_ID, Total_Customers_3, Total_Customers_5,
(P.S When I try to just GROUP BY Category_ID, I get an ERROR that syntax is incorrect because I have sub-queries in my SELECT statement.
The incorrect table (i.e. same number of total customers meeting the condition across all categories) I get looks like:
Category_ID
Total_Customers_3
Total_Customers_5
Total_Purchases
X
2
1
16
Y
2
1
8
Z
2
1
22
Update: I should have included this in the original question but I am actually looking to generate 2 columns in the same about total number of customers based on 2 conditions - one for customers with at least 3 purchases and another column for customers with at least 5 customers.
Aggregate by category, and the use conditional aggregation to get the counts and totals.
SELECT
Category_ID,
SUM(purchases >= 3) Total_Customers_3,
SUM(CASE WHEN purchases >= 3 THEN purchases ELSE 0 END) AS Num_Purchases_3,
SUM(purchases >= 5) Total_Customers_5,
SUM(CASE WHEN purchases >= 5 THEN purchases ELSE 0 END) AS Num_Purchases_5
FROM yourTable
GROUP BY Category_ID;
Demo
I have 'products' table and related 'variations' table, one product can have one or more variations. 'variations' table has 'status' column, its value can be 0 or 1. I want to get the number of products (COUNT()) which have at least one variation of status 1. How to make a query that would do that?
[EDIT]
Ok, I thought that if I simplify the question I will get away with the table structure, but, here we go (only columns relevant to the question and some mock data):
It's actually 3 linked tables:
table 1: 'products'
id
name
1
t-shirt
2
shoes
3
shorts
table 2: variations
id
product_id
1
1
2
1
3
2
4
2
5
3
6
3
7
3
table 3: stock
variation_id
quantity
status [0 or 1]
1
10
1
2
15
1
3
0
0
4
0
0
5
0
0
6
3
1
7
0
0
So, with this data, I want to know how many products there are that have at least 1 of its 'variations' of 'status' 1 - in this example it would be 2 (product 1 and 3 have some variations with status 1, product 2 does not).
You just need SUM all the quantity GROUP BY products.id with criteria is stock.status equal 1.
SELECT id, name, SUM(quantity) AS total_quantity
FROM Products pr
LEFT JOIN Variations va ON pr.id = va.product_id
LEFT JOIN Stock st ON st.variation_id = va.id
WHERE st.status = 1
GROUP BY pr.id
Join two tables and apply where filter on status column
select count(*) as cnt
from
products p
join variations v
on p.product_id = v.product_id
where status = 1