I have a database I am making with Microsoft Access 2013, where there is 2 tables. First table has productID as the primary key, second table has a unique reviewID as well as the productID of the product that the review is referring to. In first table where the products information is kept, I want to have a field that averages the ratings that it was given in it's reviews (kept in second table).
How do I average it's rating without averaging the rating for all reviews, and only for reviews about that specific product?
Based on your descriptions I've created a table called tblProducts with the following data:
I've then created a table called tblReview with the following data (here I've assumed you have a field to store a value for each review's rating that I've called ReviewRating.. and I've assumed that reviews are rated from 0-10):
I then created this query:
SELECT tblProducts.ProductName, Avg(tblReview.ReviewRating) AS AvgOfReviewRating
FROM tblReview INNER JOIN tblProducts ON tblReview.productID = tblProducts.productID
GROUP BY tblProducts.ProductName;
...which results in:
Note that this is a SELECT query, so it won't put the average review rating in to the original tblProducts table, for that you would need an UPDATE query. I wouldn't recommend that though as you'll have to remember to run the update before using tblProducts for anything that needs up-to-date averages.
Related
I am running into an issue with writing an SQL query with Google Big Query. Basically looking to transfer the top products, per country, per category which are also in stock into a table.
So far I have pulled in the top products, per country, per category but the issue is with getting the 'in-stock' part added to the table. I can't find any similar keys in the schema to match them up.
Ideally the table would include:
Rank, Product Title, Country, Category, In-Stock
I would really appreciate any help on this! Thanks.
I have tried to add in a separate table that includes the 'availability' key for each product but I could not match it
You have your top_products table to check the rank that you can join to the product_inventory using the rank_id.
This join will retrieve the product_id and join that key to the products table.
After that, you get the availability information of the product and then you have all the information you require.
Table 1: Customer Info Data (has ~50K records)
Table 2: Sales by customer ID (has ~25K records)
When I am performing a left join of Sales data from Table 2 on Table 1 based on the Customer ID, the output has a handful of records (~500) with a unit increase in the sales quantity. For e.g. sales quantity for customer #1 is 200, the sale quantity that I am getting in my joined output is 201. Note again that this is only for handful of records, for the majority of the data, it is joined absolutely correctly.
The SQL query is a pretty standard one:
SELECT [Table1$].[ID], Name, Volume, Amount
FROM [Table1$] LEFT JOIN [Table2$] ON [Table1$].[ID] = [Table2$].[ID]
What is weird is that this error is only for a few records and it is changing only by a unit for all of them. What do you think could be the potential reason here? Note that I run this SQL query from VBA.
Edit:
Maybe if I add an image, it would help, I have masked my data:
Table1, blue colored column is the joined column, I have filtered for the ID where there is a problem:
Table2, source of the joined column, as you can see it is 20, but the value that has been joined with table 1 is 21. Interestingly, there is no ID with value 21 in table2
There is certainly a space or something that makes seemingly identical IDs different. To make sure do a select distinct id on the sales table.
UPDATE: yes, the result is for a single record. I want to put this on a "computed by" field in firebird (PRODUCTS.PRODU_AVGCOST). It just takes the last 3 buyings for a product and calculates the average cost of it.
I have three tables, witch follows the revelant fields:
PRODUCTS
PRODU_ID PK,
PRODU_AVGCOST calculated
BUYINGS
BUYIN_ID PK
BUYING_ITENS
ITEN_ID PK,
ITEN_BUYING_ID FK
ITEN_COST numeric
I want to take the average cost of last three buyings from ITEN_COST field, using "select" for the PRODU_AVGCOST field of the PRODUCTS table
I tried as follows on table PRODUCTS, but this didn't work
select avg(iten_cost) from buying_itens b
where (select first 3 (iten_cost) from buying_itens where c.iten_id = produ_id)
order by b.iten_id desc
Assuming you want this for a single product (assuming ACTUAL_PRODU_ID is that value), you can do:
select avg(a.iten_cost)
from (
select first 3 iten_cost
from buying_itens
where produ_id = ACTUAL_PRODU_ID
order by iten_id desc
) a
As far as I can tell the datamodel in your question is not complete (or I fail to see how products and buying_itens are linked), so I have 'added' produ_id to buying_itens.
If you want to do this for all products in a single query, things get more complicated.
I have three table in my database.
Customers, which contains detaills of each client, such as name, phone number ...
Products, containing detaills of each product.
each time a client requests a product, a new line is inserted in the third table Orders.
the table Orders contains the customer id (foreign key), the product id (foreign key) and the quantity desired.
what I'm looking to do is to creat a report based on the Orders table, that shows me for each Client all the Orders that he has made.
I'm working on ms access 2007.
please help me !!!
Create a query based on the orders table joined to the customer table. Use the query design window to build the query. You can then base your report on the query, using grouping to get the customer details at the top of the group and the prder details as lines within the group. Use the report wizards.
Ok I have the solution.
I created a form with this record source:
SELECT
DISTINCT Costumers.Code, Costumers.Name, Costumers.phone
FROM
Costumers INNER JOIN Orders
ON Costumers.ID=Orders.IdCostumer;
then I've created a subreport with this record source:
SELECT
[Costumers].[Code],
[Orders].[Code],
[Products].[Description],
[Orders].[Quantity]
FROM
Products INNER JOIN
(Costumers INNER JOIN Orders ON Costumers.ID=Orders.IdCostumer)
ON Products.ID=Orders.IdOrder;
and that's working the way I want.
thank you for your interest !!!
:-)
I have 3 data tables.
In the entries data table I have entries with ID (entryId as primary key).
I have another table called EntryUsersRatings in there are multiple entries that have entryId field and a rating value (from 1 to 5).
(ratings are stored multiple times for one entryId).
Columns: ratingId (primary key), entryId, rating (integer value).
In the third data table I have translations of entries in the first table (with entryId, languageId and title - translation).
What I would like to do is select all entries from first data table with their titles (by language ID).
On a top of that I want average rating of each entry (which can be stored multiple times) that is stored in EntryUsersRatings.
I have tried this:
SELECT entries.entryId, EntryTranslations.title, AVG(EntryUsersRatings.rating) AS AverageRating
FROM entries
LEFT OUTER JOIN
EntryTranslations ON entries.entryId = EntryTranslations.entryId AND EntryTranslations.languageId = 1
LEFT OUTER JOIN
EntryUsersRatings ON entries.entryId = EntryUsersRatings.entryId
WHERE entries.isDraft=0
GROUP BY title, entries.entryId
isDraft is just something that means that entries are not stored with all information needed (just incomplete data - irrelevant for our case here).
Any help would be greatly appreciated.
EDIT: my solution gives me null values for rating.
Edit1: this query is working perfectly OK, I was looking into wrong database.
We also came to another solution, which gives us the same result (I hope someone will find this useful):
SELECT entries.entryId, COALESCE(x.EntryUsersRatings, 0) as averageRating
FROM entries
LEFT JOIN(
SELECT rr.entryId, AVG(rating) AS entryRating
FROM EntryUsersRatings rr
GROUP BY rr.entryId) x ON x.entryId = entries.entryId
#CyberHawk: as you are using left outer join with entries, your result will give all records from left table and matching record with your join condition from right table. but for unmatching records it will give you a null value .
check out following link for the deta:
http://msdn.microsoft.com/en-us/library/ms187518(v=sql.105).aspx