I have a table that holds the information of installation dates for shop displays in a store. Every shop display has a certain warranty period, which can be 6 months, 1 year, 2 years etc., which I store in another table as "6 MONTH", "1 YEAR", "2 YEAR", etc.
Is it possible to calculate the expiry dates for each shop display using a single MySQL query? I am looking for something like this:
SELECT t1.install_date, (t1.install_date + INTERVAL t2.period) as expiry_date FROM t1, t2
So, I am basically trying to treat string values that I get from the t2 table as a part of MySQL statement. Is this possible to do? For my query MySQL does not give any errors but displays the values in the expiry_date column like this: "20110228", "20110224" for the corresponding values of t1.install_date "2011-02-28", "2011-02-24".
This is harder than I thought because DATE_ADD won't accept a string as an INTERVAL unit. So the solution I came up with was to dynamically convert all your YEAR values to months by multiplying by 12.
Also, you need to specify a join condition, otherwise you will calculating intervals for the entire cartesian product of the two tables.
SELECT t1.install_date, DATE_ADD(t1.install_date,
INTERVAL IF(SUBSTRING_INDEX(t2.period,' ','-1')='YEAR',
SUBSTRING_INDEX(t2.period,' ','1')*12,
SUBSTRING_INDEX(t2.period,' ','1'))
MONTH) as expiry_date
FROM t1,t2 WHERE t1.id = t2.id
Have a look at STR_TO_DATE(str,format). But I think this will be really hard. Good luck.
Related
I have attendance data for employees stored in the table attendance with the following column names:
emp_id (employee ID)
date
type (leave, absent, etc.)
(there are others but I'm omitting them for the sake of simplicity)
My objective is to retrieve all dates of the given month on which the employee was on leave (type = 'Leave') and the last leave taken in the last month, if any.
It's easy to do it using two queries (I'm using PHP to get process the data), but is there any way this can be done in a single query?
I'm answering my own question so as to close it. As #bpgergo pointed out in the comments, UNION will do the trick here.
SELECT * FROM table_name
WHERE type="Leave" AND
date <= (CURRENT_DATE() - 30)
Select the fields, etc you want then se a combined where clause using mysql's CURRENT_DATE() function. I subtracted 30 for 30 days in a month.
If date is a date column, this will return everyone who left 1 month or longer ago.
Edit:
If you want a specific date, change the 2nd month like this:
date <= (date_number - 30)
I have 4 tables for different dates, the tables looks like this:
what I'm trying to do is to find the maximum tps for each service_name,function_name among all four days according to hour. for example in the figure I posted there is service_name(BatchItemService) in first raw that have (getItemAvailability) as function_name in date 13-06-12 01. I have same service_name for same function_name in all the other 3 tables for the same hour "01" but with different days, like day 13,14,15. I want to find maximum tps for this service_name,function_name set for hour "01" among all the four days.
I tried this, but it give me incorrect result.
SELECT
t.service_name,
t.function_name,
t.date,
max(t.tps)
FROM
(SELECT
service_name, function_name, date, tps
FROM
trans_per_hr_2013_06_12
UNION ALL
SELECT
service_name, function_name, date,tps
FROM
trans_per_hr_2013_06_13
GROUP BY service_name,function_name,date
UNION ALL
SELECT
service_name, function_name,date, tps
FROM
trans_per_hr_2013_06_14
UNION ALL
SELECT
service_name, function_name, date, tps
FROM
trans_per_hr_2013_06_15
UNION ALL
SELECT
service_name, function_name,date, tps
FROM
trans_per_hr_2013_06_16
) t
GROUP BY t.service_name,t.function_name,hour(t.Date);
Thanks a lot...
Your query looks like it should be returning what you want.
One possible issue is the type of the date column. As shown in the output, this looks like it might be stored as a character string rather than a date. If so, the following would work for the group by statement (assuming the format is as shown: DD-MM-YY H).
GROUP BY t.service_name,t.function_name, right(t.Date, 2);
As Bohemian says in the comment, this is not a good data structure. You have parallel tables and you are storing the date both in the table name and in a column. You should learn about table partitioning. This is a way that you can store different days in different files, but still have MySQL interpret them as one table. It would probably greatly simplify your using this data.
I have several rows in a table, each containing a start date and an end date. The user has a checkbox for each month of the year. I need to determine which rows contain a date range that includes any of the user's chosen months.
It's easy to check the start & end months by, for example, MONTH(start_date) IN ($month_list), but this approach won't match any months between the two dates.
So I suppose what I'm asking is: is there a way of obtaining the inclusive months from a date range purely in SQL?
I assume you would want to include data rows where the date range spans or intersects with the selected periods - in which case, I'd shove the user selected periods into a table and do a fuzzy join, something like.....
SELECT DISTINCT at.*
FROM a_table at, user_periods up
WHERE at.start_date<=up.end_date
AND at.end_date>=up.start_date
AND up.trans_id=$SOME_VAR
(the trans_id just allows the table to be used for multiple operations)
To minimise the effort here, the user_periods table should have an index on start_date and end_date, and similar for a_table.
Can something like this help?
WHERE
MONTH(start_date) < MONTH_YOU_ARE_CHECKING and
MONTH() > MONTH_YOU_ARE_CHECKING
If you need to check all at once you can do a list of all the months and after delete from the list the month that the user choose, and after compare against the list. It will be better with a pseudocode example :)
MONTHS = 1,2,3,4,5,6,7,8,9,10,11,12
USER_SELECTED_MONTHS= 1,6,8,9,12
LIST_TO CHECK = 2,3,4,5,7,10,11
so, now you can do:
MONTH(start_date) NOT IN (2,3,4,5,7,10,11)
What do you think, could it help you?
regards
Quite a simple question, but I haven't been able to find any useful resources to help with this.
Basically, I want to query my SQL database table, of which one of the fields is of type 'date'. Currently, the webpage outputs the three most recent records dependent on this date field. Ideally, I want to display all records which are under 4 months old, but I'm not sure how to go about implementing this in my query.
Can anyone advise?
SELECT * FROM table WHERE datecolumn > DATE_SUB(NOW(), INTERVAL 4 MONTH)
Will select all rows where the date is larger than 4 months ago (assuming you have no dates in the future - else you could use a BETWEEN)
I'm trying to do something like this:
SELECT MAX(
ADDDATE(expirationdate, INTERVAL 1 YEAR),
ADDDATE(now(), INTERVAL 1 YEAR)
)
That is, get "a year from now", or "a year from the expiration date stored in the table", whichever is greater (i'm renewing people's subscriptions).
This obviously doesn't work, since MAX() is for aggregation between rows, not for comparing 2 values. Is there a function that'll do this in MySQL? (i'd like to avoid doing an IF)
greatest()