can i use aggregation function (LAST) in mysql? - mysql

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

Related

Having an SQL Error or I'm missing a syntax?

I'm having a problem on using this SQL Query
DELETE FROM `acc_reg_num_db` t
WHERE EXISTS
(SELECT 1 FROM `acc_reg_num_db` tt
WHERE t.account_id = tt.account_id
AND t.key = tt.key
AND tt.value > t.value)
AND t.key IN ('#betaminutes', '#online_minute')
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 't WHERE EXISTS (SELECT 1 FROM acc_reg_num_db tt WHERE t.acco' at
line 1
Can anyone help me with this please? Thanks
MySQL requires that you list the table being deleted from. And, you cannot re-use the table being deleted from. So, try this:
DELETE arn
FROM `acc_reg_num_db` arn JOIN
(SELECT account_id, key, MAX(value) as max_value
FROM acc_reg_num_db arn2
GROUP BY account_id, key
) arn2
USING (account_id, key)
WHERE arn.value < arn2.max_value AND
arn.key IN ('#betaminutes', '#online_minute');
You have a couple issues here:
You cannot alias the table acc_reg_num_db you are deleting from
You cannot delete from the table you are selecting from in the subquery -> you need a derived table
Assuming there is an id column, this is what you need:
DELETE FROM `acc_reg_num_db`
WHERE id IN (
SELECT id
FROM (
SELECT id
FROM `acc_reg_num_db` t
WHERE EXISTS
(SELECT 1 FROM `acc_reg_num_db` tt
WHERE t.account_id = tt.account_id
AND t.key = tt.key
AND tt.value > t.value)
AND t.key IN ('#betaminutes', '#online_minute')
) as x
)
;
Also, key and value are reserved SQL keywords and you should avoid using them in column names

SQL query to fetch rows which has same IDs but different values in other columns

I am Using the below table
The case_id for two rows. If the case Id is same then I would want to fetch the row that has Test_script_type as automation and ignore the manual. How can I achieve it with a SQL query..If there is only manual fetch the manual row. How can I achieve it with a SQL query. The Output would be like :
Help is appreciated. Thanks for your time In-advance
You could adress this with not exists:
select t.*
from mytable t
where
script_type = 'Automation'
or not exists (
select 1
from mytable t1
where
t1.case_id = t.case_id
and t1.script_name <> t.script_name
and t1.script_type = 'Automation'
)
You can also filter with a correlated subquery:
select t.*
from mytable t
where t.script_type = (
select min(t1.script_type) -- This gives priority to 'Automation' against 'Manual'
from mytable t1
where t1.case_id = t.case_id
)
SELECT t1.*
FROM `table` t1
LEFT JOIN `table` t2 ON t1.case_id = t2.case_id AND t1.script_type != t2.script_type
WHERE t1.script_type = 'automation' OR t2.case_id IS NULL
You could do something like the following:
WITH cte AS (
SELECT T1.CASE_ID, T1.SCRIPT_NAME, T1.SCRIPT_TYPE,
COUNT(T1.CASE_ID) OVER (PARTITION BY T1.CASE_ID) AS cnt
FROM table1 T1
)
SELECT cte.CASE_ID, cte.SCRIPT_NAME, cte.SCRIPT_TYPE
FROM cte
WHERE (cte.cnt > 1 AND UPPER(cte.SCRIPT_TYPE) = UPPER('AUTOMATION'))
OR cte.cnt = 1
The WITH statement adds a column counting how many times the case_id value is duplicated, which helps identify the rows you want to work with.
Here is an example of it working with the data you have provided: SQLFiddle
If you are using MSSQL Server, You may try below query -
SELECT *
FROM (SELECT CASE_ID, SCRIPT_NAME, SCRIPT_TYPE, ROW_NUMBER() OVER(PARTITION BY CASE_ID ORDER BY SCRIPT_TYPE) RN
FROM YOUR_TAB) T
WHERE RN = 1

Query with aggregate, subquery and group by not working

Can you help me, please? I spent about 2 hours to understand what is wrong, but still don't.
SQLSTATE[42S21]: Column already exists: 1060 Duplicate column name
'id'
select count(*) as aggregate
from (
select `cities`.*,
`cities`.`id` as `id`,
`cities`.`country_id` as `country_id`,
`cities`.`name` as `name`,
`cities`.`alias` as `alias`,
`cities`.`active_frontend` as `active_frontend`
from `cities`
where (
cities.alias in (
select `alias`
from `cities`
group by `alias`
having COUNT(`alias`) > 1
)
)
) count_row_table
Don't ask me what the hell is going on please. Biggest part of this query is generated by Laravel.
If I delete this part:
where
(cities.alias IN (SELECT alias FROM cities GROUP BY alias HAVING
COUNT(alias) > 1))
It will work. But I need this part af.
The issue is with cities.*.
But you can simplify your query to:
select sum(cnt) as cnt
from (
select COUNT(alias) as cnt
from cities
group by alias
having COUNT(alias) > 1
) t
and avoid re-reading your table because in the end, all your need is total number of rows for which alias has more than one row.
You don't need to materialize a subquery for this. You can do:
select count(*)
from cities c
where exists (select 1 from cities c2 where c2.alias = c.alias and c2.id <> c.id);
With an index on cities(alias, id), this should have better performance.

Using Distinct in SQL query

Please find the query given below:
SELECT DISTINCT
reco_index_content_code,
reco_index_content_name,
reco_index_content_url
FROM tbl_reco_index_contents
WHERE
reco_index_user_action_at_select = 1
AND user_profile_number = 1
I need to select reco_index_content_name as distinct.
How should the above query be modified, in order to accomplish that, such that there are no duplicate reco_index_content_name rows ?
The standard solution is documented and uses an uncorrelated subquery as follows:
SELECT x.*
FROM my_table x
JOIN
( SELECT grouping_id
, MIN(ordering_id) min_ordering_id
FROM my_table
GROUP
BY grouping_id
) y
ON y.grouping_id = x.grouping_id
AND y.min_ordering_id = x.ordering_id;

Selecting maximum column and row id

Is there a way to tell MySQL that while making something like this
SELECT id, MAX(seq) FROM t1 GROUP BY ident;
I can also get the id value? I know I shouldn't be using id if it's not in a group by but I feel like its strange to make a multi pass to get the row ids with the maximum seq field when it already passed it. So what is the most effective way to do this? id is the primary key
SELECT a.*
FROM tableName
INNER JOIN
(
SELECT ident, MAX(seq) seq
FROM tableName
GROUP BY ident
) b ON a.ident = b.ident AND
a.seq = b.seq
Mabye:
SELECT MAX(a.seq), (SELECT id FROM t1 as b where b.ident=a.ident AND MAX(a.seq) = b.seq LIMIT 1) as id FROM t1 AS a GROUP BY a.ident;
Fiddle
Try using self-join:
SELECT t1.* FROM MyTable t1
JOIN
(SELECT ident, MAX(seq) AS MAX_Seq
FROM MyTable
GROUP BY ident
) t2
ON t1.seq = t2.MAX_Seq
AND t1.ident = t2.ident
See this sample SQLFiddle
What is seq exactly ?
I guess you can also order your results ?
SELECT id FROM t1 GROUP BY ident ORDER BY seq DESC
Regarding to the others answer, seq is in another table ?