SQL query to compare 2 columns - mysql

I have a table that has 3 columns id, visit date and appointment date.
I want to write a sql query to find the following
For each id, how many entries are there where visit date matches with appointment date. Please note that the comparison has to happen across multiple rows for each id.
Example:
id Visit-date Appointment-date
1 20-1-2016 20-2-2016
1 20-2-2016 30-3-2016
1 04-04-2016 05-05-2016
and so on
Thanks a lot for the help.
Prasad

You can use a left join with aggregation to get the desired results:
select a.id,
count(b.appointment_date) as count
from your_table a
left join your_table b on a.id = b.id
and a.visit_date = b.appointment_date
group by a.id;

You can count records grouping by id.
select id, count(*) num_records
from your_table
where visit_date = appointment_date
group by id

Related

SQL Select two records if they have certain time difference of column A and have same column B value

I have 14000 records in my sql table. They have columns ID, test_subject_id and date_created. I want to fetch all the records that have been created within a time difference of 3 minutes(difference in date_created values) and both records should have the same test_subject_id.
You should use a self join, I assume inner join is what will work for you:
SELECT a.ID, a.date_created, b.ID, b.date_created
FROM accounts a
INNER JOIN accounts b
ON a.test_subject_id = b.test_subject_id
AND TIMESTAMPDIFF(MINUTE,a.date_created,b.date_created) = 3
Note: TIMESTAMPDIFF is used assuming date_created has type datetime, details here.
You can use EXISTS:
SELECT t1.*
FROM tablename t1
WHERE EXISTS (
SELECT 1
FROM tablename t2
WHERE t2.test_subject_id = t1.test_subject_id
AND ABS(TIMESTAMPDIFF(SECOND, t1.date_created, t2.date_created)) <= 180
)
ORDER BY t1.test_subject_id, t1.date_created;

How to compare an having count with subrequest in MySQl

I'm trying to compare 2 values, the 1st one will be a count() in one request, and the 2nd is a field value from another request/table but they must be related. Here's what I'm trying:
SELECT count(id), lead_id
FROM calls_leads
group by lead_id
having count(id) = (
Select touches_phone_c, id_c
from leads_cstm
where lead_id = id_c)
Where the lead_id and id_c are the same IDs but from 2 different tables.
Thank you!
Try this:
SELECT t.*
FROM (SELECT COUNT(id) AS id_cnt, lead_id
FROM calls_leads
GROUP BY lead_id) t INNER JOIN leads_cstm l
ON l.id_c = t.id_cnt

UPDATE table using IN and COUNT

I am updating my table setting a field named "status" based on the condition that the total number of distinct rows should be more than 10 and less than 13. The query is as follows:
update myTable set status='Established'
where id IN(select id, count(*) as c
from myTable
where year>=1996 and year<=2008
group by id
having count(distinct year)>=10 and count(distinct year)<=13)
The problem is, I'm getting error1241 that is "operand should contain 1 column"! Could you please advise how can I solve this? Thanks!
The result of the sub query must return only 1 column :
update myTable set status='Established'
where id IN(select id
from myTable
group by id
having count(distinct year)>=10 and count(distinct year)>=13)
In MySQL, an update with a join often performs better than an update with a subquery in the where clause.
This version might have better performance:
update myTable join
(select id, count(*) as c
from myTable
where year >= 1996 and year <= 2008
group by id
having count(distinct year) >= 10 and count(distinct year) <= 13
) filter
on myTable.id = filter.id
set status = 'Established';
I will also note that you have a table where a column called id is not unique among the rows. Typically, such a column would be a primary key, so the having clause would always fail (there would only be one row).
update myTable
set status='Established'
where id IN(select id from myTable
group by id
having count(distinct year)>=10
and count(distinct year)>=13)
You are using IN operator and then you inner query returns two columns id and count(*) it should return only one column back.

2 rows having unique field and recent date

Table :
I am new to query writing. Now I am stuck on retrieving 2 rows from above table.
Data will be date sorted in descending order for only 2 different topic_id. There won't be a third different topic_id.
So I want to retrieve two rows only that will have different topic_id, one data for each topic_id having most recent date.
The result would be
try sql fiddle
http://sqlfiddle.com/#!2/f37963/9
SELECT t1.* FROM temp t1
JOIN (SELECT question_id, MAX(`date`) as `date` FROM temp GROUP BY topic_id) t2
ON t1.question_id= t2.question_id AND t1.`date`= t2.`date`;
The logic is to find the latest date in each group (subquery) and join it with the table again to retrieve other particulars.
use this
$qry="SELECT * FROM table_name GROUP BY TOPIC_ID ORDER BY DATE desc";

Help with a MySql Query - The Rows Holding the Group-wise Maximum of a Certain Column

I need help returning a relevant result for this query. I have one table that I am hitting with three columns. trans_date, trans_amount and user_id
what I am trying to determine is this. For a given user_id when was the last trans_date and what was the trans_amount.
I'm having trouble returning the correct transaction_amount. Here is my code so far. It's returning the correct date but the amount is not right
select user_id, trans_date, trans_credit
from table
WHERE trans_credit =
(select max(trans_date) from inclick_account_act as f
where f.user_id = table.user_id);
Thanks in advance
If I understand you correctly you just want to get the most recent transaction for all users.
SELECT user_id, trans_date, trans_credit
FROM `table`
GROUP BY user_id
ORDER BY trans_date DESC;
How about something like
SELECT t.*
FROM table t INNER JOIN
(
SELECT user_id,
MAX(trans_date) max_trans_date
FROM table
GROUP BY user_id
) MaxDates ON t.user_id = MaxDates.max_trans_date