Choosing Correct Dates - ms-access

Please excuse my formatting... I am working on that.
I am trying to select the correct price of a project in my query based off the appropriate effective date.
For example we have a table that has the following information
Price Table-------------------------
Item Price effectiveDate
A $0.57 1/1/17
A $0.72 6/1/17
Now I have a production table that contains what was produced that day and it will list out their quantity and the production for a production date.
Production Table-------------------
Item Quantity productionDate
A 100 2/1/17
A 100 7/1/17
Now when I query these I want to be able to select the appropriate price given the productionDate and effectiveDate.
What is the best way to achieve this?

Try the following (first select the highest effectiveDate lower than the productionDate, then get the price for that date):
SELECT preselection.Item, Price FROM
(SELECT Production.Item, Max(effectiveDate) As MaxEffectiveDate
FROM Production INNER JOIN Price ON Price.item = Production.Item
Where Price.effectiveDate <= productionDate GROUP BY Production.Item) As preselection
INNER JOIN Price ON Price.Item = preselection.Item
AND Price.effectiveDate = preselection.MaxEffectiveDate

Related

Subquery returns more than 1 row, how can I solve?

Question: Show the category of competitions that have always been hosted in the same country during May 2010. What is wrong with my query?
select Category
from competition
where Date >= '2010-01-01' and Date <= '2010-12-31'
group by Country, Category
having count(*) = (select count(*)
from competition
where Date >= '2010-01-01' and Date <= '2010-12-31'
group by Category)
You don't need two queries. Just use one query that checks that the count of countries is 1.
select category, count(DISTINCT country) AS country_count
from competition
where Date BETWEEN '2010-05-01' and '2010-05-31'
group by Category
HAVING country_count = 1
I also corrected the dates to be just May, not the whole year 2010.
Remove the GROUP BY if you that is making you return more than 1 row (in your HAVING CLAUSE. If you give me an example dataset and what you want I can help you more
I'd try something like this to start with:
SELECT COUNTRY
, CATEGORY
, COUNT(COUNTRY)
FROM COMPETITION
WHERE DATE BETWEEN '2010-04-30' AND '2010-06-01'
ORDER BY CATEGORY DESC, COUNT(COUNTRY) DESC
;
Your original query's date limits are just for the year of 2010 but you specified you only wanted May 2010. If the Date column is a date or datetime time you'll need to cast the string to the appropriate datatype.
Your question asked "always hosted by one country" - do you know that a competition is only going to be hosted by one country during that particular month? If you do, you're pretty much done. If you don't, however, then you need to clarify what your criteria really are

MySQL - Average number of particular product sold on date

I need to write SQL query for "average number of particular product sold by date. On each day is sold min one product".
SELECT AVG (COUNT (PID))
FROM SOLD
GROUP BY DATE, PID;
P.S. PID means Product ID.
Is this query okay?
Should this give right answer?
Consider Using distinct count of date columns
SELECT PID,
COUNT(PID)/COUNT(distinct date_) as "Avg.Product Sold By Days"
FROM SOLD
GROUP BY PID;
You can try this sql query below. Basically, it will return the average number of 'SALES' for each product you have. It will group by each distinct product ID. Please provide us the data structure your of table and etc.
SELECT product_ID, trans_date
Sum(sales_of_product) / COUNT(DISTINCT sold_transaction) AS 'avg'
FROM SOLD
GROUP BY product_ID

Get data of year on month basis in percentage

I have been wondering on google to find solution to my problem but all I get is beyond my understanding because I am new to database.
I am using Mysql.
Problem
I have thousands of records in table out of which I have to show
month wise percentage of records of whole current year. Following is structure of order table
This is how I want to show data
I'd cross join two queries. The first would sum the number of products sold each month and the second would sum the total number of products sold that year:
SELECT `month`, `num_month` / `num_year` * 100 AS "percent"
FROM (SELECT MONTH(`dateOfOrder`) AS "month", COUNT(*) AS num_month
FROM `order`
WHERE YEAR(`dateOfOrder`) = 2018
GROUP BY MONTH(`dateOfOrder`) a
CROSS JOIN (SELECT COUNT(*) AS num_year
FROM `order`
WHERE YEAR(`dateOfOrder`) = 2018) b

MYSQL - SUM of a column based on common value in other column

I'm stuck on crafting a MySQL query to solve a problem. I'm trying to iterate through a list of "sales" where I'm trying to sort the Customer IDs listed by their total accumulated spend.
|Customer ID| Purchase price|
10 |1000
10 |1010
20 |2111
42 |9954
10 |9871
42 |6121
How would I iterate through the table where I sum up purchase price where the customer ID is the same?
Expecting a result like:
Customer ID|Purchase Total
10 |11881
20 |2111
42 |16075
I got to: select Customer ID, sum(PurchasePrice) as PurchaseTotal from sales where CustomerID=(select distinct(CustomerID) from sales) order by PurchaseTotal asc;
But it's not working because it doesn't iterate through the CustomerIDs, it just wants the single result value...
You need to GROUP BY your customer id:
SELECT CustomerID, SUM(PurchasePrice) AS PurchaseTotal
FROM sales
GROUP BY CustomerID;
Select CustomerID, sum(PurchasePrice) as PurchaseTotal FROM sales GROUP BY CustomerID ORDER BY PurchaseTotal ASC;
Just by having a little Google search, I managed to find a page doing exactly what you're doing (I think). I have tailored the query below to fit your circumstance.
SELECT CustomerID, SUM(PurchasePrice) AS PurchaseTotal
FROM sales
GROUP BY CustomerID
ORDER BY PurchaseTotal ASC
Link to Page with Tutorial on SQL Groups

Return unique items in SQL query

SELECT Product.name, Product.price
FROM Purchased, Product
WHERE Purchased.date1 > '2013-11-02'
ORDER BY price DESC group by Product.name
Hello I am writing a query to return a list of unique products that were bought retailer’s site since November the 2nd with most expensive product returned first.
Can anyone tell me what is wrong with my syntax?
You are using MySQL's group by extension incorrectly. Although MySQL allows you to put items in the select list that are not in the group by or an aggregate, it is not deterministic and ORDER BY will not help this.
The MySQL documents state:
The server is free to choose any value from each group, so unless they are the same, the values chosen are indeterminate. Furthermore, the selection of values from each group cannot be influenced by adding an ORDER BY clause.
So even if your data was like:
ProductName | Price
------------+----------
A | 2.50
A | 3.50
B | 9.99
Using GROUP BY Product.Name ORDER BY Price DESC is not guaranteed to return the highest price for each product. i.e.
ProductName | Price
------------+----------
A | 3.50
B | 9.99
What you are really saying is "give me one price per product (any price), then order the final results by price". So you could just as easily get 2.50 as the price for product A. I think you could get the results you want by using ``MAX`:
SELECT product.name,
Max(product.price) AS Price
FROM purchased,
product
WHERE purchased.date1 > '2013-11-02'
GROUP BY product.name
ORDER BY price DESC;
I suspect you are also missing a relation between product and purchased, this would be avoided if you used the newer ANSI 92 explicit join syntax, the implicit join syntax you are using is 20 years out of date:
So your final query would be something like:
SELECT product.name,
Max(product.price) AS Price
FROM purchased
INNER JOIN product
ON product.id = purchased.productid
WHERE purchased.date1 > '2013-11-02'
GROUP BY product.name
ORDER BY price DESC;
N.B. I have had to guess at the relationship between Product and Purchased
SELECT
p.name AS Name,
MAX(p.price) AS Price
FROM Purchased AS pr
INNER JOIN Product AS p
ON pr.ProductId=p.ProductId
WHERE pr.date1 > '2013-11-02'
GROUP BY p.name
ORDER BY p.price DESC
I suppose that both your tables, Purchased and Product, have a productId column. This way each record to one table will relate with another record to the other table. If this is not the name of this column, please change the ProductId to the corresponding name.
The first thing you will need to do is find out how the tables are related to one another. I'm going to assume that you have a productid column in your purchased table.
SELECT Product.name, Product.price
FROM Purchased, Product
where Purchased.productid = product.id
and Purchased.date1 > '2013-11-02'
ORDER BY price DESC
What we are doing here is joining the two tables by the id column they have in common. By doing this the database knows what data links together.
Lets say we have the
purchase table
PurchaseDate , Productid,
2013-11-02 1
2013-11-03 2
Product table
id , Product name
1 Fuzzy Bunny
2 Fuzzy bear
Now if you look at that data you know that on 2013-11-02 fuzzy bunny was purchased. You can see this because the productid on your purchase table links back to the id on the product table. That's how you figure out how to work with Joins in SQL.
Referring to your previous question.
SQL Structure help and query
Here is the sqlfiddle
In the query,
group by should be before the order by clause.
SELECT product.name,
product.price
FROM purchased,
product
WHERE purchased.date1 > '2013-11-02'
GROUP BY product.name
ORDER BY price DESC;
Can you please try this:
SELECT Product.name, Product.price
FROM Purchased, Product
WHERE Purchased.date1 > '2013-11-02'
ORDER BY price DESC GROUP BY Product.name,Prodcut.price
When you are using GROUP BY , you must include all the columns that you are retrieving in select class.
Hope this helps.