I need to perform the following query in mysql.
SELECT
evaluationpart.id,
evaluationpart.creation,
evaluationpart.evaluationid,
evaluationpart.partid,
evaluation.horimeter,
personcompressorpart.hourcapacity,
evaluation.evaluationdate AS changedate,
evaluation.averageworkload,
#ed := DATEDIFF(curdate(), evaluation.evaluationdate) AS elapseddays,
#uh:= #ed * evaluation.averageworkload AS usedhours,
#htu:= personcompressorpart.hourcapacity - #uh AS hourstouse,
#nc:= curdate() + INTERVAL (#htu/evaluation.averageworkload) DAY AS nextchange
FROM evaluationpart
LEFT JOIN evaluation ON evaluation.id = evaluationpart.evaluationid
LEFT JOIN personcompressorpart ON personcompressorpart.id = evaluationpart.partid
ORDER BY #nc ASC
But the Order By is not working and I'm getting this result
Could anyone tell me why?
It seems that you are not using the column name in the ORDER BY clause.
If you want to order the query result by the column named 'nextchange', the ORDER BY clause should be ORDER BY nextchange ASC.
Here's the MySQL documentation on Sorting Rows: https://dev.mysql.com/doc/refman/8.0/en/sorting-rows.html
I hope this helps.
Related
I use a Mantis Bug Database (that uses MySQL) and I want to query which bugs had a change in their severity within the last 2 weeks, however only the last severity change of the bug should be indicated.
The problem is, that I get multiple entries per bugID (which is the primary key), which is not my desired result since I want to have only the latest change per bug. This means that somehow I am using the max function and the group by clause wrongfully.
Here you can see my query:
SELECT `bug_id`,
max(date_format(from_unixtime(`mantis_bug_history_table`.`date_modified`),'%Y-%m-%d %h:%i:%s')) AS `Severity_changed`,
`mantis_bug_history_table`.`old_value`,
`mantis_bug_history_table`.`new_value`
from `prepared_bug_list`
join `mantis_bug_history_table` on `prepared_bug_list`.`bug_id` = `mantis_bug_history_table`.`bug_id`
where (`mantis_bug_history_table`.`field_name` like 'severity')
group by `bug_id`,`old_value`,`.`new_value`
having (`Severity_modified` >= (now() - interval 2 week))
order by bug_id` ASC
For the bug with the id 8 for example I get three entries with this query. The bug with the id 8 had indeed three severity changes within the last 2 weeks but I only want to get the latest severity change.
What could be the problem with my query?
max() is an aggregation function and it does not appear to be suitable for what you are trying to do.
I have feeling that what you are trying to do is to get the latest out of all the applicable bug_id in mantis_bug_history_table . If that is true, then I would rewrite the query as the following -- I would write a sub-query getLatest and join it with prepared_bug_list
Updated answer
Caution: I don't have access to the actual DB tables so this query may have bugs
select
`getLatest`.`last_bug_id`
, `mantis_bug_history_table`.`date_modified`
, `mantis_bug_history_table`.`old_value`
, `mantis_bug_history_table`.`new_value`
from
(
select
(
select
`bug_id`
from
`mantis_bug_history_table`
where
`date_modified` > unix_timestamp() - 14*24*3600 -- two weeks
and `field_name` like 'severity'
and `bug_id` = `prepared_bug_list`.`bug_id`
order by
`date_modified` desc
limit 1
) as `last_bug_id`
from
`prepared_bug_list`
) as `getLatest`
inner join `mantis_bug_history_table`
on `prepared_bug_list`.`bug_id` = `getLatest`.`last_bug_id`
order by `getLatest`.`bug_id` ASC
I finally have a solution! I friend of mine helped me and one part of the solution was to include the Primary key of the mantis bug history table, which is not the bug_id, but the column id, which is a consecutive number.
Another part of the solution was the subquery in the where clause:
select `prepared_bug_list`.`bug_id` AS `bug_id`,
`mantis_bug_history_table`.`old_value` AS `old_value`,
`mantis_bug_history_table`.`new_value` AS `new_value`,
`mantis_bug_history_table`.`type` AS `type`,
date_format(from_unixtime(`mantis_bug_history_table`.`date_modified`),'%Y-%m-%d %H:%i:%s') AS `date_modified`
FROM `prepared_bug_list`
JOIN mantis_import.mantis_bug_history_table
ON `prepared_bug_list`.`bug_id` = mantis_bug_history_table.bug_id
where (mantis_bug_history_table.id = -- id = that is the id of every history entry, not confuse with bug_id
(select `mantis_bug_history_table`.`id` from `mantis_bug_history_table`
where ((`mantis_bug_history_table`.`field_name` = 'severity')
and (`mantis_bug_history_table`.`bug_id` = `prepared_bug_list`.`bug_id`))
order by `mantis_bug_history_table`.`date_modified` desc limit 1)
and `date_modified` > unix_timestamp() - 14*24*3600 )
order by `prepared_bug_list`.`bug_id`,`mantis_bug_history_table`.`date_modified` desc
What could be wrong with my sql query here , I'd like to retrieve data from both tables meeting a WHERE condition
SELECT *, UNIX_TIMESTAMP(i.sent_date) AS udate
FROM ibc_sent_history as i INNER JOIN
ibc_messages as u
ON i.msg_ids = u.id
WHERE (i.sent_date >= '02-02-2013' AND i.sent_date <= '02-02-2014')
ORDER BY i.sent_date
LIMIT 200
Assuming your ibc_sent_history.sent_date datatype is DATETIME, here's a way to refactor this query. (This will work even if the datatype is DATE). You need to change your date input string format from 02-02-2013 to the more standard '2014-02-02` (YYYY-MM-DD).
SELECT whatever, whatever
FROM ibc_sent_history AS i
INNER JOIN ibc_messages AS u ON i.msg_ids = u.id
WHERE i.sent_date >= '2013-02-02'
AND i.sent_date < '2014-02-02' + INTERVAL 1 DAY
ORDER BY i.sent_date DESC
LIMIT 200
I changed the ORDER BY to include DESC. This is to return the most recent items, not the oldest. If that's not what you need, take off the DESC.
I changed the date formatting.
I changed the end of your selection range to
i.sent_date < '2014-02-02` + INTERVAL 1 DAY
That's because
i.sent_date <= '2014-02-02`
will include items that occur precisely at midnight on 2-Feb-2014, but won't include any other items on that day. What you probably want are items that occurred up to but NOT including midnight on the next day.
I don't know MySQL very well, but in SQL Fiddle when I run:
CAST('2014-02-02' AS DATE)
I get a date, when I run
CAST('02-02-2014' AS DATE)
I get NULL, so seems like your date format is wrong.
Demo: SQL Fiddle
In this question, I have 2 query
1) SELECT * FROM order WHERE order-date BETWEEN '12/01/2013' AND '12/31/2013'
This query give proper data from the table.
But in 2 query
2) SELECT * FROM order WHERE order-date BETWEEN '12/01/2013' AND '01/10/2014'
This query not display any date from table, how this not display any data, there is some year change problem in mysql server.
Please help me.
You have to convert string to date for comparing two dates otherwise it consider as string. For that you have to use STR_TO_DATE() function
Try this:
SELECT *
FROM `order` o
WHERE STR_TO_DATE(o.orderDate, '%m/%d/%Y') BETWEEN '2013-12-01' AND '2013-12-31'
SELECT *
FROM `order` o
WHERE STR_TO_DATE(o.orderDate, '%m/%d/%Y') BETWEEN '2013-12-01' AND '2014-01-10'
I am trying to fetch data between the (max date in the entrydate column) and (max date in the entrydate column-15days)!!
Table1:Totally it has 953rows
Table2: Totally it has 400rows
i got this error ,Please help me !!
Select
o_material_transaction_inward.Mat_Code,
s_material_details.Mat_Spec,
s_material_details.Mat_Make,
o_material_transaction_inward.Sup_Name,
o_material_transaction_inward.Entry_Date,
o_material_transaction_inward.DC_qty,
o_material_transaction_inward.Received_qty,
from
o_material_transaction_inward
join
s_material_details
on
s_material_details.Mat_Code=o_material_transaction_inward.Mat_Code and s_material_details.Mat_Group_Id=o_material_transaction_inward.Mat_Group_id
where
o_material_transaction_inward.Entry_Date between Max(o_material_transaction_inward.Entry_Date) and Max(o_material_transaction_inward.Entry_Date - 15)
Update: Fixed BETWEEN predicate:
The reason it wasn't working is that the lowest value need to be the first value in the BETWEEN predicate, so in order to get those rows from the two tables having the entry_date between the max entry date - 15 and max entry date try this:
SELECT
o.Mat_Code,
s.Mat_Spec,
s.Mat_Make,
o.Sup_Name,
DATE_FORMAT(o.Entry_Date, '%Y-%m-%d') AS Entry_Date,
o.DC_qty,
o.Received_qty
FROM o_material_transaction_inward AS o
INNER JOIN s_material_details AS s ON s.Mat_Code = o.Mat_Code
WHERE o.Entry_Date BETWEEN ((SELECT Max(Entry_Date)
FROM o_material_transaction_inward) - 15)
AND (SELECT Max(Entry_Date)
FROM o_material_transaction_inward) ;
SQL Fiddle Demo
Try:
Select
o_material_transaction_inward.Mat_Code,
s_material_details.Mat_Spec,
s_material_details.Mat_Make,
o_material_transaction_inward.Sup_Name,
o_material_transaction_inward.Entry_Date,
o_material_transaction_inward.DC_qty,
o_material_transaction_inward.Received_qty,
from
o_material_transaction_inward
join
s_material_details
on
s_material_details.Mat_Code=o_material_transaction_inward.Mat_Code and s_material_details.Mat_Group_Id=o_material_transaction_inward.Mat_Group_id
having
o_material_transaction_inward.Entry_Date between Max(o_material_transaction_inward.Entry_Date) and Max(o_material_transaction_inward.Entry_Date - 15)
(see the accepted answer of MySQL: Invalid use of group function for an explanation of where vs having)
This should work:
SELECT
o.Mat_Code,
s.Mat_Spec,
s.Mat_Make,
o.Sup_Name,
o.Entry_Date,
o.DC_qty,
o.Received_qty,
FROM o_material_transaction_inward AS o
INNER JOIN s_material_details AS s ON s.Mat_Code = o.Mat_Code
AND s.Mat_Group_Id = o.Mat_Group_id
WHERE o.Entry_Date BETWEEN (SELECT Max(Entry_Date)
FROM o_material_transaction_inward)
AND (SELECT Max(Entry_Date)
FROM o_material_transaction_inward) - 15);
I have an unavoidable situation where the dates are stored in UK date format, e.g.:
31/12/2001 00:00:00
I need it in descending order, I've tried this but it errors
select *, DATE_FORMAT(completiondate,'\%e/%c/%Y\') as cdate
from projects
where countries = 1
order by cdate desc
Error:
check the manual that corresponds to your MySQL server version for the
the right syntax to use near '' order by cdate desc'
I'm using MySQL 4.1.9
This was the end solution
select *,completiondate from projects order by str_to_date(completiondate,'%d/%m/%Y %H:%i') desc
You are escaping the % character unnecessarily. But the actual problem is that that you have an un-terminated string literal in your query:
-- this does not terminate the string ----------v
select *, DATE_FORMAT(completiondate,'\%e/%c/%Y\') as cdate
from projects
where countries = 1
order by cdate desc
Change to:
SELECT *, DATE_FORMAT(completiondate,'%e/%c/%Y') AS cdate
FROM projects
WHERE countries = 1
ORDER BY cdate DESC
Jim, your end solution was a huge help for me. My dates are in the 02/28/2013 format. I used the code:
SELECT *,str_to_date(SaleDate,'%m/%d/%Y') AS cdate FROM mytable ORDER BY cdate DESC
Thanks!
SELECT * FROM projects WHERE countries = 1 order by cdate desc
I'm guessing you have cdate already in your database? If so, you don't need to set "date_format" since it's already there.
But I may be wrong since I've never used 4.1.9