Mysql query relating to count and date time [closed] - mysql

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 7 years ago.
Improve this question
I have a table 'transaction' which contains columns transaction_id, sender_id,amount, date_time,payee_id .
I want to find transactions which are more than five made by sender_id in one day by single sender.
i tried
mysql> select * from transaction where sender_id count() =5 and datedif() =1;

I think this query will be helpful for you.
select sender_id, count(sender_id) as count
from `transaction`
group by sender_id, date(date_time)
having count>5
this will return the list of sender_ids who have more than 5 transactions
if you also need the list of that transactions than you need this
select t.*
from (
select sender_id, count(sender_id) as count
from `transaction`
group by sender_id, date(date_time)
having count>5
) as senders
inner join `transaction` as t on t.sender_id=senders.sender_id
PS. for good performance I recommend run second query on MySQL server with version 5.6.10 or higher

Related

How to create a nested select query as a new table in SQL [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 4 months ago.
The community reviewed whether to reopen this question 4 months ago and left it closed:
Original close reason(s) were not resolved
Improve this question
I have a problem about writing a nested Select queries in sql.
I tried to devise a new table to show their counts of the tables but I couldn't complete the rest part. I show it as "...."
How can I do that?
Here is the code snippet shown below.
SELECT
(SELECT COUNT(DISTINCT u.id) AS totalUsers FROM Users u),
(SELECT COUNT(DISTINCT c.id) AS totalCategories FROM Categories c)
....
I want to get this result
New Table
totalUsers totalCategories ...
60 10 ...
First you need to create a table (but check first if it exists, I leave it up to you)
CREATE TABLE CountTable (
TotalUsers int,
TotalCategories int
);
Then insert the result of your query to the new table:
INSERT INTO CountTable (TotalUsers, TotalCategories)
SELECT
(SELECT COUNT(DISTINCT u.id) AS totalUsers FROM Users u),
(SELECT COUNT(DISTINCT c.id) AS totalCategories FROM Categories c)

How to update MySQL rows from 4th row to end of the result? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 1 year ago.
Improve this question
For example, I have n rows, I need to update from the 4th row to the nth row. here n means the last row of the query. I know multi-row updates can crash applications but in my case, I am sure that I will have a max of 40 rows for 1 user.
Please check this query for user wise update where update records start from 4 and onwards. Enable WHERE clause for specific userid otherwise disable it.
-- MySQL (v5.8)
UPDATE test tt
INNER JOIN (SELECT id, userid
, ROW_NUMBER() OVER (PARTITION BY userid order by id) row_num
FROM test
-- WHERE userid = 1
) t
ON tt.id = t.id
AND t.row_num >= 4
SET tt.amount = tt.amount + 10;
Please check from url https://dbfiddle.uk/?rdbms=mysql_8.0&fiddle=e0cda11163a44f7a2b82e31c6d13ed8d

Display group data along with their last latest message [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 3 years ago.
Improve this question
I want to display result having data categorized in the group with their latest message according to date.
I tried grouping and having along with nested queries but no luck.
SELECT groupName,date FROM chat where groupName like '%he%' group by groupName,date having min(date);
I want the two rows to be printed along having the latest message with them.Here it would be row 4 and row 5. this is just an arbitrary data though which I need to impplement on bulk.
You don't need group by, just filtering:
select c.*
from chat c
where c.date = (select max(c2.date) from chat c2 where c2.groupname = c.groupname);
You can do it with NOT EXISTS:
SELECT c.*
FROM chat c
WHERE
groupName LIKE '%he%'
AND
NOT EXISTS (
SELECT 1 FROM chat
WHERE groupname = c.groupname AND date > c.date
)

Simplify SQL query for me please [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 5 years ago.
Improve this question
I have a table of multiple employment records of an employee/s where i want to query for a certain range of date.
Parameters are Company.id,date_hired,date_end
Results must be on the range of the specified date and employee.id must only be one if the employee has multiple employment records result must get the latest employment record..
THIS IS WHAT I CURRENTLY HAVE.
SELECT *
FROM employmentrecords
WHERE employmentrecords.id IN(
SELECT MAX(employmentrecords.id)
FROM employmentrecords
WHERE ((employmentrecords.date_end >='2017-08-22'
OR employmentrecords.date_end IS NULL
OR (employmentrecords.date_end <='2017-08-22'
AND employmentrecords.date_end >='2017-08-08'))
AND employmentrecords.date_hired <='2017-08-22')
GROUP
BY employmentrecords.employee_id)
AND employmentrecords.company_id<>0`
Hope any one would suggest a better approach.Thank you
I am not sure if I got your question all clear, this query below will give you the latest employee record if there are multiple user records -
SELECT * FROM employmentrecords WHERE id IN(SELECT MAX(id) FROM employmentrecords
WHERE ((date_end >='2017-08-22'
OR date_end IS NULL
OR (date_end <='2017-08-22' AND date_end >='2017-08-08'))
AND date_hired <='2017-08-22')
GROUP BY employee_id)
AND company_id<>0
and rownum = 1
order by date_hired desc
JFYI, no need to use the table name as alias if there is only one table you are fetching the data from, it just makes it hard to read the query on go.
SELECT id, max(date_hired)
FROM employmentrecords
WHERE ((employmentrecords.date_end >='2017-08-22'
OR employmentrecords.date_end IS NULL
OR (employmentrecords.date_end <='2017-08-22'
AND employmentrecords.date_end >='2017-08-08'))
AND employmentrecords.date_hired <='2017-08-22') group by id

join and calculation issues [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
Alright, after 3-4 days of trying to solve this I am giving up.
I started out with only one table but now I have changed it alot so I am able to have two tables, I thought it would make it alot easier for a rookie like me.
See my earlier post here
Instead of posting all the table details here I am providing a screenshot:
Tables and expected result
I have managed to come quite far with this but the problem is that I am not able to do the calculations excluding the batches that should not be included.
How could I solve this? I really appreciate the help I get from you guys.
Thanks in advance
First step: Create a filter for the details table
SELECT
MAX(id) AS id
FROM details
GROUP BY `concat`, `batch`
Next step: Use this to query the details table
SELECT * FROM details
WHERE id IN (
SELECT
MAX(id) AS id
FROM details
GROUP BY `concat`, `batch`
)
Next step: Use this derived table to join the master table for your final result
SELECT
`master`.id AS id,
`master`.plant AS plant,
`master`.`code` AS `code`,
COUNT(*) AS distinct_batches,
SUM(filtereddetails.volume) AS total_vol,
SUM(filtereddetails.`value`) AS total_val,
SUM(filtereddetails.volume*filtereddetails.risk) AS risk_vol,
SUM(filtereddetails.`value`*filtereddetails.risk) AS risk_val,
MAX(filtereddetails.end_date-filtereddetails.start_date) AS max_date_diff
FROM
`master`
INNER JOIN (
SELECT * FROM details
WHERE id IN (
SELECT
MAX(id) AS id
FROM details
GROUP BY `concat`, `batch`
)
) AS filtereddetails ON `master`.`concat`=filtereddetails.`concat`
GROUP BY
`master`.`concat`