MySQL joining the previous occurrence of a record - mysql

I have the query that lists that dates that a store was open and closed. What I want is the previous open date so for example for 2014-12-27 it would be 2014-12-26. The store wasn't open on New Years so for 2015-01-02 it would be 2014-12-31.
Every time I sit down to write the query I get nowhere, I have tried subselects, case statements, joining subqueries, subqueries in the where clause and I still can't figure this one out. Any help on this would be much appreciated?
EDIT
Clarifying that the column date_nk is an integer, and add the column "previous_date" to show the desired output.
date_nk weekday opened previous_open_date
1. 20141226 Friday 1 20141225
2. 20141227 Saturday 0 20141226
3. 20141228 Sunday 0 20141226
4. 20141229 Monday 1 20141226
5. 20141230 Tuesday 1 20141229
6. 20141231 Wednesday 1 20141230
7. 20150101 Thursday 0 20141231
8. 20150102 Friday 1 20141231
9. 20150103 Saturday 0 20150102
10. 20150104 Sunday 0 20150102

Please check the below.
Example in SQL Fiddle
SELECT
date_nk,
weekday,
opened,
(select max(date_nk) from OpenTable PO
where PO.date_nk < CU.date_nk and
PO.Opened = 1) PrevDate
from OpenTable CU
order by date_nk;

It doesn't look difficult if I understod you.
SELECT * FROM table WHERE opened = '1' AND date_nk < 'yourdate' LIMIT 1;
I don't knwo the type of your date_nk field, you should think about use date types but I think that should work.

Related

how to get current week and last week record in same table with different rows in mysql

Hi Everyone i am writing mysql query for getting current week and last week data in same table with different rows expected output i have mention below also i need last week data based on current week selection. current week data i am getting based on select date but didn't know how to get last week date, please help me out.
date
total_cars
online_hours
trips
current_week
20
100
200
last_week
10
60
80
query for current week record
SELECT cd.date, COUNT( cd.car_number) as total_cars, sum(cd.trips) as total_trips,
sum(cd.online_hours) as online_hours
FROM fleet_car_dash_daily as cd
WHERE cd.team_id= 1 and cd.date BETWEEN '2022-04-04' and '2022-04-10'
This can also help:
SELECT cd.date, COUNT( cd.car_number) as total_cars, sum(cd.trips) as total_trips,
sum(cd.online_hours) as online_hours
FROM fleet_car_dash_daily as cd
WHERE cd.team_id= 1 and cd.date BETWEEN '2022-04-04' - INTERVAL 7 DAY and '2022-04-10'
GROUP BY WEEK(cd.date)
ORDER BY WEEK(cd.date);

How can I get the month that is not yet updated in SQL by inserting another row on every update?

I have a table that contains records of different transaction that is needed to be updated monthly. Once the record for a specific month has been successfully updated, it will insert a new record to that table to indicate that it is already updated. Let's take this example.
**date_of_transaction** **type**
2015-04-21 1 //A deposit record
2015-04-24 2 //A withdrawal record
2015-04-29 1
2015-04-30 2
2015-04-30 3 //3, means an update record
2015-05-14 1
2015-05-22 1
2015-05-27 2
2015-05-30 2
2015-06-09 1
2015-06-12 2
2015-06-17 2
2015-06-19 2
Let's suppose that the day today is July 23, 2015. I can only get the data one month lower than the current month, so only the data that I can get are june and downwards records.
As you can see, there is an update performed in the month of April because of the '3' in the type attribute, but in the month of May and June, there are no updates occurred, how can I get the month that is not yet updated?
This will return you months, which has no type=3 rows
SELECT MONTH([trans-date]) FROM [table] GROUP BY MONTH([trans-date]) HAVING MAX([trans-type])<3
Note: this will not work if 3 is not max value in the column
My approach would be to find all the months first, then find the months whose records were updated. Then select only those months from all months whose records werent updated (A set minus operation).
Mysql query would be something like this
select extract(MONTH,data_of_transaction) from your_table_name where month not in (select extract(MONTH,data_of_transaction) from table where type=3);
You can try this;
select *
from tbl
where date_of_transaction < 'July 23, 2015'
and
date_format(date_of_transaction, '%M-%Y') in (
select
date_format(date_of_transaction, '%M-%Y')
from tbl
group by date_format(date_of_transaction, '%M-%Y')
having max(type) != 3
)
date_format(date_of_transaction, '%M-%Y') will take month-year in consideration and filter the data having type = 3.

MySQL range between dates: First order + 6 months

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

MYSQL Get upcoming birthdays (unix timestamp) from Database

I have a Database table userinfo. In that userinfo-table there are fields user_id, user_name, user_birth.
My goal is to get upcoming birthdays of the users, in my case 6 next ones. I have found examples for same kind of thing but they all have used DATE-dbformat and those examples doesnt work for me because in my table the user_birth values are in unix timestamp-format.
I would appreciate if someone has an examplecode for that how it would be possible to make.
The result what I have tried to get out would be something like(when today is 23.06):
27.06 Mike
11.10 Steve
23.10 John
25.12 Dave
07.01 Andy
12.02 Mark
Those would be the next 6 birthdays after 23.06, year should not matter.
Thanks in advance.
Well, use from_unixtimestamp() to get it in a date/time format. So, the query would be something like this:
select *
from t
order by (case when month(date(from_unixtimestamp(user_birth))) = month(now()) and
day(date(from_unixtimestamp(user_birth))) > day(now()) or
month(date(from_unixtimestamp(user_birth))) > month(now())
then 1 else 0
end) desc,
month(date(from_unixtimestamp(user_birth))),
day(date(from_unixtimestamp(user_birth)))
limit 6
The first part of the order by puts later dates in the year first. Then dates are ordered by month and day of month.

Date ranking in Access SQL?

I have a query pulling the last six months of data from a table which has a column, UseDates (so as of today in June, this table has dates for December 2011 through May 2012).
I wish to include a "rank" column that associates a 1 to all December dates, 2 to all January dates, etc -- up to 6 for the dates corresponding one month prior. If I were to open up this query a month from now, the 1 would then be associated with January, etc.
I hope this makes sense!
Example, if I ran the query right now
UseDate Rank
12/31/2011 1
1/12/2012 2
...
5/23/2012 6
Example, if I ran the query in August:
UseDate Rank
2/16/2012 1
3/17/2012 2
...
7/21/2012 6
Example, if I ran the query in March:
UseDate Rank
9/16/2011 1
10/17/2011 2
...
2/24/2012 6
SELECT
UseDates,
DateDiff("m", Date(), UseDates) + 7 AS [Rank]
FROM YourTable;
You can use month function for UseDates and subtract it from the result of now function. If it goes negative, just add 12. Also you may want to add 1 since you start with 1 and not 0. Apparently it should work for half a year date ranges. You'll get into trouble when you need to "rank" several years.
You can rank with a count.
SELECT
Table.ADate,
(SELECT Count(ADate)
FROM Table b
WHERE b.ADate<=Table.ADate) AS Expr1
FROM Table3;
You have to repeat any where statement in the subquery:
SELECT
Table.ADate,
(SELECT Count(ADate)
FROM Table b
WHERE b.ADate<=Table.ADate And Adate>#2012/02/01#) AS Expr1
FROM Table3
WHERE Adate>#2012/02/01#