I want to retrieve duplicates for all column 1 2 and 3 and 4
What I have is this query
But that is not the result i want i just want this query 2
I want to retrieve same name, same dob but different fname
This is what i have in my code :
SELECT *
FROM demo WHERE (name, dob) in
(SELECT name, dob
FROM demo
GROUP BY name, dob
HAVING count(*) > 1)
ORDER BY name ASC
This query is based on self join, when it joins this table it will iterate each record and compare with it. Based on question requirement, I have made conditions and if it finds same "name" and "dob" with different "fname", it will be in the last result.
select a.* from TestStack a, TestStack b where a.name = b.name
and a.dob = b.dob and a.fname <> b.fname
Related
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
select max(c),
name
from(
select
name,
count(*) as c
from(
book join owns using (title)
)
join person using(ssn)
where author='X'
groupby (name)
) as counts:
But it showing max count and first name in the table instead of corresponding name.
Here it shows output manny 14, but it showing max value but first name in the table you can find that in below picture.
I think you can try this -
Instead of giving
select max(c), name
give,
select c, name
...
order by c desc limit 1;
Ive got a table like this, where I'm looking for unnecessary duplicate rows:
I want to find any rows where the First Name, Last Name, and Occupation columns are identical - in this case rows 1 and 3. I don't want to specify what the identical values should be as I dont know.
I've tried the answer to this question, but I dont think it applies to this case.
simple solution is to add a HAVING clause where there are duplicates after grouping by all three columns
SELECT
ID, FirstName, LastName, Occupation, Age
FROM table1
GROUP BY
FirstName,
LastName,
Occupation
HAVING COUNT(*) > 1
here is a DEMO with two duplicate rows to ensure it works properly
EDIT:
my first understanding was you wanted one row returned when it has duplicates.. if you want a query that will return all duplicate rows..
then here it is... this will return rows 1 and 3
SELECT p1.* FROM people p
JOIN people p1
ON p1.firstname = p.firstname
AND p1.lastname = p.lastname
AND p1.occupation = p.occupation
GROUP BY id
HAVING COUNT(*) > 1;
another DEMO
Self join, 3 times (untested): SELECT a.* from your_table a, your_table b, your_table c, your_table d
where
a.fname = b.fname and a.lname=c.lname and a.occupation=d.occupation
I have a single table and want to get distinct values from each column.
select distinct 'customer', customer from sales
union
select distinct 'product',product from sales
In the example, MySQL does two full passes of the (huge) table.
I'm trying to get distinct values from many columns, so in fact the query would do 6 full passes of the table. Indexing isn't an option in this case.
How can I persuade MySQL to do this in a single pass ?
You can try unpivot approach
SELECT DISTINCT
CASE WHEN type = 1 THEN 'customer'
WHEN type = 2 THEN 'product'
END type,
CASE WHEN type = 1 THEN customer
WHEN type = 2 THEN product
END value
FROM sales s CROSS JOIN
(
SELECT 1 type UNION ALL
SELECT 2
) t
ORDER BY type
Here is SQLFiddle demo
I have collected informations from different sources about certain IDs that should match a single name. Some sources are more trustworthy than others in giving the correct name for a given ID.
I created a table (name, id, source_trustworthiness) and I want to get the most trustworthy name for each ID.
I tried
SELECT name, id, MAX( source_trustworthiness )
FROM table
GROUP BY id
this returns th highest trustworthiness available for each ID but with the first name it finds, regarless of its trustworthiness.
Is there a way I can get that right ?
Mysql has special functionality to help:
SELECT * FROM (
SELECT name, id, source_trustworthiness
FROM table
ORDER BY 3 DESC ) x
GROUP BY id
Although this wouldn't even execute in other databases (not naming all non-aggregate columns in the GROUP BY clause), with mysql it returns the first row encountered for each unique value of the grouped by columns. By ordering the rows greatest first, the first row for each id will be the most trustworthy.
Since this question is tagged mysql, this query is OK. Not only is it really simple, it's also quite fast.
SELECT a.*
FROM TableName a
INNER JOIN
(
SELECT id, MAX(source_trustworthiness) max_val
FROM TableName
GROUP BY ID
) b ON a.ID = b.ID AND
a.source_trustworthiness = b.max_val