Let's say I have the following table
first last value
tony jones 1
james guy 5
sara miller 6
tony jones 2
sara miller 3
joe cool 4
david marting 7
Is there a command to quickly query all duplicate rows.
So I would end up with:
first last value
tony jones 1
tony jones 2
sara miller 6
sara miller 3
Help!
This is the query I need to add this to:
SELECT l.create_date, l.id, c.first_name, c.last_name, c.state, c.zipcode,
l.source AS 'affiliateId', ls.buyer, ls.amount FROM lead_status AS ls
INNER JOIN leads AS l ON l.id = ls.lead_id
INNER JOIN contacts AS c ON c.lead_id = l.id
WHERE ls.discriminator = 'AUTO_POST' AND l.affiliate_id=1003
AND ls.winner =1 AND l.test =0 AND l.create_date BETWEEN '2011-10-03' AND '2011-10-19';
SELECT t1.*
FROM <table> t1, <table> t2
WHERE t1.first=t2.first AND
t1.last=t2.last AND
t1.value<>t2.value
;
This should do it efficiently, assuming proper indexing and that the value is unique for each row:
SELECT first,
last,
value
FROM MyTable AS T1
WHERE EXISTS ( -- Is there another row with the same first/last name,
SELECT * -- but a different value
FROM MyTable AS T2
WHERE T2.first=T1.first
AND T2.last=T1.last
AND T2.value<>T1.value
)
If you only want repeated....
select t2.first, t2.last , t1.value from (
select first, last, count(*) from your_table
group by first, last
having count(*)>1
) t2 inner join your_table t1 on (t1.first=t2.first and t1.last=t2.last)
Additionally,
select first, last , count(*) from your_table
group by first, last
Will return first, last and an extra column with the number of times the record is repeated
How about just using "SELECT DISTINCT ..." ?
Doesn't it do the job?
Related
I need to select the rows from table "web_users" only if rows of another joined table called "web_users_branches" equals to 1.
What I have now:
SELECT id, code from web_users
JOIN
(
SELECT client_code
FROM web_users_branches
HAVING COUNT(*) = 1
) as t2
ON web_users.code = t2.client_code;
I get empty result.
Database example:
Web Users table:
id code
1 0001
2 0002
3 0003
Web Users Branches table:
id client_code
1 0001
2 0001
3 0002
4 0003
5 0003
Now after this query I should get only the user which client_code is 0002, because all the other user client_code count is not equal to 1 (there is x2 0003 and x2 0001). Any ideas?
I think you just want a group by in the subquery:
SELECT u.id, u.code
FROM web_users u JOIN
(SELECT client_code
FROM web_users_branches
GROUP BY client_code
HAVING COUNT(*) = 1
) c
ON u.code = c.client_code;
SELECT id, code
FROM web_users_branches as t1
JOIN web_users as t2
ON t2.code = t1.client_code
HAVING COUNT(*) = 1
should work. After an inner join, you only get a single row when both(!) tables have exactly one record in the beginning.
I have a table with 100 000 record, I want to select only the none repeated.
In another word, if the row are duplicated did not show it at all
ID Name Reslut
1 Adam 10
2 Mark 10
3 Mark 10
result
ID Name Reslut
1 Adam 10
any ideas ?
You could join a query on the table with a query that groups by the name only returns the unique names:
SELECT *
FROM mytable t
JOIN (SELECT name
FROM mytable
GROUP BY name
HAVING COUNT(*) = 1) s ON t.name = s.name
Using the same set :
ID Name Result
1 Adam 10
2 Mark 10
3 Mark 10
4 Mark 20
I'm guessing the final solution would be:
ID Name Result
1 Adam 10
4 Mark 20
Using the above query previously suggested I modified it to take the result into consideration:
SELECT t1.*
FROM myTable t1
JOIN
(
SELECT name, result
FROM myTable
GROUP BY name, result
HAVING COUNT(*) = 1
) t2
WHERE
t1.name=t2.name and
t1.result = t2.result;
I have a table1 containing duplicate column value
Table1
id code
1 201202 0000 1111
2 201202 0000 9999
3 201203 0000 9999
4 201203 0000 0999
5 201204 1000 1999
6 201204 2000 2999
7 201205 3000 3999
8 201205 4000 4999
9 201205 5000 5999
Table 2
id numbers
1 2012020010
2 2012024929
3 2012033838
4 2012052434
5 2012052229
6 2012052232
I want to count all the numbers in table2 that are substring of distinct code in table 1
i.e. result should be
code frequency
201202 2
201203 1
201205 3
I have been able to get all the numbers for every code but can't figure out how to count them
SELECT DISTINCT table1.code , table1.id, table2.number AS ph_npa, count( * )
FROM table1
INNER JOIN table2 ON substr( table2.number, 1, 6 ) = table1.code
GROUP BY table1.number
any help is appreciated.
SELECT t1.code, COUNT(*) AS frequency
FROM table_one AS t1
LEFT JOIN table_two AS t2
ON t2.numbers LIKE CONCAT(t1.code, '%')
GROUP BY t1.code
Either use LEFT JOIN or INNER JOIN depending on if you want rows with frequency = 0 or not. All I did was basicly to run a LIKE as join condition with the % wildcard.
Try out the following. It can have performance hits on large data sets, but will get you started. It is working with my test data.
SELECT SUBSTR(t2.numbers, 1,6) AS CODE, COUNT(*) AS frequency
FROM table_2 t2
WHERE SUBSTR(t2.numbers, 1,6) IN (SELECT t1.code FROM table_1 t1)
GROUP BY SUBSTR(t2.numbers, 1,6)
Let me know if its working!
I'm not really one for using the "inner join" syntax and prefer to just use the cleaner looking implicit join on the data.
select
count(*)
from
npanxxsmall n, phone_numbers p
where
substr(n.code, 1, 6) = substr(p.number, 1, 6);
Let me know if this works!
ok I got it working and query is super fast
SELECT
DISTINCT A.code as code,
B.Counts AS frequency
FROM table1 AS A
INNER JOIN (
SELECT substr( number, 1, 6 ) AS subnumber, count( 1 ) AS Counts
FROM table2
GROUP BY substr( number, 1, 6 )
)
AS B ON A.code = B.subnumber
i.e.
select the number and frequency of number from table 2
and then join with distinct code form table 1
So I have 2 tables.
table 1:
ID CUST_NO
1 51555
2 51556
3 51111
4 44444
5 54878
6 13548
and table 2:
ID CUST_NO
1 51555
2 51556
3 31333
4 97948
5 65488
6 14648
. .....
I know I can use union to get the CUST_NO's that appear in both tables. However, I need to get the list of CUST_NO's that appear in table 1, but not in table 2.
So a result for this should be
51111
44444
54878
13548
I bet this is really easy but I just can't use my head right now, any thoughts?
select t1.CUST_NO
from Table1 t1
left outer join Table2 t2 on t1.CUST_NO = t2.CUST_NO
where t2.CUST_NO is null
select cust_no
from table1
where not exists
(select cust_no from table2 where table2.cust_no = table1.cust_no)
A NOT IN subquery is simplest, though probably not fastest:
SELECT
ID,
CUST_NO
FROM tab1
WHERE CUST_NO NOT IN (SELECT CUST_NO FROM tab2);
I have this row set:
ID player_id team_id created
1 2 1 2011-05-03 19:07:03
2 3 1 2011-05-05 12:13:18
3 2 5 2011-05-07 18:12:54
I would select player that belongs to team_id=1 but from this result i want to remove player_id=2 cause this player actually has moved and he plays in team 5 (no more in team_id=1). So the final result will be:
ID player_id team_id created
2 3 1 2011-05-05 12:13:18
How to write a query to do this? Can I write it in a single query? How?
Regards
Basically you can start by selecting everyone that has ever been a part of a team and then remove anyone who has an entry for another team that was created after their entry for the team in question. This query specifically allows you to change the team_id in only one place and have the correct result if you want to check more than one team.
SELECT
t1.*
FROM
my_table t1
WHERE
t1.team_id = 1
AND
NOT EXISTS(
SELECT
t2.id
FROM
my_table t2
WHERE
t2.player_id = t1.player_id
AND
t2.team_id != t1.team_id
AND
t2.created > t1.created);
A bit like:
SELECT allData.ID, allData.player_Id, allData.team_Id
FROM myTable allData
JOIN
(SELECT
player_id,
MAX(created) AS newestTime
FROM myTable
GROUP BY player_id) mostRecent
ON allData.player_id = mostRecent.player_id
AND allData.created = mostRecent.newestTime
WHERE team_id = 1