I made a fairly simple database in access that contains the stock, customers, sales order, sales order item tables from a fictional company.
I want to reduce the stock level by using a customer/sales order form. however, if I try to do this, it reduces the "stock_level" from all records within the form.
I tried the following query to do it:
UPDATE stock
INNER JOIN Sales_order_item ON stock.stock_ID = Sales_order_item.fk2_stock_ID
SET stock.Stock_level = [stock]![Stock_level]-[Quantity_Ordered]
WHERE (((stock.stock_ID)=[Forms]![Sales Order SubForm]![fk2_stock_ID]));
And this is the stock table:
thanks for looking into it, I'm completely stuck on this matter!
Related
I have a relational database model based on a hotel booking site set up for a uni project, however I am stumped by one query.
This is as far as I have gotten:
SELECT DISTINCT
property.property_id,
property_name,
property_description,
star_rating,
room_base_price
FROM
property
INNER JOIN rooms ON property.property_id = rooms.property_id;
The problem is that I have several rooms per property listed in my database. In this query, I want to select the room with the minimum price per hotel. The way I am currently doing it, it is showing every single property with every single room and its individual price.
Can anyone advise me on how I can write a query where I will be able to return the lowest price of a room in a property(i.e. a hotel), where it will only be one row per hotel instead of returning multiple rows for each property?
Try following query:
SELECT DISTINCT
property.property_id,
property_name,
property_description,
star_rating,
room_base_price
FROM
property
INNER JOIN rooms ON property.property_id = rooms.property_id;
where (rooms.property_id,rooms.room_base_price) in
(select property_id,min(room_base_price)
from rooms
group by property_id
)
;
Hope it helps!
I'm currently working on a database for a company, for them to use when making production orders. A report is to be made consisting of several things, mainly product number, order number etc etc. A part of the report is to be made up of a list of spare parts needed for the production of the item in question. I have a table with an order number and product number, which needs to look in another table to find the necessary spare parts. However, the name, location and stock of those spare parts are in a third table, and I can't seem to find a way to include these things automatically when the product number is known. I'm pretty new to MS Access, so any help will be greatly appreciated. Thanks!
I have a table called Table1, which uses a combobox to automatically fill boxes such as production time, test time etc from a given product number. This data is gathered from the second table StandardTimes, which has as a primary key the product number. Other columns in this table includes production area, standard quantity, average production time, and also includes in several columns, the necessary spare parts needed. In a third table called Inventory, we have the product numbers of the spare parts, their location in storage, and number of items currently in store. I created a report using a query which takes an order number, and creates a report on that order number from Table1. What needs to be included in this report is a list of the spareparts necessary, the location in the storage, and the number of items currently in store.
Revised from new user input
Your question still does not provide actual columns or data. As a result, it's hard to model your needs.
However, based on what I can read, I think that you have are missing some basic design setup items in a relational model.
Assuming that you have 3 tables: Table1 (Orders), StandardTimes (Products) and Inventory (SpareParts)
In English, every order has one or more products. Every product has one or more spare parts. Really you'd want an orders table, and an order details table which has records for each item as part of that order. But I'm answering it on your setup which I believe is flawed.
Orders <-(1:M)-> Products <- (1:M) -> SpareParts
You have an OrderID, a ProductID, and a SparePartID.
A query such as this would join those 3 tables together with that kind of relationship.
SELECT o.OrderNum, o.ProductNum, st.ProductionArea, st.StandardQuality, i.SparePartsNum, i.Location, i.Qty
FROM Orders as o
INNER JOIN StanardTimes as st on o.ProductNum = st.ProductNum
INNER JOIN Inventory as i on i.ProductNum = st.ProductNum
Some sample data would be helpful to help design the queries.
In principal you would need to join the tables together to get the desired result.
You would join the productID on tblOrders to the ProductID on tblProducts. This will net you the name of the product etc.
This would be an INNER join, as every order has a product.
You would then join to tblSpareParts, also using the productID so that you could return the status of the spare parts for that product. This might be a LEFT JOIN instead of an INNER, but it depends on if you maintain a value of 0 for spare parts (e.g. Every product has a corresponding spare parts record), or if you only maintain a spare parts record for items which have spare parts.
I am fairly new to Databases and I am just beginning to understand the DML/queries, I have two tables, one named customer this contain customer data and one named requested_games, this contains games requested by the customers, I would like to write a query that will return the customers that have requested more than two games, so far when I run the query, I don't get the desired result, not sure if I'm doing it right.
Can anyone assist with this thanks,
Below is a snippet of the query
select customers.customer_name, wants_list.requested_game, wants_list.wantslists_id,count(wants_list.customers_ID)
from customers, wants_list
where customers.customers_ID = wants_list.customers_id
and wants_list.wantslists_id = wants_list.wantslists_id
and wants_list.requested_game > '2';
just include a HAVING clause
GROUP BY customers_ID
HAVING COUNT(*) > 2
depending on how you have your data setup you may need to do
HAVING COUNT(wants_list.requested_game) > 2
This is how I like to describe how a query works maybe itll help you visualize how the query executes :)
SELECT is making an order at a restaurant....
FROM is the menu you want to order from....
JOIN is what sections of the menu you want to include
WHERE is any customization you want to make to your order (aka no mushrooms)....
GROUP BY (and anything after) is after the order has been completed and is at your table...
GROUP BY tells your server to bring your types of food together in groups
ORDER BY is saying what dishes you want first (aka i want my entree then dessert then appetizer ).
HAVING can be used to pick out any mushrooms that were accidentally left on the plate....
etc..
I would like to write a query that will return the customers that
have requested more than two games
For this to happen you need to do the following
First you need to use GROUP BY to group the games based on customers (customers_id)
Then you need to use HAVING clause to get customers who requested more than two games
Then make this a SUBQUERY if you need more information on the customer like name
Finally you use a JOIN between customers and the sub query (temp) to display more information on the customer
Like the following query
SELECT customers.customer_id, customers.customer_name, game_count
FROM (SELECT customer_id, count(wantslists_id) AS game_count
FROM wants_list
GROUP BY customer_id
HAVING count(requested_game) > '2') temp
JOIN customers ON customers.customer_id = temp.customer_id
I now have tried DLookUp, but I do not get the result I want.
My Situation: I am creating a database for a cinema. It includes tables named cinemas, movies, sold seats, price.
I have made a form where I can record a new "guest". I can select the ID of the Show and then I see the date and the time. I now have recorded some guests and now there should be less seats free. I want to show that on the same form as the one where I record new guests. I don't get up with the Dlookup.
I have made this query which calculates how many seats are left but I cannot put that into the form.
SELECT
tbl_Vorstellung.ID_Vorstellung,
tbl_Kino.Kino,
Sum([tbl_Kino]![Sitzplätze]-[qry(02)PlätzeVerkauft]![AnzahlvonTicketNummer]) AS Ausdr1
FROM
tbl_Kino
INNER JOIN ([qry(02)PlätzeVerkauft]
INNER JOIN tbl_Vorstellung
ON [qry(02)PlätzeVerkauft].ID_Vorstellung = tbl_Vorstellung.ID_Vorstellung)
ON tbl_Kino.ID_KINO = tbl_Vorstellung.FS_KINO
GROUP BY
tbl_Vorstellung.ID_Vorstellung,
tbl_Kino.Kino;
Save your working query as a new name, for this example MyQuery
Then use
DLOOKUP("Ausdr1", "MyQuery", "ID_Vorstellung=SomeValue AND Kino=SomeValue")
On your form. You'll have to put in the SomeValue values based on fields on your form, etc.
I have a table with the following fields (for example);
id, reference, customerId.
Now, I often want to log an enquiry for a customer.. BUT, in some cases, I need to filter the enquiry based on the customers country... which is in the customer table..
id, Name, Country..for example
At the moment, my application shows 15 enquiries per page and I am SELECTing all enquiries, and for each one, checking the country field in customerTable based on the customerId to filter the country. I would also count the number of enquiries this way to find out the total number of enquiries and be able to display the page (Page 1 of 4).
As the database is growing, I am starting to notice a bit of lag, and I think my methodology is a bit flawed!
My first guess at how this should be done, is I can add the country to the enquiryTable. Problem solved, but does anyone else have a suggestion as to how this might be done? Because I don't like the idea of having to update each enquiry every time the country of a contact is changed.
Thanks in advance!
It looks to me like this data should be spread over 3 tables
customers
enquiries
countries
Then by using joins you can bring out the customer and country data and filter by either. Something like.....
SELECT
enquiries.enquiryid,
enquiries.enquiredetails,
customers.customerid,
customers.reference,
customers.countryid,
countries.name AS countryname
FROM
enquiries
INNER JOIN customers ON enquiries.customerid = customers.customerid
INNER JOIN countries ON customers.countryid = countries.countryid
WHERE countries.name='United Kingdom'
You should definitely be only touching the database once to do this.
Depending on how you are accessing your data you may be able to get a row count without issuing a second COUNT(*) query. You havent mentioned what programming language or data access strategy you have so difficult to be more helpful with the count. If you have no easy way of determining row count from within the data access layer of your code then you could use a stored procedure with an output parameter to give you the row count without making two round trips to the database. It all depends on your architecture, data access strategy and how close you are to your database.