My Access database transactions table and in that there is items, qty sold, etc
I want to do a query that select the top 10 highest items along with how many qty sold of that items till now.
How to do this? Thanks.
i am able to select the top X items but unable to select the total qty sold of that items
Related
I have a table like that. (there are more records)
Here is the question. How can I get the first 20 customers having money (amount) between 9000 and 24000 in same bank branch (BranchID), multiple same currency accounts (not only one).
Maybe something like this?
select distinct top 20
yt.CustomerId
from [$your_table] yt
group by yt.CustomerId, yt.BranchId, yt.Currency
having count(*)>1
and sum(yt.amount) between 9000 and 24000
The count(*)>1 catchs those customers with the same branch and currency more than once. Then, for the sum of the amounts (sum(yt.amount)) it gets just those between 9000 and 24000.
Finally, because the same customer could have more than one record (same branch and two different currencys several times - or even several branches), we get the distinct top 20 CustomerId.
In SQL Server you can use TOP for this.
As in:
SELECT TOP 20
FROM TABLE
WHERE filter condition...
Clearly a newbie to this platform. I've been a long time reader of stack overflow, but first time poster, so do go easy on me please.
I have 2 databases (within same instance), one for each company. Let's call them company A and company B. Both of these databases have the following columns:
ProductID, Warehouse, Quantity
I would like to create a SQL View where it has 3 columns:
ProductID, Quantity A (from database A), Quantity B (from database B)
I would also like to include the following conditions:
It should only return results for products that either have stock in Quantity A or Quantity B or Both. It shouldn't return any results if no stock in any of the databases.
For Quantity A - return results where warehouse = XY and XZ
For Quantity B - return results where warehouse = XY
Any help is much appreciated
If I understand correctly, you have two separate tables for each company that you want to combine at the product level.
One method is union all and group by:
select productid, sum(quantity_a) as quantity_a,
sum(quantity_b) as quantity_b
from ((select productid, quantity as quantity_a, 0 as quantity_b
from a
) union all
(select productid, 0 as quantity_a, quantity as quantity_b
from b
)
) ab
group by productid;
I would like to make a viww that find the top 5 highest quantity items from a characters inventory but also a column that shows how many other people also has the item.
CHARACTERS
CHARID PK
INVENTORY
ITEMID
CHARID
quantity
ITEM
ITEMID
This is what I got so far
CREATE VIEW vwTop5
AS
SELECT TOP 5 INVENTORY.itemID, INVENTORY.charID,quantity,COUNT(SELECT *
FROM INVENTORY)
FROM INVENTORY
WHERE INVENTORY.charID = 3
I know this what above is wrong but could you guys please show me what to do to fix it? :)
You were close :
CREATE VIEW vwTop5
AS
SELECT TOP 5 INVENTORY.itemID, INVENTORY.charID,quantity,
(SELECT COUNT(*) FROM INVENTORY) as cnt
FROM INVENTORY
WHERE INVENTORY.charID =
Like #FelixPamittan said, you need to add an ORDER BY clause to specify which TOP 5 records you want.
I have a table, inventory. The items in inventory have an item ID, but there might be multiple entries with different colors and sizes for the same item id. I'm trying to figure out how to return the total number in inventory for each item. So, I would return the total number of item 1, and not the total number of yellow item 1's then the total number of small item 1's, etc.
Thanks!
The basic query would be an aggregation query, something like:
select itemid, sum(quantity)
from inventory
group by itemid;
If you want the total for a single item, then you would add something like where itemid = 1.
If I'm understanding correctly, it sounds like you need to use distinct with count:
select count(distinct itemid)
from inventory
Depending on your desired results, you may be looking to group your results. In which case, you'd use group by with the desired field. Maybe something like this:
select itemid, count(*)
from inventory
group by itemid
This will return a row with the count of records per item id.
I have a products table, with the fields product, category and cost, of type varchar, varchar and decimal.
I then have a sales table, with the fields client, productname, quantity, cost, and saledate, of type varchar, varchar, int, decimal and date.
I want to show all of the products sold for a month, say the current month.
However, I donĀ“t want to show every sale individually. I want to automatically add all of one product together and show it as one row.
In the sales table, the cost for each sales record is already multiplied by the quantity.
So for example, to if 5 beers were purchased, it would returned as one row showing name as beers, quanity as 5, and cost as however much.
I need something like say:
Select product, cost from sales, WHERE sales.product=products.name
AND category='food' AND WHERE month(date_field) = month(getdate())
This should show all the sales for a certain category of product for the current month, but is there an easy way to "group" products together?
I would have to take into account the quantity field in the sales table, because one sale is not necessarily only one product
A hopefully clearer example, one sale record maybe for 2 beers for one client with a cost of 10, and another sales record may be to a different client with a quantity of 3 and cost of 15. I want just one record that would say for beer, 5 were sold and the total cost is 25.
I have no idea where to go from as far as I have gotten...
You are looking for something like
Select product, cost from sales,products WHERE sales.product=products.name
AND category='food' AND month(date_field) = month(getdate())
To get a listing of the items linked as you suggest according to the category in the products tables.
To get the summary by category you need something like:
Select category,SUM(Sales.Quantity),SUM(Sales.cost) from
sales,products WHERE sales.product=products.name
AND category='food' AND month(date_field) = month(getdate()) group by category
This will work but there is a lot to criticise in your database structure, specifically to link products by name as you do is not very reliable.
This question looks awfully familiar...
The answer is a SQL GROUP BY statement.
SELECT product, SUM(quantity) FROM sales s, products p WHERE product=p.name AND category='food' AND month(date_field)=month(getdate()) GROUP BY product
The SUM(quantity) above will tally up all the units sold of a particular product in the given month.
Use GROUP BY and COUNT
SELECT productname, cost, COUNT(*) AS cnt
FROM sales LEFT JOIN products ON sales.product = products.productname
WHERE category='food' AND month(date_field) = month(getdate())
GROUP BY productname