I need to update row which having same value in debit column & only row which having create_date ASC basically 2019-11-15
I am having transaction table with following data
table name - tbl_transactions
id client_id user_id debit add_date
1 9991101 1 7.69 2019-11-15
2 9991101 1 7.69 2019-11-30
3 9991101 2 28.9 2019-11-15
4 9991101 2 11.49 2019-11-30
Now i just want to UPDATE record which have same value in column "debit" & date "2019-11-15"
which means only id-1 will be get updated.
id client_id user_id debit add_date
1 999110100 1 7.69 2019-11-15
We can add additional 00 to client_id field, i have tried with below sql but seems its not working
UPDATE tbl_transactions
SET client_id=999110100
WHERE id IN (
SELECT *
FROM tbl_transactions
WHERE client_id=9991101
AND DATE(create_date)='2019-11-15'
GROUP BY user_id, debit HAVING COUNT(*) = 2 )
Thanks in advance.
From what I understood from your question, you can try a query like
UPDATE
`tbl_transactions`
SET
`COLUMN_NAME` = DESIRED_VALUE
WHERE
`debit` = 7.69
AND
`add_date` = '2019-11-15'
You just need to replace COLUMN_NAME in the above query with your desired column name and it's respective value ahead of it. Do let me know if that's what you're trying to achieve or something else.
Mysql cannot specify target table for update in FROM clause.
You need to use subquery select them in a temporary table and update it,
And you can use CONCAT to concat your client_id with 00,
According to your post, you need the same debit and order by created_date ASC,
however, mysql cannot just order a field in group, so you can do it like this:
UPDATE
`tbl_transactions`
SET
`client_id` = CONCAT(client_id, '00')
WHERE id IN (
SELECT id FROM (
SELECT t2.id, t2.user_id t2.created_date FROM
(
SELECT id, user_id, MIN(created_date) AS created_date
FROM tbl_transactions
WHERE client_id=9991101
GROUP BY user_id, debit
HAVING COUNT(*) >= 2
) AS t1
INNER JOIN tbl_transactions AS t2
ON t1.user_id = t2.user_id AND t1.debit = t2.debit AND t2.client_id = 9991101 AND t1.created_date = t2.created_date
GROUP BY t2.user_id
) AS temp_table
)
Related
Let's say I have table like this:
some_id
date
1
2022-02-01
2
2022-02-02
3
2022-02-03
3
2022-02-04
3
2022-02-05
3
2022-02-06
I want to get the number of rows based on the id where the date was found?
I tried this but it's not working:
SELECT COUNT(id) FROM dates WHERE date = '2022-02-04'
Expected output should be 4 rows since there are 4 same id's where the 2022-02-04 was found.
This should do the job:
SELECT COUNT(*) FROM tbl
WHERE id IN (
SELECT id FROM tbl WHERE `date`='2022-02-04'
)
An exists query should do it:
SELECT id, COUNT(*)
FROM t
WHERE EXISTS (
SELECT 1
FROM t AS x
WHERE x.id = t.id
AND x.date = '2022-02-04'
)
GROUP BY id
Using exists logic we can try:
SELECT COUNT(*)
FROM dates d1
WHERE EXISTS (SELECT 1 FROM dates d2
WHERE d2.some_id = d1.some_id AND
d2.date = '2022-02-04');
I need to update row which having same value in debit column & only row which having create_date ASC basically 2019-11-15
I am having transaction table with following data
table name - tbl_transactions
id client_id user_id debit add_date
1 9991101 1 7.69 2019-11-15
2 9991101 1 7.69 2019-11-30
3 9991101 2 28.9 2019-11-15
4 9991101 2 11.49 2019-11-30
5 9991101 3 17.09 2019-11-15
6 9991101 3 17.09 2019-11-30
Now i just want to UPDATE record which have same value in column "debit" & date "2019-11-15"
which means only id-1 will be get updated.
id client_id user_id debit add_date
1 999110100 1 7.69 2019-11-15
5 999110100 3 17.09 2019-11-15
We can add additional 00 to client_id field, i have tried with below sql but seems its not working
UPDATE tbl_transactions SET client_id=999110100 WHERE id IN ( SELECT * FROM tbl_transactions WHERE client_id=9991101 AND DATE(create_date)='2019-11-15' GROUP BY user_id, debit HAVING COUNT(*) = 2 )
Thanks in advance.
for select you should use
select user_id, debit
from tbl_transactions
INNER JOIN (
select distinct user_id
from tbl_transactions
where add_date = "2019-11-15"
) t2 on t2.user_id = a.user_id
group by user_id, debit
HAVING COUNT(*) = 2
for update you could use
update tbl_transactions a
INNER JOIN (
select user_id, debit
from tbl_transactions
INNER JOIN (
select distinct user_id
from tbl_transactions
where add_date = "2019-11-15"
) t2 on t2.user_id = a.user_id
group by user_id, debit
HAVING COUNT(*) = 2
) t on t.user_id = a.usre_id
and t.debit = a.debit
set your_column = 'your_values'
Am trying to fetching records using select query with where clause, id duplicate records are presented then i want to filtered it by createdAt field.
while am running the below query and getting the result like this :
select * from security_development.user_requests
where
fully_approved = 1 and user_request_role=4
Please find the results
createdAt User_request_name fully_approved user_request_role
2019-02-22 18:57:08 dealerAdmin 1 4
2019-02-24 08:57:08 ask1 1 4
2019-02-25 12:20:22 dealerAdmin 1 4
If i try to avoid only the duplicate User_request_name by using max(creaTed_at) as sub query,
select * from security_development.user_requests
where fully_approved =1 and user_request_role=4
created_at =
(select max(creaTed_at) from security_development.user_requests group by user_request_name having count(user_request_name) > 1 )
then am getting the below result
createdAt User_request_name fully_approved user_request_role
2019-02-25 12:20:22 dealerAdmin 1 4
But am expecting the below results
createdAt User_request_name fully_approved user_request_role
2019-02-24 08:57:08 ask1 1 4
2019-02-25 12:20:22 dealerAdmin 1 4
You can use a correlated subquery
select t1.* from security_development.user_requests t1
where
fully_approved = 1 and user_request_role=4
and createdAt=( select max(createdAt)
from security_development.user_requests t2
where t1.User_request_name=t2.User_request_name)
Not quite the same as the question:
To delete existing duplicates keeping the latest:
DELETE t1 FROM user_requests t1
INNER JOIN user_requests t2
WHERE t1.createdAt < t2.createdAt AND t1.user_request_name = t2.user_request_name
To prevent duplicate User_request_name being inserted:
Alter table user_requests
add unique key (User_request_name);
Following database sample
id | date | customer | ...
-----------------------------------------
1 2016-07-05 1
2 2016-07-05 2
3 2016-07-06 1
4 2016-07-07 1
5 2016-07-07 2
I want to select all customers which have an entry at 2016-07-07, but not at 2016-07-06.
At first, I thought I'd do this using WHERE:
SELECT * FROM table
WHERE EXISTS ( SELECT * FROM table WHERE date = '2016-07-07' )
AND NOT EXISTS ( SELECT * FROM table WHERE date = '2016-07-06' )
GROUP BY customer
but since WHERE is executed before GROUP BY, the result can only by empty - there IS a record in table at 2016-07-06.
So, using HAVING, how would I do this? I am having difficulties checking for row existance in a HAVING clause. Something like:
SELECT * FROM table
GROUP BY customer
HAVING exists date '2016-07-07'
AND not exists date '2016-07-06'
Group by the customer and take only those groups having at least one date entry for 2016-07-07 and none for 2016-07-06
SELECT customer
FROM your_table
GROUP BY customer
HAVING sum(date = '2016-07-07') > 0
AND sum(date = '2016-07-06') = 0
SELECT customer FROM table t
WHERE EXISTS ( SELECT * FROM table t1
WHERE date = '2016-07-07' AND t.customer = t1.customer)
AND NOT EXISTS ( SELECT * FROM table t2
WHERE date = '2016-07-06' AND t.customer = t2.customer)
GROUP BY customer
Example table_1
ID Email Answer UpdateDate
1 xxx.#xx.com 1 2011-07-02
2 xxx.#xx.com 3 2011-07-11
3 vvv.#xx.com 3 2011-07-12
4 vvv.#xx.com 5 2011-07-13
5 xxx.#xx.com 5 2011-07-14
6 xxx.#xx.com 4 2011-07-14
7 xxx.#xx.com 4 2011-07-14
8 zzz.#xx.com 4 2011-07-15
How do I delete this records but keep the latest UpdateDate
And the result :
ID Email Answer UpdateDate
4 vvv.#xx.com 5 2011-07-13
7 xxx.#xx.com 4 2011-07-14
8 zzz.#xx.com 4 2011-07-15
I'd check the UpdateDate against a correlated sub-query.
CREATE TEMPORARY TABLE
latestRecord (
Email VARCHAR(128),
updateDate DATETIME
)
INSERT INTO
latestRecord
SELECT
Email,
MAX(updateDate) AS updateDate
FROM
table_1
GROUP BY
Emal
DELETE
table_1
FROM
table_1
INNER JOIN
latestRecord
ON latestRecord.Email = table_1.Email
AND latestRecord.updateDate < table_1.updateDate
EDIT
Another refactor of the same logic
You could use a temporary variable to store the highest date and then a seperate query to delete everything that is < than that. Remember that variables are connection specific.
select max(UpdateDate) from table_1 into #TempUpdateDate
delete from table_1 where UpdateDate < #TempUpdateDate
Make sure UpdateDate is a DATETIME field.
Do you want to keep the latest UpdateDate for each Answer value? This would do that:
delete from table_1 where UpdateDate not in ( select max(UpdateDate) from table_1 group by Answer );
if you only want to keep the latest date then:
delete from table_1 where UpdateDate not in ( select max(UpdateDate) from table_1 );