MySQL range between dates: First order + 6 months - mysql

I'm using MySQL Workbench to run my query.
I want to run a couple of different date queries, I don't know if it's possible in SQL.
1) Run the report from the first date in the system to X date.
How can I find what the first orderID or date is as part of the calculations?
I know that if I have the date to begin with, I can use:
where T5.date_purchased BETWEEN '2005-01-01' AND '2015-12-31' OR:
where T5.date_purchased BETWEEN '2005-01-01' AND CURDATE() + INTERVAL 1 DAY
UPDATE
----- Query 2 has been answered (although open to any improvement) -----
2) Run the report from the first time a product shows up, to + 6 months (to see it's first 6 months of order) ie: Widget 1 (first order) + 6 months from order date. Something like:
where widget=widgetID AND date between widget1's first purchase and +6 months
Update: This doesn't work, however this is somewhat of what I was thinking:
where (T3.products_id = 39) and DATE_ADD((T1.products_date_added), INTERVAL 1 MONTH)
I would use my P3.products_date_added, however, I don't know how to use it as part of the above, correctly.
Are either of these possible, I know how to pull the records when I know the date, I just don't know if it can be done with 'date unknown' or if I have to run a "pre-report" first. Or is it a post processing filter in excel?
Thank you in advance.

Answering problem #2:
2) Run the report from the first time a product shows up, to + 6 months (to see it's first 6 months of order) ie: Widget 1 (first order) + 6 months from order date. Something like:
where widget=widgetID AND date between widget1's first purchase and +6 months
Answer:
-- Use for specific comparisons of products OR for the first X months of sales
where (T3.products_id = 39) and T5.date_purchased between T1.products_date_added and DATE_ADD((T1.products_date_added), INTERVAL 2 MONTH)
-- This results in PID: release: 10th July, +2 months; 10th Sep. 31 units.
OR
-- (T3.products_id = 11 or T3.products_id = 39) gives the results of the 2 product orders from release date to the first 2 months of each
-- (T3.products_id) gives all products, their first 2 months of release
-- (T3.products_id = 39) gives specific product release sales
-- Inspired by: http://stackoverflow.com/questions/28788691/mysql-range-between-dates-first-order-6-months?noredirect=1#comment45853546_28788691

If it is possible for your solution, try and run 2 separate queries:
- ONE for finding the date of first order
- then calculate +6 months from query 1 result
- AND the second one to get the purchases BETWEEN both dates

Related

Availability by day and week

I'm trying to come up with a database structure to account for products that are available on certain days of week and on certain weeks ( odd / even )
An example:
- product1 is available for selling Mondays in odd weeks
- product2 is available everyday on even weeks
- product3 is available on weekends
I thought of isolating the availability in a second table with the product as fk and each condition on a separate row like so:
fk_prod1 weekday 1
fk_prod1 weekparity 1
fk_prod2 onweekday 1
fk_prod1 weekparity 0
In this form I don't know how I would get the products that are available for today, for the rest of the week and next week.
Any suggestions are welcome :)
Queries are also much appreciated!
This may help out :) Might needed adjusted depending on your circumstances with the program but heres an idea:
Table 1: Products
This could hold ProductID, ProductName
So a row could look like this: 1, product1
Table 2: ProductDayMatrix (ProductDayID,ProductDay)
1,Monday
2,Tuesday
3,Wednesday
4,Thursday
5,Friday
6,Saturday
7,Sunday
Table 3: ProductWeekMatrix (ProductWeekID, ProductWeek)
1 = odd weeks
2 = even weeks
or like
1, Week1
2, Week2
3, Week3
4, Week4
Finally then Table 4:
Product Matrix: (ProductID,ProductDayID,ProductWeekID)
This could hold your products and their corresponding data. So you would insert data into this table with your information and it would look like this.
(1,4,2) = Product 1, On a Thursday, on the even weeks.
Or if things need multiple conditions have 2 different tables that would hold the day information.
Like: ProductAvailablityMatrixDay & ProductAvailabilityMatrixWeek
Then those could hold mulitple values for each so if product 1 was on monday and thursday you could insert: (1,1) & (1,4). Then reading from that table would tell you that product 1 was on day 1 and 4. Same goes with the weeks.
Would it be worth considering an approach similar to the way cron jobs are structured?
Your day of the week will always be 1-7
Your week of the month will always be 1-6
So using LIKE would give 0 possible conflicts where 1 could be matched to 10 for example as you won't ever have 2 digits.
So if your product1 it could be:
day = 1
week = 1,3,5
For product2 it would be:
day = 1,2,3,4,5,6,7
week=2,4,6
For product3 it would be:
day=6,7
week=1,2,3,4,5,6 (September this year spans 6 different weeks)
The your queries would be something like:
Today:
"SELECT * FROM availability WHERE day LIKE '%".date("N")."%' AND week LIKE %".(date("W")-date("W",strtotime("first day of the month")))."%'";
// this is using some PHP to get the week of the days of the week and week of the month.
Tomorrow:
"SELECT * FROM availability WHERE day LIKE '%".date("N",strtotime("tomorrow")."%' AND week LIKE %".(date("W",strtotime("tomorrow"))-date("W",strtotime("first day of the month")))."%'";
// this again is using some PHP to get the week of the days of the week and week of the month - you can achieve the same with just MYSQL but I'm more familiar with PHP to give you an idea.
I'm not saying its perfect but unless your going to do more with these rows I don't think the use of foreign keys is going to help you? Hope that helps?

Access SQL - DateDiff Where it only looks for values that have existed over 2 weeks in the database

SELECT SerialNumber
FROM Warranty
WHERE (ABS(DateDiff('d', EndDate1, EndDate2)) > 7)
I currently have a query that shows me the Serial Numbers that have a difference of more than 7 days between the 2 dates. I want to add another condition where it will skip checking Serial Numbers that have been added to the database in the last 2 weeks
I'm just going to go ahead and add this as an answer, to make it clearer.
SELECT SerialNumber
FROM Warranty
WHERE (ABS(DateDiff('d', EndDate1, EndDate2)) > 7)
AND MyNewDateField >= DateAdd("d", -14, Date())
Essentially, this adds -14 days to the current date (or subtracts 2 weeks), and then only takes records where the DateAdded field (aka "MyNewDateField") is greater than, or equal to, that date. So, this will keep all records where DateAdded is within the most recent 2 weeks.

Get sum of values based on the value of 2 other date related columns

Given the sample data in the screenshot below, would it be possible in mysql to return a sum of values from monthly_amount only where the values are before this month. I used a join to pull this data. The 5 left columns are from one table, and the rest are from another.
The issue I'm running into is, lets say its April of 2015, I can't just do a sum WHERE goal_year <= 2015 AND month_id_FK <= 4, or else I'll get only those 4 months from both years, when in that scenario, I really want all the months from 2014, plus the 4 months from 2015.
I could handle this in PHP, but I wanted to first see if there would be a way to do this in mysql?
try
WHERE Goal_Year*100+month_id_FK <= 201504
alternatively:
WHERE
GOAL_YEAR < 2015 OR
(GOAL_YEAR = 2015 and month_id_FK <= 4)
select sum(monthly_amount) from table where goaldate<(SELECT CURDATE())
this is not the actual query for your table..but if you do like this you will get the answer
you need the sum of monthly amount where the date is before current-date means today.
then you can just compare the currentdate with goal date

Gather individual record for the last three days

I have a table that contains three things: a start number, an end number, and a date which look something like this:
table: number2day
first last day
109288787 136388928 2013-06-29
136388929 144276079 2013-06-30
144276080 147295660 2013-07-01
Given today's date, I need to find the first value from days ago so I can compare it to a number within another query
I know that there is WHERE <col-name> IN (SUBQUERY) syntax but there is a similar statement that can use operators? >,<,=?
Something like:
WHERE num >= (SELECT first FROM number2day WHERE day = SUBDATE(CURDATE(), 3))
Here I only want to check if num is greater than first from 3 days ago. Any thoughts?
The ALL keyword should work for you here:
WHERE NUM >= ALL (SELECT first FROM number2day WHERE day = SUBDATE(CURDATE(), 3))

MySQL: Returning records from the current month and previous 3 months

I'm using PHP/MySQL booking system and i'm using a Google Line Chart to try and display the gross sales from the current month, and also the previous 3 months.
Each booking has a date in the standard phpmyadmin "date" format, so yyyy:mm:dd.
So, im looking to get 4 results from 4 queries, each query filtering out a certain month and grabbing the sum of each booking from that month.
My question is, how can i distinguish between the months in the query? How would i structure the query?
Based on the title:
select * from bookings where MONTH(CURDATE())=MONTH(booking_date);
select * from bookings where MONTH(booking_date) > MONTH(CURDATE()-INTERVAL 3 MONTH) and < MONTH(CURDATE() + INTERVAL 1 MONTH);
For simple per-month searches you can use the following:
Select * from bookings where MONTHNAME(booking_date)='July' and YEAR(booking_date)=2013;
Or:
Select * from bookings where MONTH(booking_date)=7 and YEAR(booking_date)=2013;
Also since you've already got the months, you could do this (this method requires that you maintain a table of ending dates for each month an compensate for leap year though):
select * from bookings where booking_date>'2013-06-30' AND booking_date<'2013-08-01';
In first place, excuse my english....
I know this is old thread and cant comment but, #AbsoluteƵERØ, that answer apply to the current month, in example, if i got records of July in 2013-2014-2015, the query will return the records on the month for those years.... To avoid that and using your posted code:
SELECT * FROM bookings WHERE MONTH(CURDATE()) = MONTH(booking_date) AND YEAR(CURDATE()) = YEAR(booking_date);
Note: if use the "name form" and specify the year there's no problem, like this:
SELECT * FROM bookings WHERE MONTH(CURDATE()) = MONTH(booking_date) AND YEAR(booking_date) = 2013;