Mysql data extraction - mysql

I have a table called deals, it has records like this for example
id deal_ref objectname status
1 1234 tom correct
2 1234 tom correct
3 1234 jerry wrong
4 1234 tom correct
I need to identify all latest deals where the status is "correct for example, but the last entry(row 4) must meet the following criteria, where the Max ID is equal to the deal_ref and the status is correct
I tried this
select distinct deal_ref, deal_status
from dealv1 d
where d.deal_ref = max(id)
and d.deal_status = 'Prospect'
and date_created between '2022-11-02 00:00:00' and '2022-11-04 00:00:00'

You use other names in your SQL than in the table (deal_status, date_created).
Nevertheless try do it the following:
SELECT *
FROM dealv1 d
WHERE status = 'correct'
ORDER BY ID DESC
LIMIT 1
i donĀ“t get exactly what you are trying to do with the maxID. You just want the one row where deal_ref=max(id) and status is correct?
Then add
AND deal_ref = (SELECT MAX(id) from dealv1)
after 'correct' from the above statement.

Guys the query below worked, but its displaying multiple deal_refs that has the same deal_ref, for example 2 rows below each other with deal_ref 1234 twice.
SELECT *
FROM dealv1 d
WHERE status = 'correct'
ORDER BY ID DESC
LIMIT 1

Related

Get Unique Count Using MYSQLI Query

I have one table called quiz_data which have column called id, user_id and user_band which have value like below
id user_id user_band
1 1 A
2 2 B
3 1 A
4 3 A
I want get total unique user_band with keep in mind user_id will count only one time. from above example I want result called userband A = 2 and B = 1.
I have tried like below
select('user_band,COUNT(distinct(user_id)) as count')from quiz_data GROUP BY user_band ORDER BY user_band
but its not working properly. Let me know if someone can help me for same.
Thanks!
The syntax you want is:
select user_band, count(distinct user_id) no_users
from quiz_data
group by user_band
order by user_band

Show DISTINCT user_id ONLY if record column matches threeconditions

Fairly new to MySQL and I'm struggling with a query for table of data I'm trying to filter through. What I'd like to be able to do is identify the user_id's only where a set of conditions is met on the record column.
Example
Return a SINGLE user_id of each of the users that hold ALL of the records 1, 2 & 3.
user_id record
---------------------
1000 1
1001 1
1002 1
1003 1
1004 1
1000 2
1000 3
1002 2
1002 3
The ideal output in this example would be...
user_id
-------
1000
1002
I've tried quite a few variants using HAVING, COUNT and IN but I never seem to get the correct output and I think I'm starting to confuse myself. Anyone that could help would be greatly appreciated.
Do aggregation :
select user_id
from t
where record in (1, 2, 3)
group by user_id
having count(*) = 3; -- Use distinct inside function in case of duplicate records
If you don't know what the records are, then you can do :
select user_id
from t
group by user_id
having count(*) = (select count(distinct record) from t);
You can use HAVING Clause with a query to distinctly count all user_id values :
SELECT user_id
FROM t
GROUP BY user_id
HAVING COUNT(*) = (SELECT COUNT(distinct record) FROM t );

MySQL - Get the latest record for a given list of column values

I have the following table structure:
Request table:
id insret_time account id
------------------------------------
1 2018-04-05 08:06:23 abc
2 2018-09-03 08:14:45 abc
3 2018-08-13 09:23:34 xyz
4 2018-08-04 09:25:37 def
5 2018-08-24 11:45:37 def
I need to find the latest records for account IDs abc and def. I don't care about xyz.
I tried to get the results using group by and inner join methods but was not successful in limiting the results to just the user list I care about.
Please advice
Update:
Thanks everyone for your feedback. Appreciate it! I needed the entire row as the output. I have used id column instead of timestamp to get the latest record since its auto-incremented This is what I finally came up with that gave me the output I need:
select t.* FROM table t
join (select max(table.id) as maxNum from table
where account_id in ('abc','def') group by account_id) tm on t.id = tm.maxNum;
I think this is what you were looking for
select account_id,max(insret_time)
from table where account_id in ('abc', 'def')
group by account_id
You can use not in and ignore xyz records and placing order by desc:
select account_id,max(insert_time) from table where account_id not in ('xyz') group by account_id order by id desc
Also you can use != operators just for one expression:
select account_id,max(insert_time) from table where account_id!='xyz' group by account_id order by id desc
I hope it helps :)

How can I get the second higher number in a query

Example, Lets suppose I have a table like next one:
id | start | userId
1 3 1
2 2 2
3 5 1
Now, I want to get all the row information for the second higher value of the column start related to some userId. So, if userId equals to 1 I'm expecting to get the next result:
(id, start, userId) = (1,3,1)
I have tried this query:
SELECT id, max(start) FROM table_user WHERE userId = 1;
But this gives me the higher number.
You can do this easy using ordering by column start and the features of LIMIT, like this:
SELECT
*
FROM
table_user
WHERE
userId = 1
ORDER BY
`start` DESC LIMIT 1, 1
You can check this online also: DB-Fiddle

MySQL group multiple rows based on DISTINCT value

I need to display the last 2 results from a table (results), the results are comprised of several rows with matching submissionId, The number of rows per submission is unknown, and of course I prefer a single query.
Here is the DB table structure
submissionId input value
1 name jay
1 phone 123-4567
1 email test#gmail.com
2 name mo
2 age 32
3 name abe
3 email abe#gmail.com
4 name jack
4 phone 123-4567
4 email jack#gmail.com
Desierd results:
submissionId input value
3 name abe
3 email abe#gmail.com
4 name jack
4 phone 123-4567
4 email jack#gmail.com
Or even better, if I can combine the rows like this:
3 name abe 3 email abe#gmail.com
4 name jack 4 phone 123-4567 4 email jack#gmail.com
One option here is to use a subquery to identify the most recent and next to most recent submissionId:
SELECT submissionId, input, value
FROM yourTable
WHERE submissionId >= (SELECT MAX(submissionId) FROM yourTable) - 1
ORDER BY submissionId
Demo here:
SQLFiddle
Update:
If your submissionId column were really a date type, and you wanted the most recent two dates in your result set, then the following query will achieve that. Note that the subquery in the WHERE clause, while ugly, is not correlated to the outer query. This means that the MySQL optimizer should be able to figure out that it only needs to run it once.
SELECT submissionDate, input, value
FROM yourTable
WHERE submissionDate >=
(SELECT MAX(CASE WHEN submissionDate = (SELECT MAX(submissionDate) FROM yourTable)
THEN '1000-01-01'
ELSE submissionDate
END) FROM yourTable)
ORDER BY submissionDate
SQLFiddle
You can use limit in subqueries in the from clause, so a typical way to write this is:
SELECT submissionDate, input, value
FROM t join
(select distinct submissionDate
from t
order by submissionDate desc
limit 2
) sd
on t.submissionDate = sd.submissionDate;
This is how the query looks like now, so i can get the results with a LIMIT, RANGE, and id/timestamp (with help of Tim and Gordon):
SELECT *
FROM rmyTable t
JOIN
(SELECT DISTINCT sd.submissionId
FROM myTable sd
WHERE sd.questionId = yourId
ORDER BY sd.submissionId
LIMIT 2
) t2
ON t.submissionId = t2.submissionId
WHERE t.formId = yourId
AND dateTime BETWEEN 0000 AND 1111