I am using this query to get duplicate rows in sql
SELECT
col1, col2, COUNT(*)
FROM
emp
GROUP BY
col1, col2
HAVING
COUNT(*) > 1
I have used col1, col2 here to match duplicates but what if I have 1000 more columns will I write all of there names? Like :
SELECT
col1, col2,...,col1000 COUNT(*)
FROM
emp
GROUP BY
col1, col2...,col1000
HAVING
COUNT(*) > 1
Or is there any better way?
Related
SELECT Duplicate row item from MySQL table using
SELECT * FROM `table` GROUP BY `col1`,`col2` Having COUNT(`col1`)>1 and COUNT(`col2`)>1
Actual result
The above query return first duplicate entry. from above data row 1 and row 7 contains duplicate field in same column(col1, col2).
But I need to Get last duplicate entry. Highlighted duplicate row
Expected Result
I need to get last duplicate entry.
How do you define the last duplicate? In a database table, records are not inhenrently ordered, and you did not tell which column we should use for ordering.
If you want to order by col3, then you can just use aggregation, like so:
select col1, col2, max(col3) -- or min(col3)
from mytable
group by col1, col2
-- having count(*) > 1
-- uncomment the above line if you want to see only records for which a duplicate exists
If you have some other column that you want to order with, say id, then you can filter with a correlated subquery
select col1, col2, col3
from mytable t
where id = (
select max(id) from mytable t1 where t1.col1 = t.col1 and t1.col2 = t.col2
)
I have a collection table1 with the following columns:
id (INT)
col1 (VARCHAR)
col2 (VARCHAR)
value (INT)
I want to calculate the average separately by col1 and by col2 to have a response like this:
{
averageByCol1: {col1Value1: 23, col1Value2: 44},
averageByCol2: {col2Value1: 33, col2Value2: 91}
}
Tried to use multiple columns in GROUP BY, but this combines the columns:
SELECT
CONCAT(col1, col2, AVG(value))
FROM table1
GROUP BY col1, col2
Also tried with subquery but it gives me Subquery returns more than 1 row error:
SELECT
(SELECT
CONCAT(col1, AVG(value))
FROM table1
GROUP BY col1) AS col1Averages,
(SELECT
CONCAT(col2, AVG(value))
FROM table1
GROUP BY col2) AS col2Averages;
Using Mysql v5.5.
edit with sample data:
id col1 col2 value
1 v1 b1 34
2 v2 b1 65
3 v1 b1 87
4 v1 b2 78
5 v2 b2 78
6 v1 b2 12
Want average of value by v1, v2, b1, and b2 independently.
Use a UNION for each column you want to calculate an average for
SELECT col1 as col_key, avg(value) as average
FROM test
GROUP BY col1
UNION
SELECT col2, avg(value)
FROM test
GROUP BY col2
this will work:
select avg(value),col1 from Table1 group by col1
union all
select avg(value),col2 from Table1 group by col2
sql fiddle:http://sqlfiddle.com/#!9/c1f111/5/0
If you want 2 queries for separate results:
SELECT col1, AVG(value) AS average1
from table1
GROUP BY col1
ORDER BY col1
and
SELECT col2, AVG(value) AS average2
from table1
GROUP BY col2
ORDER BY col2
I have table with columns say:
col1, col2, col3
I want to find if either values say 22 or 33 or 3 is/are in the columns.
For single value assume 22, I could have done:
SELECT * from table_name
WHERE 22 IN (col1, col2, col3)
How can I find 22, 33 in columns col1, col2, col3.
Any help is highly appreciable.
Thanks!
SELECT * from table_name
WHERE col1 IN (1,2,3) or
col2 IN (1,2,3) or
col3 IN (1,2,3)
Maybe also like this
with search as
(
select 1 v
union all
select 2 v
union all
select 3 v
)
select distinct t.*
from table_name t
join search s on t.col1 = s.v or t.col2 = s.v or t.col3 = s.v
dbfiddle demo
I have a table 'table1' as follows:
col1
----
1
1
2
2
2
3
3
I want to get count by group and total count from this table as follows:
col1 group_count total_count
-------------------------------------
1 2 7
2 3 7
3 2 7
I tried as follows:
SELECT col1, group_count, total_count FROM
(SELECT col1, COUNT(col1) AS group_count FROM table1 GROUP BY col1) Temp1,
(SELECT COUNT(col1) AS total_count FROM table1) Temp2
How to do it in optimised way
the optimized way is to first calculate the count and then simply put the variable in your select statement:
set #rowCount = (select count(col1) from table1);
select col1, count(col1), #rowCount from table1 group by col1;
See the result
The approach given by #Meherzad will calculate the row count many times. But if you want to do this in a single query u can use:
select col1, count(col1), (select count(col1) from table1) rowCount
from table1 group by col1;
Try this query
select col1, count(*), tot
from tbl t1, (select count(*) as tot from tbl) t2
group by col1
Fiddle
I have a mysql table that looks like this:
1 value1 value2 3534
2 value1 value1 8456
3 value1 value2 3566
4 value1 value3 7345
5 value2 value3 6734
I need a query to select all the rows with distinct column 2 and 3, for example the output I want for this example will look like this:
1 value1 value2 3534
2 value1 value1 8456
4 value1 value3 7345
5 value2 value3 6734
i've found a few samples on how to do it but they all select distinct on each column individually.
Update 1
Better you use this against above.
SELECT id, col2, col3, col4
FROM yourtable
GROUP BY col2, col3;
Demo
The reason I am saying is because using CONCAT, I am not getting desired result in this case. First query is returning me 5 rows however CONCAT is returning me 4 rows which is INCORRECT.
Hope you got my point.
Assumed the columns in the table are (id, col2, col3, col4).
SELECT DISTINCT(CONCAT(col2, col3)) as "dummy column", id, col2, col3, col4
FROM yourtable
GROUP BY CONCAT(col2, col3);
OR
SELECT id, col2, col3, MIN(col4)
FROM yourtable
GROUP BY col2, col3;
live working example
Assuming that the first column is unique, you can do this:
SELECT id, col2, col3, col4
FROM yourtable
WHERE id IN
(
SELECT MIN(id)
FROM yourtable
GROUP BY col2, col3
)
See it working online: sqlfiddle
Assuming the columns in the table are (id, col1, col2, col3), you could:
SELECT *
FROM YourTable yt
JOIN (
SELECT MIN(id) as minid
FROM YourTable
GROUP BY
col1, col2
) filter
ON filter.minid = yt.id
This query makes sure that the combination of column1 and column2 is unique, while selecting the minimum value of column three
SELECT col1, col2, MIN(col3)
FROM yourTable
GROUP BY col1, col2
THe simplest query for this is
SELECT col1, col2, MIN(col3)
FROM myTable
GROUP BY col1, col2
Using the group by method is returning me extra rows, where as explicitly checking each field although longer returns the same no of records as count(Distinct ..)
SELECT id, col2, col3, col4
FROM yourtable yt
WHERE id =
(
SELECT MIN(id)
FROM yourtable yt1
WHERE yt.col2 = yt1.col2
AND yt.col3 = yt1.col3
)