i have a table with date(actually they are string time in this format: 2004-Mar). I want to rank the record based on this date, so i have the following query:
SELECT *,STR_TO_DATE(detail,'%Y-%b')
FROM table2 JOIN user_table
ON table2.user_id = user_table.id
ORDER BY STR_TO_DATE(detail,'%Y-%b') DESC
WHERE table2_col = 11;
But this query doesn't work, it asks me to check syntax err near 'WHERE table2_col = 11' at line 5
If i delete ORDER BY STR_TO_DATE(detail,'%Y-%b') DESC , then everything works fine. so i think the error comes from str_to_date? what's wrong with my code?
thanks
The order by should come after the where clause
Try this::
SELECT *,STR_TO_DATE(detail,'%Y-%b')
FROM table2 JOIN user_table
ON table2.user_id = user_table.id
WHERE table2_col = 11
ORDER BY STR_TO_DATE(detail,'%Y-%b') DESC
;
Related
This has to be a no brainer, but I am stumped. I'm used to using aggregate 'FIRST' in MsAccess, but MySql has no such thing.
Here is a simple table. I want to return the most recent record based on the date,
for each unique 'group ID'. I need the three records in yellow.
I was asked to add my full query. I tried one of the suggestions using the JOIN feature replacing 't' with the temp table name, but it failed to work. "Can't reopen table 't'"
The code is below. I know it's ugly, but it does return the correct data set.
I cleaned up the code a bit and added the JOIN code. Error: "Can't reopen table 't'"
enter code here
DROP TABLE IF EXISTS `tmpMaxLookupResults`;
create temporary table tmpMaxLookupResults
as
SELECT
REPORTS.dtmReportCompleted,
RESULTS.lngMainReport_ID, RESULTS.lngLocationGroupSub_ID
FROM
(tbl_010_040_ProcedureVsTest_Sub as ProcVsSub
INNER JOIN tbl_010_050_CheckLog_RESULTS as RESULTS
ON (ProcVsSub.lngLocationGroupSub_ID = RESULTS.lngLocationGroupSub_ID)
AND (ProcVsSub.lngProcedure_ID = RESULTS.lngProcedure_ID)
AND (ProcVsSub.lngItemizedTestList_ID = RESULTS.lngItemizedTestList_ID)
AND (ProcVsSub.strPasscodeAdmin = RESULTS.strPasscodeAdmin)
AND (ProcVsSub.strCFICode = RESULTS.strCFICode))
INNER JOIN
tbl_000_010_MAIN_REPORT_INFO as REPORTS ON (RESULTS.lngPCC_ID =
REPORTS.lngPCC_ID)
AND (RESULTS.lngProcedure_ID = REPORTS.lngProcedure_ID)
AND (RESULTS.lngMainReport_ID = REPORTS.idMainReport_ID)
AND (RESULTS.strPasscodeAdmin = REPORTS.strPasscodeAdmin)
AND (RESULTS.strCFICode = REPORTS.strCFICode)
WHERE
(((RESULTS.lngProcedure_ID) = 143)
AND ((RESULTS.dtmExpireDate) IS NOT NULL)
AND ((RESULTS.strCFICode) = 'ems'))
GROUP BY RESULTS.lngMainReport_ID, RESULTS.lngLocationGroupSub_ID
ORDER BY (REPORTS.dtmReportCompleted) DESC;
SELECT t.*
FROM tmpMaxLookupResults AS t
JOIN (
SELECT lngLocationGroupSub_ID,
MAX(dtmReportCompleted) AS max_date_completed
FROM tmpMaxLookupResults
GROUP BY lngLocationGroupSub_ID ) AS dt
ON dt.lngLocationGroupSub_ID = t.lngLocationGroupSub_ID AND
dt.max_date_completed = t.dtmReportCompleted
enter code here
Try this
SELECT
tn.*
FROM
tableName tn
RIGHT OUTER JOIN
(
SELECT
groupId, MAX(date_completed) as max_date_completed
FROM
tableName
GROUP BY
groupId
) AS gt
ON
(gt.max_date_completed = nt.date_completed AND gt.groupId = nt.groupId)
You can use the following SQL.
select * from table1 order by date_completed desc Limit 1;
Use Order By
SELECT *
FROM table_name
ORDER BY your_date_column_name
DESC
LIMIT 1
In a Derived Table, get the maximum date_completed value for every group_id.
Join this result-set back to the main table, in order to get the complete row corresponding to maximum date_completed value for every group_id
Try the following query:
SELECT t.*
FROM your_table_name AS t
JOIN (
SELECT group_id,
MAX(date_completed) AS max_date_completed
FROM your_table_name
GROUP BY group_id
) AS dt
ON dt.group_id = t.group_id AND
dt.max_date_completed = t.date_completed
I have an issue with a request, it works fine with pgsql and mysql, but with sqlite, it seems like I am missing something
here is the request :
select * from mesure_insitu
where (id_formulaire, gid) IN (
select distinct id_formulaire, max(gid) as gid
from mesure_insitu
where id_dispo_comp_ouvr = 1
GROUP BY id_formulaire
ORDER BY id_formulaire ASC
)
This subquery below works fine:
select distinct id_formulaire, max(gid) as gid
from mesure_insitu
where id_dispo_comp_ouvr = 1
GROUP BY id_formulaire
ORDER BY id_formulaire ASC)
I think the problem is with the condition with two values (id_formulaire, gid), like sqlite cannot use a condition with two values.
I will appreciate any type of help.
To get this query to work, update your SQLite to a newer version.
If you cannot do this, you have to use a join instead:
SELECT *
FROM mesure_insitu
JOIN (SELECT id_formulaire, max(gid) AS gid
FROM mesure_insitu
WHERE id_dispo_comp_ouvr = 1
GROUP BY id_formulaire)
USING (id_formulaire, gid);
(The DISTINCT and ORDER BY are superfluous in this subquery.)
everbody. I have prooblem with LIKE in IF construction. I have the following query SQL:
SELECT DISTINCT a.ID_SPORTSMAN, a.NAME, a.SURNAME, a.SEX, a.CLUB, b.RESULTS
FROM `sportsman` AS `a`
JOIN `results` AS `b`
ON a.ID_SPORTSMAN = b.ID_SPORTSMAN AND b.ID_DISCIPLINE = '1' ORDER BY b.RESULTS;
IF (b.RESULTS LIKE '`%')
THEN
ASC LIMIT 10
ELSE
DESC LIMIT 10
END IF
I usage MariaDB. In phpmyadmin return me this error:
Error
Static analysis:
1 errors were found during analysis.
Unrecognized statement type. (near "IF" at position 0)
SQL query:
IF (b.REZULTAT LIKE '`%') THEN ASC LIMIT 10 ELSE DESC LIMIT 10 END IF
MySQL said: Documentation
1064 - You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use
near 'ASC LIMIT 10 ELSE DESC LIMIT 10 END IF' at line 3
What is wrong?
You can try using case in the order by:
select distinct s.ID_SPORTSMAN,
s.name,
s.SURNAME,
s.SEX,
s.CLUB,
r.RESULTS
from sportsman as s
join results as r on s.ID_SPORTSMAN = r.ID_SPORTSMAN
where r.ID_DISCIPLINE = '1'
order by case
when r.RESULTS like '`%'
then r.RESULTS
end asc,
r.RESULTS desc limit 10;
You cannot use if like this. Start with this query:
SELECT s.ID_SPORTSMAN, s.NAME, s.SURNAME, s.SEX, s.CLUB, r.RESULTS
FROM sportsman s JOIN
results r
ON s.ID_SPORTSMAN = r.ID_SPORTSMAN AND r.ID_DISCIPLINE = 1
ORDER BY r.RESULTS ASC
LIMIT 10;
And then work from there.
Notes:
I removed SELECT DISTINCT because I doubt it is necessary. If it is, use the DISTINCT.
I replaced the table aliases with meaningful table abbreviations. That makes the query much easier to follow.
I simplified the ORDER BY clause so it should work.
I removed the single quotes around "1", because it is presumably an integer.
You can modify this to get what you really want.
EDIT:
Hmmm, I think you might want this:
(SELECT s.ID_SPORTSMAN, s.NAME, s.SURNAME, s.SEX, s.CLUB, r.RESULTS
FROM sportsman s JOIN
results r
ON s.ID_SPORTSMAN = r.ID_SPORTSMAN AND r.ID_DISCIPLINE = 1
WHERE s.RESULTS LIKE '`%'
ORDER BY r.RESULTS ASC
LIMIT 10
) UNION ALL
(SELECT s.ID_SPORTSMAN, s.NAME, s.SURNAME, s.SEX, s.CLUB, r.RESULTS
FROM sportsman s JOIN
results r
ON s.ID_SPORTSMAN = r.ID_SPORTSMAN AND r.ID_DISCIPLINE = 1
WHERE s.RESULTS NOT LIKE '`%'
ORDER BY r.RESULTS DESC
LIMIT 10
);
select d.order_type from migu_td_aaa_order_log_d d where exists(select 1
from migu_user r where r.user_id = '156210106' and r.user_num =
d.serv_number) and d.product_id in ('2028594290','2028596512','2028597138' )
order by d.opr_time desc limit 1
why the above sql failed ,indicates :
FAILED: SemanticException [Error 10002]: Line 4:11 Invalid column reference 'opr_time'
but the below one works :
select temp.order_type from (
select d.* from migu_td_aaa_order_log_d d where exists(select 1 from
migu_user r where r.user_id = '156210106' and r.user_num = d.serv_number)
and d.product_id in ('2028594290','2028596512','2028597138' ) order by
d.opr_time desc limit 1) temp;
this one works fine ,too ,and much more efficient than the second one:
select d.* from migu_td_aaa_order_log_d d where exists(select 1 from
migu_user r where r.user_id = '156210106' and r.user_num = d.serv_number)
and d.product_id in ('2028594290','2028596512','2028597138' )
order by d.opr_time desc limit 1
I only need to get order_type field,so even though the second one works,but it cost much more time.
Can anyone help me?
Thanks a lot!
Your first query does not work because, in the first select statement, you are just getting one column (d.order_type), but you are trying to order by another column (d.opr_time), which you have not included in your select statement
select d.order_type from ...
...
order by d.opr_time desc limit 1
Note that if you added the column d.opr_time to your first query, it would work:
select d.order_type, d.opr_time from ...
...
order by d.opr_time desc limit 1
Your second query works because, in the subquery, you have selected all the columns of d (d.*), so when you order by opr_time, that column is present. (Same for the third query).
select temp.order_type from (
select d.* ... order by d.opr_time ...
EDITED:
According to the Hive documentation:
When using group by clause, the select statement can only include
columns included in the group by clause. Of course, you can have as
many aggregation functions (e.g. count) in the select statement as
well.
So, this query:
select d.order_type, d.opr_time from ...
...
order by d.opr_time desc limit 1
Shouldn't work either, because the select clause has an additional column (d.order_type) that is not included in the group by clause.
I hope this helps.
P.S. This answer about SQL execution order might be useful.
1.
Hive currently have an order by limitation.
The current status of this issue is PATCH AVAILABLE.
see -
"Can't order by an unselected column"
https://issues.apache.org/jira/browse/HIVE-15160
2.
You might want to get familiar with LEFT SEMI JOIN which is a cleaner syntax for EXISTS
https://cwiki.apache.org/confluence/display/Hive/LanguageManual+Joins#LanguageManualJoins-JoinSyntax
3.
using min / max over a struct / named_struct can be used instead of order by ... asc / desc and limit 1
Here is an alternative solution:
select max(named_struct('opr_time',d.opr_time,'order_type',d.order_type)).order_type
from migu_td_aaa_order_log_d d
left semi join migu_user r
on r.user_num =
d.serv_number
and r.user_id = '156210106'
where d.product_id in ('2028594290','2028596512','2028597138')
;
P.s.
You seriously want to consider to treat IDs (user_id, product_id) as numeric and not as strings.
The following SQL code comes back with everything selected being NULL and the event_size being 0. My tables are set up correctly for the following criteria. What am I doing wrong here? Thank you for your help
SELECT table_one.event_id AS event_id, table_one.event_name AS event_name, table_one.event_address AS event_address, COUNT( table_two.user_id ) AS event_size
FROM table_one
JOIN table_two
ON table_one.event_id = table_two.event_id
WHERE (
table_one.event_start_date = '5/10/2012'
OR table_one.event_mid_date = '5/10/2012'
OR table_one.event_end_date = '5/10/2012'
)
ORDER BY event_size DESC
Try adding a group by clause:
group by table_one.event_id, table_one.event_name, table_one.event_address
This should do what you want.