show duplicates from each group in temporary table using mysql - mysql

Can some one help me with a mysql query to show duplicates from each group in temporary table using mysql
sqlfiddle
http://sqlfiddle.com/#!2/d8c9c/17
Table
TOWN ID1
town1 1
town1 1
town1 4
town2 1
town2 5
town2 8
town3 1
town3 3
town3 3
Required result
TOWN ID1
town1 1
town1 1
town3 3
town3 3
I have tried SELECT * FROM Table1 group by TOWN,ID1
but this removes duplicates and also shows non-duplicate records

This is the query you're looking for
SELECT TOWN, ID1 FROM Table1 t WHERE ( SELECT COUNT(*) FROM Table1 WHERE TOWN = t.TOWN AND ID1 = t.ID1 ) > 1;

SELECT a.TOWN, a.ID1 FROM Table1 a JOIN
(SELECT TOWN, ID1 FROM Table1 GROUP BY TOWN, ID1 HAVING COUNT(*) > 1) b
ON a.TOWN = b.TOWN
AND a.ID1 = b.ID1

Related

Using count with group without distinct elements

I have the following table:
select id1, id2 from myTable
id1 id2
1 100
2 100
3 100
3 400
4 200
I want to count how many times each case of id1 occurs, while retaining all cases of id2 in the second column such that I receive the following table:
id1 id2 count
1 100 1
2 100 1
3 100 2
3 400 2
4 200 1
The following query gives the correct count, but only shows distinct elements of id1, thus it doesn't retain all cases of id2
select id1, id2, count(id1) from myTable
group by id1
id1 id2 count
1 100 1
2 100 1
3 100 2
4 200 1
Instead I tried this query, to group by both columns, which retains all of the information, but does not give the count I desire:
select id1, id2, count(id1) from myTable
group by id1, id2
id1 id2 count
1 100 1
2 100 1
3 100 1
3 400 1
4 200 1
This feels like it should be extremely simple. Can this be done in a single query, or must I use a subquery? If so, what would the subquery look like?
Try this:
select id1, id2,
(select count(*) from mytable as t2 where t1.id1 = t2.id1) "count"
from mytable as t1
By using a subselect as a column result, you can build your desire structure in output.
Try this.
SELECT
a.*, b.count
FROM
test.id a,
(SELECT
id1, COUNT(id1) AS count
FROM
test.id
GROUP BY id1) b
WHERE
a.id1 = b.id1;
select id1, id2, count(id1) OVER (PARTITION BY id1) from myTable

Fetch data from Table 1 column's count in Table 2

Table 1 :
ID City State
1 NewYork NY
2 Oklahama OK
3 california CA
4 new jersey NJ
5 Las Vegas LA
Table 2 :
ID City
1 NewYork
2 NewYork
3 NewYork
4 Oklahama
5 Oklahama
NewYork is 3 times and Oklahama is 2 times in table 2.
So I want to fetch City List from Table 1 which Cities are used less then 5 times in Table 2
So what will exact query in Mysql?
I am using below code :
select *
from Table1
where Table1.city in
(select Table2 .city,count(*)
from Table2
having count(*) < 5
group by Table2.city )
You can use a in clause with a subselect
select * from table1
where city in (select city from table2 having count(*) < 5 group by city)
In your code you have a space between the Table2 and .city
select *
from Table1 where Table1.city in (select
Table2 .city,count(*) from Table2 having count(*) < 5 group by Table2.city )
must be
select *
from Table1
where Table1.city in (select Table2.city
from Table2
group by Table2.city
having count(*) < 5 )
no space between tablename and column ..
you can seee http://sqlfiddle.com/#!9/556cb6/10
How about
select Table1.city
from Table1 inner join Table2 on Table1.city = Table2.city
group by Table1.city
having count(Table2.city) < 5
SQLFiddle

Find duplicate records in MySQL without named column

I have a table like this:
**lead_id** **form_id** **field_number** **value**
1 2 1 Richard
1 2 2 Garriot
2 2 1 Hellen
2 2 2 Garriot
3 2 1 Richard
3 2 2 Douglas
4 2 1 Tomas
4 2 2 Anderson
Where field_number = 1 is the name and field_number = 2 is the surname.
I would like to find entries that are equal by name OR surname and group them by lead_id, so the output could be like this:
1
2
3
Any thoughts on how this can be done?
This should work and be reasonably efficient (depending upon indexes):
select distinct lead_id
from tablename as t1
where exists (
select 1
from tablename as t2
where t1.field_number = t2.field_number
and t1.value = t2.value
and t1.lead_id <> t2.lead_id
)
Select leadid from (
Select DISTINCT leadid,value from tablename
Where fieldnumber=1
Group by leadid,value
Having count(value) >1
Union all
Select DISTINCT leadid,value from tablename
Where fieldnumber=2
Group by leadid,value
Having count(value) >1
) as temp
Surely there is a faster option

mysql to select records where values are in sequence

How can I select records where ID1 values are in sequence (of two or more) where TOWN matches
My table
TOWN ID1
town1 1
town1 2
town1 4
town2 1
town2 5
town2 8
town3 1
town3 2
town3 3
required result
TOWN ID1
town1 1
town1 2
town3 1
town3 2
town3 3
sql fiddle
http://sqlfiddle.com/#!2/b409f/26
You can use an EXISTS clause to check for the next value in the sequence. This code will only match "sequences" of length >= 2, which seems to be what you want from your example.
SELECT *
FROM Table1 a
WHERE EXISTS (SELECT *
FROM Table1 b
WHERE b.TOWN=a.TOWN
AND b.ID1 IN (a.ID1 - 1, a.ID1 + 1))
ORDER BY TOWN, ID1
If you question is "give me all rows that have an adjacent id1 field for the town", then simply:
select distinct t1.*
from Table1 t1
join Table1 t2 on t2.town = t1.town and ABS(t1.ID1 - t2.ID1) = 1
order by 1, 2
See SQLFiddle for this.
To also match on another column, add the condition to the join, eg:
select distinct t1.*
from Table1 t1
join Table1 t2
on t2.town = t1.town
and t2.state = t1.state
and ABS(t1.ID1 - t2.ID1) = 1
order by 1, 2

Select all IDs that have that value in MySQL

I have these two tables:
Table 1:
ID ITEM
--------------------
1 AB
1 22S
1 AB
2 F45
2 BB
3 1
3 1
3 AA
3 F45
3 F67
3 A
......
Table 2:
ITEM COUNTRY
---------------
0 usa
1 italy
2 mexico
3 mexico
A greece
AA malta
AB usa
AC pakistan
B uk
BB france
BA france
BB russia
F45 uk
And I use the following query to get the Items that are like "A%" for each ID:
SELECT GROUP_CONCAT(ITEM)
FROM (SELECT Table1.ID, Table1.ITEM, Table1.date
FROM Table2 Table2 INNER JOIN Table1 Table1
ON (Table2.ITEM = Table1.ITEM) WHERE table2.ITEM like "A%"
ORDER BY Table1.id, Table1.date, Table2.ITEM) as temp
GROUP BY ID
ORDER BY ID
I want to find a way to edit my query so I can get the above only for the IDs that happen to have item "F45" in them (but I don't want to use this item, just select the IDs that have it)..
Any help will be appreciated
I guess
SELECT GROUP_CONCAT(ITEM) AS group_item
FROM (SELECT Table1.ID, Table1.ITEM, Table1.date
FROM Table2 Table2 INNER JOIN Table1 Table1
ON (Table2.ITEM = Table1.ITEM) WHERE table2.ITEM like "A%"
ORDER BY Table1.id, Table1.date, Table2.ITEM) as temp
GROUP BY ID
HAVING group_item LIKE '%F45%'
ORDER BY ID
Or adjust the subquery to select only needed rows (here I'm not sure what are you trying to achieve)