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
Related
I am trying to get duplicate counts but without actually removing duplicates.
I tried using GROUP BY id and then COUNT(id) but it removes all duplicate entries.
Is there any way to not remove duplicates?
The table looks like this:
ID1 ID2 Value
1 2 someval
1 3 someval
1 4 someval
2 3 someval
2 1 someval
3 1 someval
4 1 someval
I am trying to get this:
ID1 ID2 Value COUNT
1 2 someval 3
1 3 someval 3
1 4 someval 3
2 3 someval 2
2 1 someval 2
3 1 someval 1
4 1 someval 1
I used this:
SELECT ID1, ID2, Value, COUNT(ID1) FROM table GROUP BY ID1;
One of way doing this is to have a separate query for the count and join on it:
SELECT t.id1, t.id2, t.value, cnt
FROM my_table t
JOIN (SELECT id1, count(*) AS cnt
FROM my_table
GROUP BY id1) c ON t.id1 = c.id1
You can do this with a correlated subquery in MySQL;
select id1, id2, value,
(select count(*) from table t2 where t2.id1 = t.id1) as count
from table t;
If performance is an issue then an uncorrelated subquery will likely be orders of magnitude faster than a correlated one...
SELECT x.*
, cnt
FROM my_table x
JOIN
( SELECT id1,COUNT(*) cnt FROM my_table GROUP BY id1) y
ON y.id1 = x.id1;
try something like this :
SELECT YourColumn, COUNT(*) TotalCount
FROM YourTable
GROUP BY YourColumn
HAVING COUNT(*) > 1
ORDER BY COUNT(*) DESC
I feel very very stupid now because I have a problem and cannot seem to figure it out.
Very simple MySQL table with 2 columns :
ID1 | ID2
1 | 1
1 | 2
2 | 1
Don't know very good how to explain the conditions : I want to select the value 1 from the column ID1 because it has connections with the values 1 AND 2 from ID2.
It's somewhat the opposite of IN.
If I make
SELECT ID1 FROM X WHERE ID2 IN (1,2) I recieve both 1 and 2 because it is a reunion. I want an intersection, something like SELECT ID1 FROM X WHERE ID2 IN BOTH 1 AND 2.
I am fairly sure it has something to do with grouping.
1 solution is to make
SELECT * FROM
(SELECT ID1, GROUP_COCAT(ID2) y
FROM X
GROUP BY ID1)t
WHERE t.y = '1,2'
but this is NOT ok because I do not know the order ( 1,2 or 2,1 ) and I can have more values.
Hopefully this is clear enough, I am very tired.
SELECT t.*
FROM TEMP t
WHERE t.id2 IN (1, 2)
GROUP BY t.id1 HAVING COUNT(*) = 2
OR
SELECT t.*
FROM TEMP t
WHERE t.id2 IN (1, 2, 3, 4)
GROUP BY t.id1 HAVING COUNT(*) = 4
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
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
I have a table like so;
id1 id2
1 1
2 1
1 2
3 2
1 3
4 3
1 4
5 4
I'd like to select it in a way that I'd get rows GROUPed by id2, but still preserving both values of id1 for the corresponding rows in the table.
So I'd get a result set like so;
id1 id1 id2
1 2 1
1 3 2
1 4 3
1 5 4
I've never been even half good in advanced database queries -- how would I go about achieving this?
If you have exactly 2 rows (with 2 values for id1) for every different value of id2, you can use this:
SELECT MIN(id1) AS id1_a
, MAX(id1) AS id1_b
, id2
FROM tableX
GROUP BY id2 ;
You could try using
SELECT id2, GROUP_CONCAT(id1) FROM your_table
GROUP BY id2
This way you have, for each id2 value, a column with all id1 values comma separated.
Take a look at GROUP_CONCAT syntax.
This might not be the perfect solution but in your case, it should work. This is a trick I used.
select id1, (sum(id1) - id1 ) as nID1, id2 from table_name group by id2
Hope it works.
Ujjwal