MS Access 2013: Top 10 Items sold in each country - ms-access-2013

I need write a query to get top 10 Items sold in each country
Database Type is MS Access 2013
Table Name: SoldItems
Fields
Country
Item
Query
This query will get me all items sold in each country ordered descending by count of sold items in each country, i only need top 10 items sold in each country
SELECT count(*) As CountOfItemsSold, Country, Item
from SoldItems
group by Country, Item
order by 2,1 desc

You will need to use a sub query that returns the Top 10 items grouped by country and use that as the basis for a filter. Something like:
SELECT S.Country, S.Item, COUNT(S.Item) AS CountOfItem
FROM SoldItems AS S
GROUP BY S.Country, S.Item
HAVING S.Item In (SELECT TOP 10 R.Item
FROM SoldItems AS R
GROUP BY R.Country, R.Item
HAVING R.Country=S.Country
ORDER BY COUNT(R.Item) DESC
)
ORDER BY S.Country, COUNT(S.Item) DESC;
Note that if there are items with an equal number of items sold, then you will get more than 10 records returned per country.
Regards,

Related

How can i solve this query in sql oracle?

It's an exercise that is not solved in the book in which I am studying.
The goal is to find the seller who has had the highest number of sales per month,
during all the months for which there is registered information. The problem is that I do not know how to divide tuples into periods of one month.
First table is:
Table Sellers
Id_seller
Name_Product
And the other one is:
Table Product
Name_Product
View_datetime
Budget
What did i do?
I made this query:
SELECT id_seller FROM(SELECT id_seller, COUNT(id_seller)
FROM SELLERS INNER JOIN PRODUCT
ON SELLERS.name_product = PRODUCT.name_product
GROUP BY id_seller HAVING COUNT(id_seller)>= 1
ORDER BY 2 DESC)
WHERE ROWNUM = 1;
The query returns me the seller that most sales has done, but not "per month since there are records" as the statement asks. Any ideas? I'm so lost...
The idea is to compare the total sales of each salesman in this month (sysdate), with those of a month ago, two months ago ... so long as there are older records. And get the maximum from each seller. And then you print the seller with more sales from the previous list. If a seller sells 400 products this month(April, the sysdate), but another seller sold in October last year 500, the result would be the second seller . That's what I do not know how to do.
Thanks ^^
You could try this query
select MonthName, id_seller, max(TotalSales) from (
select to_char(sysdate, 'Month') MonthName, sellers.id_seller, count(sellers.id_seller) TotalSales
from sellers inner join product
on sellers.name_product = product.name_product
group by to_char(view_datetime, 'Month'), sellers.id_seller
) tab
group by MonthName, id_seller
There are a few points to make...
The tables are weird. I assume your table sellers would better be called sales, right?
In this example, having count... >= 1 is a no-op. Count could only be 0 if there were no rows at all, in which case there would be no row in the group- by output. You can just leave this count away, here.
To get the sales per month, just add the month to the group by. I.e. group by id_seller, To_date(view_datetime,'YYYYMM').

Count Length and then Count those records.

I am trying to create a view that displays size (char) of LastName and the total number of records whose last name has that size. So far I have:
SELECT LENGTH(LastName) AS Name_Size
FROM Table
ORDER BY Name_Size;
I need to add something like
COUNT(LENGTH(LastName)) AS Students
This is giving me an error. Do I need to add a GROUP BY command? I need the view:
Name_Size Students
3 11
4 24
5 42
SELECT LENGTH(LastName) as Name_Size, COUNT(*) as Students
FROM Table
GROUP BY Name_Size
ORDER BY Name_Size;
You may have to change the group by and order by to LENGTH(LastName) as not all SQL engines let you reference an alias from the select statement in a clause on that same statement.
HTH,
Eric

Return only one record of distinct item with multiple unique locations

I am not sure if this could even be done.. but I really only want an item showing up with 1 location to pull stock from rather than two in the mix causing a double pull of inventory... preferably the location with MIN qty, BUT some items have multiple locations of the same qty. This is too deep for me and I've been hounding for ways to return only one location of the 3 even if I cant get the MIN.
Here is my table
ITEM LOC QTY
R12345 40A1000005 22
R12345 50B0300003 98
R12345 60C2300004 22
I wish to return only ONE of the minimal qty locations.
ITEM LOC QTY
R12345 40A1000005 22
SELECT DISTINCT item,loc,min(qty) as minqty
FROM whse
GROUP BY item,loc
ORDER BY item;
This obviously doesn't work and I've tried creating other views and joining them together but I haven't the slightest luck on it.
As you haven't mentioned any Database name, I am assuming you are using Oracle DB.
Here is one of the many solutions :-
with tab (item,loc,qty)
as
(
select 'R12345','40A1000005','22' from dual
union all
select 'R12345','50B0300003','98' from dual
union all
select 'R12345','60C2300004','22' from dual
)
select * from (
select item,loc,qty,dense_rank() over (partition by item order by qty,loc) rn from tab)
where rn=1;
Let me know if that solves your problem.
Assuming mysql:
SELECT DISTINCT item,loc,min(qty) as minqty
FROM whse
GROUP BY item,loc
ORDER BY item LIMIT 1;
So just add a "LIMIT 1"
For other database engines, similar solutions exist.

obiee count distinct by as part of total count distinct

I need create following "numeric distribution" report in OBIEE :
item name,
count(distinct item_id by item_name),
count(distinct item_id by item_name)/count(distinct item_id)
(third column should be percent of count distinct by item name / count distinct by total)
How to get total count distinct ? I'm stuck and i will be appreciate any help.
The result should be:
Item 1 , 10, 50%
Item 2 , 20, 100%
where total number of customers is 20, and 10 od them buy Item_1, and 20 of them buy Item_2
Click on Measure in Pivot table then click on 'show Data as'>Percent of>Column.
Thats it

How to do a limited query like MySQL (LIMIT 0, 10) on Dbase?

It is possible?
I know you can limit your queries with
SELECT TOP 10 name FROM customers ORDER BY name
but how can I get names from 50 to 100?
I need to make a pagination but I can't find a way to do it.
Thank you very much.
There is more simple way:
SELECT * FROM table1 WHERE RECNO()>50 AND RECNO()<=100
You can do two opposite sorted SELECT TOP queries.
see: http://thepcspy.com/read/paging_in_sql/
SELECT name FROM
(SELECT TOP 50 name FROM
(SELECT TOP 100 name FROM customers ORDER BY name ASC) AS a
ORDER BY name DESC
) AS b
ORDER BY name ASC
Here, 50 is page size and 100 - 50 (ie: 50) is start index.

Resources