I want to update table1 by taking max(date) and min(Date) from Table2 for only if table1.status=100 and table1.Todate='0000-00-00 00:00:00'
same thing I am trying with following query but it is giving error in group by
update table1 s
left join table2 t
on s.stCode=t.tsTask
set s.stActFrom= min(t.tsDate),s.stActTo=max(t.tsDate)
WHERE s.stActTo='0000-00-00 00:00:00' and s.stStatus=100
group by t.`tsTask`
if i execute this query i am getting following error.
#1064 - You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'group by t.tsTask' at line 1
Can any one tell me how i can write this update query?
update s
set s.stActFrom= t.MinDate,
s.stActTo = t.MaxDate
FROM table1 s left join
(
SELECT MinDate = min(tsDate), MaxDate = max(tsDate)
FROM table2
group by `tsTask`
) AS t
WHERE s.stActTo='0000-00-00 00:00:00' and s.stStatus=100
ON s.stCode=t.tsTask
I have tried the above answers. That is not given me the required answer. So i have got answer from the following query.
update table1 s left join
(
SELECT min( tsDate ) AS MinDates, max( tsDate ) AS MaxDates, tsTask
FROM table2
group by `tsTask`
) AS t ON s.stCode=t.tsTask
set s.stActFrom= t.MinDate,
s.stActTo = t.MaxDate
WHERE s.stActTo='0000-00-00 00:00:00' and s.stStatus=100
Related
I am trying to migrated MySQL 5.0 views to Oracle 12c. This is the syntax I have in MySQL:
select count(`table1`.`uuid`) AS `Count`,
`table2`.`description` AS `STATUS`,
`table3`.`name` AS `Court`
from ((`table2` join `table1`)
join `table3`)
where ((`table1`.`statusCode` = `table2`.`code`)
and (`table3`.`uuid` = `table1`.`courtUuid`))
group by `table1`.`courtUuid`,`table2`.`description`;
This is the syntax I have for Oracle 12c:
select count(table1.uuid) AS Count,
table2.description AS STATUS,
table3.NAME as Court
from table2
join table1
on (table1.statuscode = table2.code)
join table3
on (table3.uuid = table1.courtuuid)
group by table1.courtuuid,table2.description;
When I run the Oracle 12c syntax, I get:
ORA-00979: not a GROUP BY expression
00979. 00000 - "not a GROUP BY expression"
*Cause:
*Action:
Error at Line: 3 Column: 1
However, if I add table3.NAME as part of the group by, I get 866 rows. The query in MySQL produces 1025 rows.
select count(table1.uuid) AS Count,
table2.description AS STATUS,
table3.NAME as Court
from table2
join table1
on (table1.statuscode = table2.code)
join table3
on (table3.uuid = table1.courtuuid)
group by table1.courtuuid,table2.description,table3.NAME;
Can someone help me figure this one out?
You probably want:
select count(table1.uuid) AS Count,
table2.description AS STATUS,
table3.NAME as Court
from table2 join
table1
on table1.statuscode = table2.code join
table3
on (able3.uuid = table1.courtuuid
group by table3.name, table2.description;
The unaggregated columns in the select should be in the group by. Unfortunately, MySQL has a (mis)feature that does not require this logic.
Strictly speaking, you could also do:
select count(table1.uuid) AS Count,
table2.description AS STATUS,
max(table3.NAME) as Court
from table2 join
table1
on table1.statuscode = table2.code join
table3
on table3.uuid = table1.courtuuid
group by table1.courtuuid, table2.description;
This is more equivalent to your query, but the first version is probably what you intend.
i have been trying to find the 3rd highest cost of the product sold.
from table tblproducts.i am using mysql server 5.6.
i used the below query:
select name
from tblproducts
where cost IN
(select distinct top 3 cost
from tblproducts
order by cost desc);
but when i run the query it shows the below error:
ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that
corresponds to your MySQL server version for the right syntax to use near '3 cos
t
from tbbproducts
order by cost dec)' at line 4
.....................
someone kindly help me with the syntax to correct this error.
thankyou.
SELECT t1.name
FROM tblproducts t1
WHERE (3) = ( SELECT COUNT(t2.cost)
FROM tblproducts t2
WHERE t2.cost >= t1.cost
)
Demo here:
SQLFiddle
Update:
The above query works fine so long as there is no possibility for products with duplicate costs in tblproducts. If there be the chance for cost duplicates, then we can modify the query as follows:
SELECT t1.name
FROM tblproducts t1
WHERE (3) = ( SELECT COUNT(t2.cost)
FROM (SELECT DISTINCT cost FROM tblproducts) t2
WHERE t2.cost >= t1.cost
)
I have a problem with this delete query:
DELETE r
FROM table AS r
WHERE r.stoptime IS NULL
and r.address IN
(select address from table where starttime <= r.starttime and stoptime > r.starttime)
I get the following error:
Error : You can't specify target table 'r' for update in FROM clause.
My goal is to delete records that the starttime is contained in another record but I got an error with the alias in the subquery.
Somebody know how to do this? Thanks in advance.
Try to use JOINS like this:
DELETE r
FROM `table` r
JOIN `table` t ON t.id = r.id
WHERE t.starttime <= r.starttime and t.stoptime > r.starttime
AND r.stoptime IS NULL
can i use aggregation function (LAST) in mysql??
if yes then why give me error for following query::
SELECT `user_id`,last(`value`)
FROM `My_TABLE`
group by `user_id`
ERROR:: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '(value) FROM My_TABLE group by user_id' at line 1
EDIT:: I got answer "last" is not used in MySql. then How to perform it in MySql??
No, There is nothing called LAST in mysql
See the list of aggregated function
EDIT
You can perform the same something like this
select f.user_id, f.value
from (
select MAX(value) as maxval
from my_table group by user_id
) as x inner join my_table as f on f.value = x.maxval
Something like this -
SELECT * FROM table1 t1
JOIN (SELECT depno, MAX(id) max_id FROM table1 GROUP BY depno) t2
ON t1.depno = t2.depno AND t1.id = t2.max_id
There is no "last" function defined in MySQL. Are you just trying to get the last (newest) record?
If so:
SELECT `user_id`, `value`
FROM `My_TABLE`
ORDER BY `user_id` DESC
LIMIT 1;
or
SELECT `user_id`, `value`
FROM `My_TABLE`
WHERE `user_id` = (SELECT MAX(`user_id`));
Because there is no such function called as last() in mysql..
Try to use group, order by clause in mysql
The best way I found how to handle this:
SELECT user_id, json_unquote(json_extract(json_objectagg('value', value), '$.value'))
FROM my_table
GROUP BY user_id
GOT error for the following query in MYSQL(version5.1)
SELECT year,month,sum(fact_1),sum(fact_2),sum(fact_3),sum(fact_4)
from(
select year,month,fact_1,fact_2,0 as fact_3,0 as fact_4 from table_1
intersect
select year,month,0 as fact_1,0 as fact_2,fact_3,fact_4 from table_2
) as combined_table
group by month,year
Error Line with code#1064:-
You have an error in your SQL syntax;
check the manual that corresponds to
your MySQL server version for the
right syntax to use near 'select
year,month,0 as fact_1,0 as
fact_2,fact_3,fact_4 from table_2 ) as
ct g' at line 5
but following query was giving desired Result:-
SELECT year,month,sum(fact_1),sum(fact_2),sum(fact_3),sum(fact_4)
from(
select year,month,fact_1 ,fact_2,0 as fact_3,0 as fact_4 from table_1
union
select year,month,0 as fact_1,0 as fact_2,fact_3,fact_4 from table_2
) as ct
group by month,year
Can anybody tell what error i am committing?
can Anybody help me to understand the root cause behind the Problem.
you can fake INTERSECT quite easily using an INNER (self) JOIN, this way you’ll only get rows from both resultsets:
SELECT `a`.`id`, `a`.`name`
FROM `a`
INNER JOIN `b`
USING (`id`, `name`)
MINUS can be faked with a LEFT JOIN:
SELECT DISTINCT `a`.`id`, `a`.`name`
FROM `a`
LEFT JOIN `b`
USING (`id`, `name`)
WHERE `b`.`id` IS NULL
MySQL doesn't support the INTERSECT keyword. Full syntax of SELECT for 5.1 is here:
http://dev.mysql.com/doc/refman/5.1/en/select.html