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.
Related
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
I have the following tables:
table part_list:
part_number | description | type
100 blablabla blabla
table part_list_supplier:
part_id | artikel
100 100100
100 200100
and I have this query:
select part_list.part_number, part_list.description, part_list.type, group_concat(coalesce(part_list_supplier.artikel, "nothing")) as "artikel"
from part_list
left join part_list_supplier on (part_list.part_number = part_list_supplier.part_id)
group by part_list.part_number;
this is the result:
part_number | description | type | artikel
100 blablablabla blabla 100100,200100
but I want to show the total stock per partnumber behind it. table receipt:
Number | import
100 5
100 10
table sales:
Number | sold
100 5
this is my query for one table:
SELECT SUM(sold) AS sold
FROM sales WHERE number = '".$partnumber.”'
but I want to calculate the stock per number and that must be shown behind the other results.
the full result:
part_number | description | type | artikel | stock
100 blablablabla blabla 100100,200100 10
The stock should be 10 because the total number of imports is 15 (5 + 10) and the total number of sales is 5.
I broke this up into pieces to solve it. I started by writing two queries, one that counted total receipt and one that counted total sales:
SELECT r.number, SUM(r.import) AS totalIn
FROM receipt r
GROUP BY r.number;
SELECT s.number, SUM(s.sold) AS totalOut
FROM sales s
GROUP BY s.number;
Then, I used those as two subqueries of a join to get the stock:
SELECT r.number, totalIn - totalOut AS stock
FROM(
SELECT r.number, SUM(r.import) AS totalIn
FROM receipt r
GROUP BY r.number) r
JOIN(
SELECT s.number, SUM(s.sold) AS totalOut
FROM sales s
GROUP BY s.number) s ON s.number = r.number;
Once I verfied this gave the proper stock, I was able to include those subqueries into your original query to build this:
SELECT pl.part_number, pl.description, pl.type,
GROUP_CONCAT(COALESCE(pls.artikel, "Nothing.")) AS artikel,
r.totalIn - s.totalOut AS stock
FROM part_list pl
LEFT JOIN part_list_supplier pls ON pls.part_id = pl.part_number
JOIN(
SELECT number, SUM(import) AS totalIn
FROM receipt
GROUP BY number) r ON r.number = pl.part_number
JOIN(
SELECT number, SUM(sold) AS totalOut
FROM sales
GROUP BY number) s ON s.number = r.number
GROUP BY pl.part_number;
Here is an SQL Fiddle example.
I may not be understanding your question properly, but can't you just add sum(sales.sold) to your select statement and join the sales table? E.g.:
select part_list.part_number, part_list.description, part_list.type, group_concat(coalesce(part_list_supplier.artikel, "nothing")) as "artikel", sum(sales.sold)
from part_list
left join part_list_supplier on (part_list.part_number = part_list_supplier.part_id)
left join sales on (part_list.part_number = sales.number
group by part_list.part_number;
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.
I’m new to mySQL and I’m struggling to write a query that will list all stores where a price for a product has been scanned as well as the stores where it has not been scanned. The following gives the correct result for a single product:
select distinct(s.id) as store_id, s.chainID as chain_id, p1.productID as product_id,
s.chain, s.location, s.city, prd.brand, prd.product, prd.quantity, prd.size, prd.unit
from analytics.price p1 -- Fact table with prices
join analytics.pricetime pt -- Dimension table with time a price was scanned
on p1.priceTimeID = pt.id
join analytics.product prd -- Dimension table with products
on p1.productID = prd.id
and prd.published = 1
right join analytics.store s -- Dimension table with stores and the chain they belong to
on p1.storeID = s.id
and p1.chainID = s.chainID
and p1.productID = 46720
and p1.priceTimeID between 2252 and 2265
where s.published=1
and s.chainID = 5;
When I remove the p1.productID = 46720 clause to get results for all products, I get all the stores that have scanned prices (correct), but the no price side of the right join only shows stores that have not had any prices scanned for any products. (This is a simple star schema with a price fact and dimensions of product, time and store). I would greatly appreciate help – I’ve tried this every way I can think of including “in”, “not exists” and stored procedure with cursor but I seem to hit a brick wall each way I try it.
Edited to clarify:
Here's what I'm trying to achieve:
Price table
Product Chain Store Price
100 5 1 $10
101 5 2 $20
Store table
Chain Store
5 1
5 2
5 3
Desired Result
Product Chain Store Price
100 5 1 $10
100 5 2 NULL
100 5 3 NULL
101 5 1 NULL
101 5 2 $20
101 5 3 NULL
Actual Result
Product Chain Store Price
100 5 1 $10
101 5 2 $20
NULL 5 3 NULL
I prefer the readability of using a LEFT JOIN -- this should return all published stores in chainid 5 and the associated products (given the criteria).
select distinct s.id as store_id, s.chainID as chain_id, s.chain, s.location, s.city,
prd.id as product_id, prd.brand, prd.product, prd.quantity, prd.size, prd.unit
from analytics.store s
left join analytics.price p1
on p1.storeID = s.id
and p1.chainID = s.chainID
and p1.priceTimeID between 2252 and 2265
left join analytics.product prd
on p1.productID = prd.id
and prd.published = 1
left join analytics.pricetime pt
on p1.priceTimeID = pt.id
where s.published=1
and s.chainID=5;
EDIT -- Give comments, it looks like you're looking for a Cartesian Product:
SELECT P.Product, P.Chain, S.Store, IF(P.Store=S.Store,P.Price,NULL) Price
FROM Price P, Store S
WHERE P.Chain = 5
AND S.Chain = P.Chain
ORDER BY P.Product, S.Store
SQL Fiddle Demo
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;