Goal. To make a query that displays Product Name(Products), Product Type(ProductTypes), and total number of sales of each product(Sales)
Here are my tables:
I am having real difficulty figuring out how I am meant to do this. I am trying to do a UNION and a few other things but cannot get it to work.
I can get the total number of sales by using this SELECT ProductID, count(*) as NumSales from Sales group by ProductID but really struggling to do the rest and format it correctly. Any help would be appreciated.
EDIT:
Select Products.ProductName, ProductTypes.ProductType
From Products
INNER JOIN ProductTypes ON Products.ProductTypeID=ProductTypes.ProductTypeID
I have this to display this right now, just need to join the sales count somehow.
try:
select prod.ProductName, ptyp.ProductType, count(SaleID) count_sale
from Products prod
join ProductTypes ptyp on ( ptyp.ProductTypeID= prod.ProductTypeID)
join Sales sal on ( sal.ProductID = prod.ProductID)
group by prod.ProductName, ptyp.ProductType
Related
I need to identify products that have purchased more than 1 time.
ERD diagram looks like this:
I wrote this query
SELECT DISTINCT good_name
FROM Goods
JOIN Payments
on Payments.good = Goods.good_id
WHERE good in (SELECT good
FROM (SELECT good
, COUNT(good) as c
FROM Payments
GROUP
BY good) as a
WHERE c > 1)
It works, but is this code great?
Grouping would work better:
SELECT good_name
FROM Goods
JOIN Payments on Payments.good = Goods.good_id
GROUP BY Goods.good_id
HAVING COUNT(Payments.good) > 1
Probably you also need an index over Payments.good column.
It is also better to create another column in the table Goods which will hold success payments count and update it after each payment.
I'm trying to merge these two statements into one query to get the a list of product names(or ids) against the average of their TTFF data, and I'm stuck.
select AVG(TTFF) from TTFFdata group by product_id
select product.product_name, count(*) from product join TTFFdata on product.product_id = TTFFdata.product_id
I've looked into using a temporary table (CREATE TEMPORARY TABLE IF NOT EXISTS averages AS (select AVG(TTFF) from TTFFdata group by product_id)) but couldn't get that to work with a join.
Anyone able to help me please?
You need to understand the components. Your second query is missing a group by. This would seem to be what you want:
select p.product_name, count(t.product_id), avg(t.TTFF)
from product p left join
TTFFdata t
on p.product_id = t.product_id
group by p.product_name
It is better to do group by on product_id, product_name for two reasons. One is, you can select product id along with product name. Second reason is, If the product name is not unique then it may give wrong results(this may be a rare scenario like product name is same but it differs based on other columns like version or model). The below is the final query.
select Product.product_id,
product_name,
AVG(TTFF) as Avg_TTFF
from Product
inner join
TTFFdata
on Product.product_id = TTFFdata.product_id
group by Product.product_id,Product.product_name
TTFFdata:
product:
Output:
Currently I have the following 4 tables: customer, customer_orders_product, customer_order and customer
What I am trying to do is to run a query that could show the following columns:
order_id, product_name, quantity and order total (which is quantity *
product_price)
But I am not sure if there is any query that is capable to do so, any help on this?
Furthermore, is there any query with JOIN syntax that I could run with those 4 tables?
You just join each pair of tables according to the fields that reference each other:
SELECT co.order_id,
p.product_name,
cop.quantity,
cop.quantity * p.product_price AS total
FROM customer_order co
JOIN customer_order_product cop ON co.order_id = cop.order_id
JOIN product p ON cop.product_id = p.product_id
Incidentally, you don't need all four tables for this query - the customer details are irrelevant for your question.
I have 3 tables:
products (sku, price, priceOffer, etc)
stock (sku, branch, items)
sales_provider (sku, items, date)
The table products holds all the information about a product, except for stock or sales. Current stock is stored in the table stock, and has information about how many items are available on each branch. The table "sales_provider" stores how many items were sold each day for every product. The product ID is "sku".
Now, I'm trying to get with one query the product that:
Has generated the best profit (number of sales * offered price)
Is still available on stock
And, of course, I want to know how many items are still on stock and how many items were sold.
I'm trying a query like this:
select
*
from
(
select
p.*,
sum(s.items) stock,
sum(sp.items) sales,
case when
p.priceOffer < p.price
and
p.priceOffer > 0
then
p.priceOffer
else
p.price
end finalPrice
from
products p
join
stock s
on
s.sku = p.sku
join
sales_provider sp
on
sp.sku = p.sku
group by
sku
) temp
where
stock > 0
order by
(finalPrice * sales) desc
limit 1;
But I'm having problems with that. Basically, I'm getting a huge sum of stock items ans sales_provider items, not the real amounts. Also, it's a slow query (it's taking about half a second with only 9,500 products).
I've been trying to modify it and I'm having doubts about the subquery being necessary, but I just can't nail it.
If someone can help me improve it and get the correct result, I'll really appreciate it.
Thanks in advance for any helpful comment.
Francisco
For this type of query, you want to do the aggregations separately on stock and sales_provider. Otherwise, you will generate a cartesian product between the two tables for a given item.
Try this:
select p.sku, (salesitems*offeredprice) as profit, stockitems, salesitems
from products p left join
(select sku, SUM(items) as stockitems
from stock
group by sku
) s
on p.sku = s.sku left join
(select sku, SUM(items) as salesitems
from sales_provider sp
group by sku
) sp
on p.sku = sp.sku
where p.stockitems > 0
order by profit desc
This assumes that product(sku) is unique.
I have 3 tables:
Orders
- id
- customer_id
Details
- id
- order_id
- product_id
- ordered_qty
Parcels
- id
- detail_id
- batch_code
- picked_qty
Orders have multiple Details rows, a detail row per product.
A detail row has multiple parcels, as 10'000 ordered qty may come from 6 different batches, so goods from batches are packed and shipped separately. The picked quantity put in each parcel for a detail row should then be the same as the ordered_qty.
... hope that makes sense.
Im struggling to write a query to provide summary information of all of this.
I need to Group By customer_id to provide a row of data per customer.
That row should contain
Their total number of orders
Their total ordered_qty of goods across all orders
Their total picked_qty of goods across all orders
I can get the first one with:
SELECT customer_id, COUNT(*) as number_of_orders
FROM Orders
GROUP BY Orders.customer_id
But when I LEFT JOIN the other two tables and add the
SELECT ..... SUM(Details.ordered_qty) AS total_qty_ordered,
SUM(Parcels.picked_qty) AS total_qty_picked
.. then I get results that dont seem to add up for the quantities, and the COUNT(*) seems to include the additional lines from the JOIN which obviously then isn't giving me the number of Orders anymore.
Not sure what to try next.
===== EDIT =======
Here's the query I tried:
SELECT
customer_id,
COUNT(*) as number_of_orders,
SUM(Details.ordered_qty) AS total_qty_ordered,
SUM(Parcels.picked_qty) AS total_qty_picked
FROM Orders
LEFT JOIN Details ON Details.order_id=Order.id
LEFT JOIN Parcels ON Parcels.detail_id=Detail.id
GROUP BY Orders.customer_id
try COUNT(distinct Orders.order_id) as number_of_orders,
as in
SELECT
customer_id,
COUNT(distinct Orders.order_id) as number_of_orders,
SUM(Details.ordered_qty) AS total_qty_ordered,
(select SUM(Parcels.picked_qty)
FROM Parcels WHERE Parcels.detail_id=Detail.id ) AS total_qty_picked
FROM Orders
LEFT JOIN Details ON Details.order_id=Order.id
GROUP BY Orders.customer_id
EDIT: added an other select with subselect
Is there any particular reason you feel the need to combine all these in one query? Simplify by breaking it up in to separate queries, and if you want a single call to get the results, put the queries in a stored procedure, using temp tables.