I got a table StockMovements which records all movements of my
products. It has a field named "Status", which can have either the
value Sold or Purchased, Quantity and Product (there are few more but
arent important now).
I made a query to take all the products with the status "Purchased"
and to take the quantity and named it Purchased Products (From the
table StockMovements). I have just sum the quantity and got the
purchased quantity for the each product (named the field Purchased).
I made another query, all the same just with the Status of Sold.
After I made a new query with the name Stock. Its built of the table
Products and the 2 queries I mentioned above. It takes the prodict_ID
and the product name from the table Product, the purchased field from
the first query and the Sold field from the second query and then a
final field not connected to any of above but a calculation. Named it
AvailableQuantity and added next with expression builder to it
=[Purchased] - [Sold].
Now when I run the query it works fine, except one thing. When I have
a specific quantity of a purchased product which I havent sold to
anyone yet (not even 1 piece) it doesnt want to show up in the query.
I want the product to show in the query and that the available
quantity is the purchased quantity. So somehow to skip the Sold value
if its 0 and just to write the Purchased quantity to that field.
When you have no sold qty your sum effectively becomes Purchased-Null which will result in no answer.
You should allow for nulls by using [Purchased] - (Nz([Sold],0)) which will force a zero into the calculation.
I went into the SQL view and changed the INNER JOIN to RIGHT JOIN, it did the job.
SELECT tblProduct.Product_ID, tblProduct.ProductName, Nz([Purchased],0) AS
PurchasedQuantity, Nz([Sold],0) AS SoldQuantity, [PurchasedQuantity]-(Nz([SoldQuantity],0))
AS [Available] FROM qrySoldProducts RIGHT JOIN (qryPurchasedProducts RIGHT JOIN
tblProduct ON qryPurchasedProducts.Product_ID = tblProduct.Product_ID) ON
qrySoldProducts.Product_ID = tblProduct.Product_ID;
Related
While im not new to databases, i am out of practice and was wondering how to create a total price attribute for a store database.
The table has the attributes: PT KEy, Product ID, Quantity, offer, Total item price(the product table has the prices, this field is just the price multiplied by the quantity), offer.
I am trying to make a field that adds up all the Total item prices in the table, giving the entire price of the transaction.
Should i make this field in another table or do it in the current one? if so how?
So in the end i made it a calculated field in that was generated in a query. I removed any calculated fields of the such and replaced them in the queries.
I am working on a project and I am a newbie in database. I need help answering the questions below with the scenario and the database tables listed.
Database tables:
Product (pid:integer, timestamp:integer, name: string, price:real, location:string)
Customer (cid:integer, email: string)
Purchases (pid:integer, cid:integer, orderid:integer, amount:integer)
Totals (orderid:integer, cid:integer, totalprice:real, timestamp:integer)
Scenario:
A product ID can occur multiple times in the schema. Each time the location or price is updated, another line is added to the database with a timestamp that indicates the time of change. The name does not get changed, so the same pid will always imply the same name.
Totals is a summary of the purchases table which shows when the purchases were made, and what the combined price of all products were.
Whenever possible, try to do your projections as early as possible.
Use the above database and provide queries for the following problems:
Find the names of products that at some point in time cost more than e20.00 and the names of products that have at some point cost less than e0.10.
Find the email addresses of customers that have spent more than e200 at once.
Find the pids of products that have had at least one price change.
Find the names of products that have both been displayed at location ’5-12’ and ’A3’
Find the cid of customers that have bought each product that at some point cost less than e1.00
Find the cid of customers that are registered with the store but have made no purchases.
Find the cid of the customer(s) that have made the largest total purchase.
Find the most expensive product that has been purchased at least once by each registered customer.
Find the pids of products that have not been sold since timestamp 20150625 but have been sold at least once before that date.
The grocery store wants to improve its database. Write a query that returns a table that is basically the Purchases table plus the price of the product at the time of purchase.
Let's try to provide you a bit of help to start your project ❤
In query one, we need to work exclusively with table Products, and You want to find the name of those products with price above x and less than y.
First, the columns we want to get FROM the table:
SELECT name, price FROM Products
Then, as you need a query that get products more expensive than 20, and producs less expensive than 0.10, you could use the Condition BETWEEN reverted with NOT:
SELECT name, price FROM Products
WHERE price NOT BETWEEN 0.10 and 20
And you can order it to be more readable:
SELECT name, price FROM Products
WHERE price NOT BETWEEN 0.10 and 20
ORDER BY name ASC;
I'm not sure if this is what you need, but I hope it helps a bit!
I'm building a small inventory database in MS Access (2007), and I have one big dilemma: Should I store purchased (acquired) products/quantities in the same table as sold, with a field for transaction type, or should I separate them in two tables?
I'm working on second option now, but I'm stuck on querying these two tables.
tblProducts: ProductID (PK), ProductCode, ProductName
tblVendorsCustomers: VndCstID(PK), VndCstName, etc..(Vendors can also be Customers and vice-versa)
tblPurchase: PurchaseID(PK), PurchaseNumber(specific), VndCstID(FK), DatePurchased, DueDate
tblPurchaseDetails:PDetailsID(PK), PurchaseID(FK), ProductID(FK), QuantityPurchased, PricePurchased
tblSale: SaleID(PK), SaleNumber(specific), VndCstID(FK), DateSold, PayDate
tblSaleDetails: SDetailsID(PK), SaleID(FK), ProductID(FK), QuantitySold, PriceSold
Two tables (Purchase, Sale) are updating fine. Now, for example, when I want to show a report for a chosen Product, I need to pull data from these two tables, to see purchased quantity (along with Vendor name, date of purchase, and price) and sold quantity (with same set of data) and to calculate available quantity (at a given date). Maybe I'm missing something, but the only way to do this is create two select queries (for each of these tables), than union of these (to pull all transaction data), then a select query of that union, add an identifier field (for row from purchases and row from sales) and criteria for product selection..and I'm stuck on calculating available quantity..I'm guessing sum IIf, but not sure how to use it..
Sorry for such a long post...
Is this the right approach?
Any help or advice would be appreciated.
I have three tables: product, sales_order (where I sell products) and purchase_order (where I buy products). Now I can think of two ways of keeping the quantity of each product:
Have a column in the product table called quantity; when inserting into sales_order, I subtract the quantity; when inserting into purchase_order, I add the quantity
Instead of storing the quantity in the product table, I calculate the quantity from the sales_order and the purchase_order table each time I need to get the product table
I am wondering if the second approach is preferable to the first one? I like the second one more because it doesn't store any redundant data; however, I am not so sure if calculating the quantity every time is a bit too much calculation. I am wondering what is the convention and best practice here? Thank you!
I would use the first one. Add a column to the product table in the coding u code -x amount when order and you would then display this in the order table. You could right a script for when the products get to a certain amount it emails you and tells u to replenish stocks. However the second would also work and sql is very powerful so i wouldnt wprry about it being ro demanding as it will prbably work it out faster than we can lol
I prefer the first one because in-memory calculations are faster than issuing select statements to check the sales orders and purchase orders assuming that the number of times the quantity value is retrieved is significantly more than the number of times the quantity value is updated.
I have 2 Tables in MySQL
First is Sale and second is Purchase I want to see a Stock
this is My sale table that contains
Date Quantity ProductName
this is My Purchase Table that Contains sane attributes as sale have
I perform a Query but i Can not get my desire output
SELECT sale.Date, sale.ProductName, SUM(sale.StockQuantityIn) as StockIN,
SUM(purchase.StockQuantityout) as Stockout,
(SUM(sale.StockQuantityIn)-SUM(purchase.StockQuantityout)) as stock
from sale
join purchase on purchase.ProductName=sale.ProductName
GROUP BY sale.Date,purchase.Date ,purchase.ProductName,sale.ProductName
this is the Query and Result is noy my desired output
You cannot join the two tables as you did.
When you join two tables, you can imagine (it is not what really happens, but it helps to understand the result) that all the possible combinations of their rows are evaluated and later filtered based on the "where" constraints.
Try to simulate this process and you will see that the result is what you would expect.
It's not very good to JOIN, since each line in each table will appear as many times as matched in second table. This is why you get all these extra stockOut.
I would UNION 2 queries, one on sale, one on purchase, with ProductName, Date, and StockIn (which would be negative for the query on purchase)
Then I would GROUP BY ProductName and Date and SUM StockIn