Here is my query for a maintenance dates list.
SELECT `checkdates`.`checkdateplanneddate`, `checkdates`.`checkdatevehicle`, `checktypes`.`checktype`, `checktypes`.`emailto`, `checktypes`.`daysnotice`
FROM `checkdates`
, `checktypes`
WHERE `checktypes`.`checktype` = `checkdates`.`checkdatechecktype`;
The idea is..
Everyday the server will email customers to let them know which checkdates are coming, based on the days notice that is set for that type of check. (see image)
Currently it is showing all checkdates.
All i need to do is filter the list so it only shows the dates that are
"Todays date plus checktypes.daysnotice"
I have tried many different queries, but cannot seem to get the right combo.
Thank you in advance
I have attached an image to show that the data is available
If I understand your question correctly, and assuming that you are running MySQL (as the use of backticks for quoting and the phpmyadmin screen copy indicate), you can use date arithmetics as follows:
SELECT cd.checkdateplanneddate, cd.checkdatevehicle, ct.checktype, ct.emailto, ct.daysnotice
FROM checkdates cd
INNER JOIN checktypes ct ON ct.checktype = cd.checkdatechecktype
WHERE cd.checkdateplanneddate = current_date + interval ct.daysnotice day
The where condition implements the desired logic.
Side notes:
Use standard, explicit joins! Implicit joins (with commas in the from clause) is a very old syntax, that should not be used in new code
Table aliases make the query easier to write and read
Related
I'm trying to do a left join in two tables, the principal one is a table with production orders and the other one contains the quantity of defective material in those production orders. It is possible to have more than one different type of defect in one production order, so that means more than one register in the second table for one production order. The relations between table are the field production order, machine, production phase, article and enterprise:
SELECT PM.FECHA_FABRICACION,
--TRUNC(PM.FECHA_FABRICACION) AS FECHA,
PM.ORDEN_DE_FABRICACION,
PM.CODIGO_FAMILIA,
PM.CODIGO_ARTICULO,
PM.COD_MAQUINA,
DECODE (PM.COD_MAQUINA,'AN001','ANODIZADO', 'GR001','ANODIZADO', 'ES001','ANODIZADO','PU001', 'ANODIZADO', 'ZZ141', 'ANODIZADO', PM.COD_MAQUINA) AS MAQUINA_PARTE,
PM.DESC_MAQUINA,
PM.CANTIDAD_ACEPTADA,
PM.M2_ACEPTADOS,
PM.M2_CONPEPTO,
PM.M2_TOTAL,
PM.M2_EXT,
PM.KILOS_ACEPTADOS,
PM.BARRAS_ACEPTADAS,
PM.FASE_REALIZADA,
PR.CODIGO_DEFECTO,
PR.CANTIDAD_RECHAZADA,
PR.LONGITUD,
PR.KILOS_RECHAZADOS,
PR.OBSERVACIONES
FROM ST_VW_PRODUCCION_MAQUINAS PM
LEFT JOIN P_INFO_RECHAZOS PR
ON PM.CODIGO_EMPRESA = PR.CODIGO_EMPRESA
AND PM.ORDEN_DE_FABRICACION = PR.ORDEN_DE_FABRICACION
AND PM.CODIGO_ARTICULO = PR.CODIGO_ARTICULO
AND PM.COD_MAQUINA = PR.CODIGO_MAQUINA
AND PM.FASE_REALIZADA = PR.FASE
AND PM.CODIGO_EMPRESA = '01'
AND PM.FECHA_FABRICACION > TO_DATE('04/07/2022 00:00:00', 'DD/MM/YYYY HH24:MI:SS')
--AND TRUNC(PM.FECHA_FABRICACION) = TRUNC(PR.FECHA_RECHAZO);
However it's not working
If you are specifically looking for rejections, you dont need a LEFT JOIN, but a normal INNER JOIN, meaning you ONLY WANT to see those projects where at least one, or more, rejections had occurred.
The query itself looks ok otherwise, just get rid of the LEFT clause of left join.
As for the function DECODE, that is using PM.COD_MAQUINA at both the begin and end of the list of codes. Dont know if that was intentional.
For date filtering, if you are only looking for a specific date without regard to any time portion, you could change it to YYYY-MM-DD format without using the TO_DATE() construct as in
AND PM.FECHA_FABRICACION > '2022-07-04'
If you still are getting errors, please EDIT your post and provide what the error message is.
To explain further, you are using a LEFT JOIN, which is a type of outer join. An outer join means that all the results from one side that don't match will still be displayed, and will just be matched to null. In your case, production orders that aren't defective will be listed.
However, it seems that you want to only display results that match on both sides (production orders that do have defects). When you just JOIN then you will have the result you want.
Here's a helpful diagram
I have a table that includes dates, I'm trying to check if a date I have falls between the dates in the table. My query is working, but it doesn't return anything. This seems like it should be very simple, but I can't wrap my head around it.
SQL query looks like this:
SELECT id FROM table
WHERE
(this_date) between (beginning_date_from_table) and (end_date_from_table)
The dates are generated dynamically in my script so I can ascertain if what I'm passing into it falls between the beginning and end dates in my table. I don't need any specific data from the table, just a boolean telling me whether the date is between the beginning and end dates or not.
You are looking for EXISTS:
SELECT
EXISTS(
SELECT id FROM table
WHERE
(this_date) between (beginning_date_from_table) and (end_date_from_table)
) AS hasValue
Hope this helps,
Check your MySQL server's date format and your generated date format.
MySQL date format is like that: '2019-01-30 18:19:52'
Also you can try change
(beginning_date_from_table) and (end_date_from_table)
to
(end_date_from_table) and (beginning_date_from_table)
Check: How do I query between two dates using MySQL?
I phrased the question slightly wrong, in that I was trying to return the result of a conditional. Once I realised how to ask google the right thing, I quickly came up with this solution:
SELECT (CASE WHEN this_date BETWEEN beginning_date AND end_date THEN 1 ELSE 0 END) AS date_result
Thanks to the other people who answered, your input put my thinking on the right track.
I have this union statement when I try to take parameters from a form and pass it to a union select statement it says too many parameters. This is using MS ACCESS.
SELECT Statement FROM table 1 where Date = Between [Forms]![DateIN]![StartDate]
UNION
SELECT Statement FROM table 2 where Date = Between [Forms]![DateIN]![StartDate]
This is the first time I am using windows DB applications to do Database apps. I am Linux type of person and always use MySQL for my projects but for this one have to use MS Access.
Is there anther way to pass parameters to UNION Statement because this method of defining values in a form can work on Single SELECT statements. But I don't know why this problem exist.
Between "Determines whether the value of an expression falls within a specified range of values" like this ...
expr [Not] Between value1 And value2
But your query only gives it one value ... Between [Forms]![DateIN]![StartDate]
So you need to add And plus another date value ...
Between [Forms]![DateIN]![StartDate] And some_other_date
Also Date is a reserved word. If you're using it as a field name, enclose it in brackets to avoid confusing the db engine: [Date]
If practical, rename the field to avoid similar problems in the future.
And as Gord pointed out, you must also bracket table names which include a space. The same applies to field names.
Still getting problems when using this method of calling the values or dates from the form to be used on the UNION statement. Here is the actual query that I am trying to use.
I don't want to recreate the wheel but I was thinking that if the Date() can be used with between Date() and Date()-6 to represent a 7 days range then I might have to right a module that takes the values from the for and then returns the values that way I can do something like Sdate() and Edate() then this can be used with Between Sdate() and Edate().
I have not tried this yet but this can be my last option I don't even know if it will work but it is worth a try. But before i do that i want to try all the resources that Access can help me make my life easy such as its OO Stuff it has for helping DB programmers.
SELECT
"Expenditure" as [TransactionType], *
FROM
Expenditures
WHERE
(((Expenditures.DateofExpe) Between [Forms]!Form1![Text0] and [Forms]![Form1]![Text11]))
UNION
SELECT
"Income" as [TransactionType], *
FROM
Income
WHERE
(((Income.DateofIncom) Between [Forms]!Form1![Text0] and [Forms]![Form1]![Text11] ));
Access VBA has great power but I don't want to use it as of yet as it will be hard to modify changes for a user that does not know how to program. trying to keep this DB app simple as possible for a dumb user to fully operate.
Any comments is much appreciated.
I would like to filter out old records and present only newer ones. The idea is currentDate minus 2 years. One record looks like this (table Symposium):
Attributes:
ID;Day;Month;Year;Firstname;Lastname;Symposium_title;Speakers;
Records
1;26;10;2012;Markus;Meier;Topic MySQL;5;
3;26;10;2011;Markus;Meier;Topic PHP;5;
6;26;01;2010;Markus;Meier;Topic CSS;5;
Wished output
1;26;10;2012;Markus;Meier;Topic MySQL;5;
3;26;10;2011;Markus;Meier;Topic PHP;5;
I could easily do a small php to filter out older data, but I'm sure there is a way to do it direct with mysql in one command.
some query like this (this is just pseudo code)
SELECT * FROM Symposium WHERE (current_data() - 2 Years);
remark: The attributes day;month;year will not be changed. There are more then 50 tables, that are build up by this attributes. So don't recommend change of the table.
Can someone help me ?
You can use STR_TO_DATE function and DATE_ADD for this task:
syntax:
STR_TO_DATE(string, format)
So in your code:
SELECT * from Symposium
WHERE DATE_ADD(STR_TO_DATE(concat(Day,'-',Month,'-',Year),'%d-%m-%Y'), INTERVAL 2 YEAR) > NOW();
(sadly, I don't have a MySQL at hand right now, so I can't test it, but unless there is some very basic typo in it, it should work)
I have a database containing a list of events, it is formatted something like this:
Datef Event Location Discipline
10/01/2012 MG Training Brooklands MG
I am running this query in order to get the results between certain dates:
SELECT * FROM events WHERE Discipline IN ('MG') AND Datef BETWEEN 01/01/2012 AND 31/01/2012
The query runs correctly and I know that there are relevant results in the database, but I receive no results when running the query in phpmyadmin (I just get told "Your SQL query has been executed successfully").
I was wondering if anyone had any idea why this wouldn't be returning results?
Update:
Putting dates in quotes (e.g. SELECT * FROM events WHERE Discipline IN ('MG') AND Datef BETWEEN 01/01/2012 AND 31/01/2012) kinda works but there's a bug.
I've certain dates doesn't work. e.g. SELECT * FROM events WHERE Discipline IN ('COMM') AND Datef BETWEEN '2012-02-01' AND '2012-02-29' shows no results, even though there is an event on 2010-02-01. Is this a peculiarity in the BETWEEN algorithm, or am I still getting my query wrong?
Without quotes or anything designating those values as dates, it will simply do the math on the integers you wrote.
In other words, you ask for
BETWEEN 01/01/2012 AND 31/01/2012
So you get 1/1=1, then 1/2012 which is almost 0.
Then you do 31/1=31, then 31/2012 which is also almost 0.
So it becomes
BETWEEN 0 and 0
Try wrapping your code strtotime(). MySQL and PHP use different formatting.
For some additional info, here are a couple of links:
http://www.richardlord.net/blog/dates-in-php-and-mysql
http://www.binarytides.com/blog/parse-and-format-dates-between-php-and-mysql/
Your column Datef is of the wrong type and should be datatime
SELECT
*
FROM
`events`
WHERE
`Discipline` IN ('MG')
AND
`Datef` BETWEEN '2012-01-01' AND '2012-01-31'