get the number of similar items by year for each item - mysql

Please take a look at this fiddle:
I have this query to get the percentage difference of each item's price against the average price of their own category:
SELECT name, product.category, ((price - avg_price)/avg_price)*100 as price_difference
FROM product
JOIN (
SELECT category, AVG(price) as avg_price
FROM product
GROUP BY category
) t
ON t.category = product.category
Now I want to take a step further to add another query to count the number of products in the same category by year
Original table:
ID name category year
1 A Outdoor 2010
2 B Doll 2009
3 C Doll 2009
4 D Outdoor 2010
5 E Brainteaser 2010
6 F Brainteaser 2009
7 G Brainteaser 2009
8 H Brainteaser 2009
The outcome should be like this:
name CATEGORY DIFFERENCE similar_products_of_the_same_year
A Outdoor -72% 2
B Doll 29% 2
C Doll -29% 2
D Outdoor 9% 2
E Brainteaser -88% 1
F Brainteaser 134% 3
G Brainteaser 65% 3
H Brainteaser -47% 3
I can only make a specific query like the following but I need to get the records for each product.
SELECT COUNT(year) AS similar_items_of_the_year from `product` where year in (
SELECT year
FROM product
where name = "A"
)
Would anyone please show me how to get that outcome?

SELECT p.name
, p.year
, p.category
, ((p.price - avg_price)/avg_price)*100 price_difference
, ttl
FROM product p
JOIN
( SELECT category
, AVG(price) avg_price
FROM product
GROUP
BY category
) t
ON t.category = p.category
JOIN
( SELECT category,year, COUNT(*) ttl FROM product GROUP BY category,year ) n
ON n.year = p.year
AND n.category = p.category;
http://sqlfiddle.com/#!2/f182e/14
If my result differs from yours, I think that's because I don't understand whether or not you consider a row as being similar to itself. If you do, just subtract 1 from ttl.

Related

Aggregate information from one table to another with a different “layout” (mysql)

this is my starting table which provides sales information by Id.
Id
Store_Name
Market
Sales
Main_Product
1
StoreA
Rome
10
a
2
StoreB
Rome
15
b
3
StoreC
Rome
9
c
4
Mag1
Paris
10
a
5
Mag2
Paris
23
b
6
Mag3
Paris
12
c
7
Shop1
London
11
a
8
Shop2
London
31
b
9
Shop3
London
45
c
10
Shop4
London
63
d
In order to build a report and create some dynamic sentences, I will need the dataset to be "paginated" as per below table:
Id
Dimension
Dimension_Name
Sales
Main_Product
1
ShoppingCentre
StoreA
10
a
1
Market
Rome
34
a
2
ShoppingCentre
StoreB
15
b
2
Maket
Rome
34
b
3
ShoppingCentre
StoreC
9
c
3
Market
Rome
34
c
Do you have any tip about how to build the last table starting from the first one?
To sum-up:
The new table will be always by Id
Aggregation of market sales happens at row level where every single shopping centre is located
This is the query that I have built so far but wondering if there is a better and more efficient way to accomplish the same:
with store_temp_table as (
select
id
,Store_Name
,Market
, Main_Product
, sum(Sales) as Sales
from Production_Table
where 1=1
group by
1,2,3,4
)
, market_temp_table as (
select
market
, sum(Sales) as Sales
from Production_Table
where 1=1
group by
1
)
, store_temp_table_refined as(
Select
a.id
,a.Main_Product
, 'ShoppingCentre' as Dimension_Name
,SUM(a.Sales) as Sales
FROM store_temp_table a INNER JOIN
market_temp_table b on a.market = b.market
group by
1,2,3
)
, market_temp_table_refined as (
Select
a.id
,a.Main_Product
, 'Market' as DimensionName
,SUM(b.Sales) as Sales
FROM store_temp_table a INNER JOIN
market_temp_table b on a.market = b.market
group by
1,2,3
)
select * from store_temp_table_refined
union all
select * from market_temp_table_refined
Thank you
Use a CTE that returns the dimensions that you want and cross join it to a query that returns the columns of the table and an additional column with the total sales of each market:
WITH Dimensions(id, Dimension) AS (VALUES
ROW(1, 'ShoppingCentre'),
ROW(2, 'Market')
)
SELECT p.Id,
d.Dimension,
CASE d.id WHEN 1 THEN p.Store_Name ELSE p.Market END Dimension_Name,
CASE d.id WHEN 1 THEN p.Sales ELSE p.MarketSales END Sales,
p.Main_Product
FROM Dimensions d
CROSS JOIN (SELECT *, SUM(Sales) OVER (PARTITION BY Market) AS MarketSales FROM Production_Table) p
ORDER BY p.id, d.id;
Or, with UNION ALL:
SELECT Id,
'ShoppingCentre' Dimension,
Store_Name Dimension_Name,
Sales,
Main_Product
FROM Production_Table
UNION ALL
SELECT Id,
'Market',
Market,
SUM(Sales) OVER (PARTITION BY Market),
Main_Product
FROM Production_Table
ORDER BY Id,
CASE Dimension WHEN 'ShoppingCentre' THEN 1 WHEN 'Market' THEN 2 END;
See the demo.

SQL task if the product has no sales in the given month, then display 0 in this month

I have to build and SQL query which must do these things:
select all products from table "products" - satisfied
SUM all sales and forecast to the next 3 months - satisfied
check if the product has no one sale, then write "0" -> here is the problem, because I don't know how to do that..
My SQL query is here..
select product.name,
(select sum(amount)
from forecast
where forecast.product_id = product.id),
sum(sale.amount)
from product join
 sale
  on sale.product_id = product.id
where sale.outlook > -4
group by product.id
Here is the products table:
id name
1 milk
2 roll
3 ham
Table sale (same structure like forecast):
product_id outlook amount
1 -1 9
1 -2 13
1 -3 14
2 -1 88
2 -3 61
3 -1 33
3 -4 16
You can use left join to bring in the rows and coalesce() to get the 0 instead of NULL:
select p.name,
(select sum(f.amount)
from forecast f
where v.product_id = p.id),
coalesce(sum(s.amount), 0)
from product p left join
sale s
on sale.product_id = product.id and
sale.outlook > -4
group by p.id
Understand the requirement to be, show the sales per product and if no sale for a product show "0". Tables are named Products and Sale.
For this, "with" statements are useful and help understanding too:
With SalesSummary as
(
select product_id, sum(amount) as ProductSales
from Sale
Group by product_id
)
select a.ProductID, ISNULL(b.ProductSales,0) as Sales
from products a left join SalesSummary b on a.product_id=b.product_id

SSRS top n rows in every value of a Group

I am new to SSRS. I have a requirement to display top 3 products based on sales for each country where country is a column grouping, in a tablix.So the report should look like this ( the countries should appear as columns with products and total sales underneath.Unfortunately I am unable to type the below as expected)
UK
Prod1 100
Prod3 70
Prod4 50
Spain
Prod2 80
Prod3 55
Prod4 30
Italy
Prod3 45
Prod1 20
Prod4 15
Top N filter applied at the column group does not work as it seems to be ignoring it completely. Top N cannot be applied at the Table level as it will only display Top 3 across all countries. The data is sourced from a cube.
Any help is much appreciated
This is something you should solve in your select query and that shouldn't be too hard.
I don't know your database layout so I'll try and explain it with a simple example:
Sales Table
SalesId
CustomerId
Date
Total
SalesDetails table
SalesDetailId
SalesId
Product
Quantity
CustomersTable
CustomerId
Country
Name
SELECT DISTINCT(c.Country), q.Product, q.Quantity
FROM Customers c
JOIN (SELECT c1.Country, d1.Product, SUM(d1.Quantity) as Quantity
FROM SalesDetails d1
JOIN Sales s1 ON s1.SalesId = d1.SalesId
JOIN Customer c1 ON c1.CustomerId = s1.CustomerId
GROUP BY d1.Product, c1.Country) q ON q.Country = c.Country
WHERE q.Product IN (SELECT TOP 3 d2.Product
FROM SalesDetails d2
JOIN Sales s2 ON s2.SalesId = d2.SalesId
JOIN Customer c2 ON c2.CustomerId = s2.CustomerId
WHERE c2.Country = c.Contry
GROUP BY d2.Product, c2.Country
ORDER BY SUM(d2.Quantity) DESC)
ORDER BY c.Country
I hope this is of use to you, if you share your actual database layout I'm willing to update my example based on yours.

How to find Profit for Category

My project is about a jewelery store and i try to find the profit of each product-category.
Let me be more specific
I have 3 tables which gives me the info:
SALES(salesid,productid,quantity,price)
salesid productid Quantity Price
11001 13001 4 5
11002 13002 6 10
11003 13003 5 16
.
.
11012 13012 7 15
RETURN(salesid,productid,date,quantity,price)
salesid productid Quantity Price
11003 13003 1 16
11007 13007 3 12
11008 13008 3 8
PROCUREMENT(procurementid,productid,quantity,price)
procurementid productid Quantity Price
100001 13001 10 2
100002 13002 10 2
.
.
100012 13012 10 2
product_category(categoryid,category)
categoryid category
1 Gold
2 Silver
.
5 Platin
product(Productid,categoryid)
Productid categoryid
13001 1
13002 3
.
.
13010 5
The profit is given from this type:
Profit=Quantity*Price(Sell)-Quantity*Price(Return)-Quantity*Price(Procurement)
And now here is the problem.. I came up to this so far
SELECT categoryid,
category,
(coalesce(a.rev,0)- coalesce(b.ret,0),
coalesce(c.cost,0)) AS profit
FROM product category AS g
JOIN product AS h ON g.categoryid = h.categoryid
JOIN
(SELECT categoryid,
sum(quantity*price) AS rev
FROM sales AS a,
product AS b
WHERE a.productid = b.productid
GROUP BY categoryid) a
LEFT OUTER JOIN
(SELECT cartegoryid,
sum(quantity*price) AS ret
FROM RETURN AS a ,
product AS b
WHERE a.productid = b.productid
GROUP BY categoryid) b ON a.categoryid = b.categoryid
LEFT OUTER JOIN
(SELECT categoryid,
sum(quantity*price) AS cost
FROM procurement AS a,
product AS b
WHERE a.productid = b.productid
GROUP BY categoryid) c ON a.categoryid = c.categoryid ,
product AS d,
procurement AS e
WHERE MONTH(f.date) = MONTH(e.date)
AND YEAR(date) = 2013
[sorry for the align i am new to the site dont know how to copy paste code well(:D)]
wahtever when i do this it comes to a state like
categoryid category profit
1 Gold -100
2 Silver -100
.
5 Platin -100
dont know where is the problem...i made a lot of changes and switches but nothing came up...any suggestion would be so helpfull.Thank u in advane
Initially looks like your profit formula has an extra comma in it.
this
(coalesce(a.rev,0) - coalesce(b.ret,0),coalesce(c.cost,0)) as profit
should be this
coalesce(a.rev,0) - coalesce(b.ret,0) - coalesce(c.cost,0) AS profit
Few more issues with this query
Right before the where clause, after you join the cost subquery, you add product and procurement tables but don't join them. This will cause a cartesian join which will throw off your results.
In the where clause you don't specify which tables date field you want to use. AND YEAR(date) = 2013 should be e.date or f.date. That should have given you an error if you tried to run it.
WHERE MONTH(f.date) = MONTH(e.date) which table is f.date referring to? You didn't give an alias of f to any table.
You join in procurement and use its date field to filter results by month, but none of your revenue, returns, and cost subquery totals take dates into account. This will throw off your results.

having trouble writing a query that list all salesmen that did not sell to a particular customer

I was successful in writing the query that lists salesmen that did sell to a particular customer, but not those that have not. I suspect it is because the same salesmen that sold to the specific customer, also sold to other customers.
select a.name from salesperson a inner join orders b on
a.salesperson_id = b.salesperson_id where cust_id="4";
I was thinking that modifying the same query like this would do the trick:
.... a.salesperson_id <> b.salesperson_id where cust_id="4";
But the result lists all the salesmen. This is most likely due to the fact that the same salesmen that were returned in the original query, also sold to other customers
The 3 tables look like this:
Salesperson table
salesperson_ID, Name, Age, Salary
1 Abe 61 140000
2 Bob 34 44000
5 Chris 34 40000
7 Dan 41 52000
8 Ken 57 115000
11 Joe 38 38000
Customer table
cust_ID, Name, City Industry Type
4 faralon sacramento H
6 Apple cupertino S
7 Honda NY B
9 Kolb Oshkosh B
Orders table
Number, Order_date, cust_id, salesperson_id, Amount
10 8/2/1996 4 2 540
20 1/30/1999 4 8 1800
30 7/14/1995 9 1 460
40 1/29/1998 7 2 2400
50 2/3/1998 6 7 600
60 3/2/1998 6 7 720
70 5/6/1998 9 7 150
Any help would be greatly appreciated. ~Alpinehyker
You can do something like this:
select a.name from salesperson a
left join orders b on a.salesperson_id = b.salesperson_id and b.cust_id="4"
where b.Number is null
So, get all salepersons, left join to orders for customer 4, and return only rows where there is no such order.
I am assuming that Number is the primary key for Orders, or at least not null.
All salespeople who have NOT sold to customer_ID 4:
SELECT s.Name FROM Salesperson AS s
LEFT JOIN Orders AS o
ON s.salesperson_ID = o.salesperson_ID
WHERE o.customer_ID <> 4
GROUP BY o.salesperson_ID;
Perhaps this will work
SELECT
s.*
FROM `Salesperson` AS s
LEFT JOIN `Orders` AS o ON o.`salesperson_id` = s.`salesperson_ID`
WHERE
o.`cust_id` NOT IN (4)
GROUP BY s.`salesperson_ID`;
Answer to your 2nd question:
SELECT
COUNT(*) AS num_of_orders
,s.`Name`
FROM `Salesperson` AS s
LEFT JOIN `Orders` AS o ON o.`salesperson_id` = s.`salesperson_ID`
GROUP BY s.`salesperson_ID`
HAVING num_of_orders >= 2;
...and 3rd question. (assuming you have your highAchiever table ready)
INSERT INTO `highAchiever`
(`Name`,`Age`)
SELECT
`Name`
,`Age`
FROM `Salesperson`
WHERE
`Salary` >= 100000;