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
Related
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?
Sorry for the poor formatting but as part of a larger problem, I have created a query that produces this table:
id id2
4 7
4 6
1 3
1 2
1 1
How would I extract the rows that don't have the highest id2 for each id1.
What I want:
id id2
4 6
1 2
1 1
I can only seem to figure out how to get rid of the max id2 overall but not for each distinct id1. Any help on actually differentiating the max id2 for each id1 would be appreciated.
You can try below way -
select a.id, a.id2
from tablename a
where a.id2 <> (select max(a1.id2) from tablename a1 where a.id=a1.id)
If you are using MySQL 8+, then RANK() provides one option:
WITH cte AS (
SELECT id, id2, RANK() OVER (PARTITION BY id ORDER BY id2 DESC) rnk
FROM yourTable
)
SELECT id, id2
FROM cte
WHERE rnk > 1
ORDER BY id DESC, id2 DESC;
Demo
instead of a correlated subquery in the where, you can LEFT JOIN and apply not in...
select id, id2
from yourTable YT
LEFT JOIN
( select id, max( id2 ) highestID2
from YourTable
group by id ) TopPerID
on YT.ID = TopPerID.ID
AND YT.ID2 != TopPerID.highestID2
where TopPerID.id IS NULL
Since you can have id values with only one id2 value, you need to check for that situation as well, which you can do by comparing the MAX(id2) value with the MIN(id2) value in a JOIN:
SELECT t1.*
FROM Table1 t1
JOIN (SELECT id, MAX(id2) AS max_id2, MIN(id2) AS min_id2
FROM Table1
GROUP BY id) t2 ON t2.id = t1.id
AND (t1.id2 < t2.max_id2 OR t2.min_id2 = t2.max_id2)
If we add a row 2, 5 to your sample data this correctly gives the result as
id id2
4 6
1 2
1 1
2 5
Demo on SQLFiddle
How would I find the average from this query using SQL:
SELECT count(col1) as count FROM table1 GROUP BY col1 HAVING count > 1
I'm trying to find the average number of rows per col1
So far I managed to find the total amount of rows per col1, now I just need the avg
select avg( c )
from ( SELECT count(col1) as c FROM table1 GROUP BY col1 HAVING count > 1 )
For table you need alias and change count to count(*).
Query:
SELECT avg(t1.c) AS avgcol1
FROM
(SELECT count(col1) AS c
FROM table1
GROUP BY col1 HAVING COUNT(*) > 1) t1
I have a table with a list of names spread across five different columns. I'm trying to get the 6 most frequent distinct names. Each name will only appear in each record once. The five columns are name_1, name_2...name_5. And just for names sake call the table 'mytable'.
Any help would be much appreciated.
Here's one approach:
SELECT name, COUNT(1)
FROM ( SELECT name_1 AS name FROM mytable
UNION ALL SELECT name_2 AS name FROM mytable
UNION ALL SELECT name_3 AS name FROM mytable
UNION ALL SELECT name_4 AS name FROM mytable
UNION ALL SELECT name_5 AS name FROM mytable
) AS myunion
GROUP BY name
ORDER BY COUNT(1) DESC LIMIT 6
;
How many rows are there in the table?
try this:
SELECT iTable.iName, Count(iTable.iName) as TotalCount
FROM
(
SELECT DISTINCT name_1 as iName FROM myTable
UNION
SELECT DISTINCT name_2 as iName FROM myTable
UNION
SELECT DISTINCT name_3 as iName FROM myTable
UNION
SELECT DISTINCT name_4 as iName FROM myTable
UNION
SELECT DISTINCT name_5 as iName FROM myTable
) as iTable
GROUP BY iTable.iName
ORDER BY TotalCount DESC
LIMIT 6
You should be able to select all the names from each table and union the results together. Then you can count the number of times each name occurs.
select *
from
(
select name, count(*)
from (
select name from table1
union all
select name from table2
union all
select name from table3
union all
select name from table4
union all
select name from table5
)
group by name
order by count(*) desc
)
where rownum <= 6
UNION + subselect should work for you in this case.
SELECT name_1, COUNT(*) FROM (
SELECT name_1 FROM mytable
UNION ALL SELECT name_2 FROM mytable
UNION ALL SELECT name_3 FROM mytable
UNION ALL SELECT name_4 FROM mytable
UNION ALL SELECT name_5 FROM mytable
) AS names GROUP BY name_1 ORDER BY 2 DESC LIMIT 6;
i am trying to run a sql query which will not show distinct/duplicate values.
For example if using distinct option it would display only one unique result, but i would like to skip all detected distinct values i.e dont display distinct values
is it possible?
select col1 d from tb_col where col1 = '123';
col1
------
123
123
(2 rows)
select distinct col1 d from tb_col where col1 = '123';
col1
------
123
(1 row)
SELECT col1
FROM tb_col
GROUP BY col1
HAVING count(*) = 1
Not showing duplicates at all:
SELECT col1 AS d
FROM tb_col
GROUP BY col1
HAVING COUNT(*) = 1 --- or perhaps HAVING COUNT(*) > 1
--- it's not clear what you want.
select col1
from tb_col
group by col1
having count(*) < 2
Try with DISTINCT it will works!
SELECT DISTINCT(col1) as d from tb_col where col1 = '123';