SQL - Removing Row Groups - sql

I have a table with the following information:
Is there a way to remove all groups which have multiple IDs? For example group 3 would be removed because it consists of ID 1 and 2.
Thank you!

A simple, portable and efficient approach is not exists:
select t.*
from mytable t
where not exists (
select 1
from mytable t1
where t1.group = t.group and t1.id <> t.id
)
For performance, consider an index on (group, id).
Side note: group is a SQL keyword (as in group by), hence not a good choice for a column name.

You can use below query to remove all groups having multiple IDs
Delete from <your_table_name> where Group in (select Group from <your_table_name> group by Group,ID having count(*) > 1)
inner query will return Group having multiple IDs.

select * from temp where group in (
select groups from temp group by id,group having count(1)<3)
delete from temp where group in (
select groups from temp group by id,group having count(1)<3)

Try to execute below query:
select id,group from table where group in
(
select group from(
select group,count(distinct id) as cn from table group by 1 having cn=1) a
)

Related

Need a SQL delete script for group by and having clause

I'm trying to build a Delete query for the below script which has multiple columns in group by.
select Test_1,Test_2,Test_3,Test_4,Test_5,Test_6,Test_7,Test_8,Test_9,
Test_10,Test_11,Test_12,Test_13,Test_14,Test_15,Test_16,Test_17,Test_18,Test_19,Test_20,
Test_21,Test_22,Test_23,Test_24,Test_25,Test_26,Test_27,Test_28,Test_29,Test_30 from Test_Table
where PROS_Test='236458'
group by Test_1,Test_2,Test_3,Test_4,Test_5,Test_6,Test_7,Test_8,Test_9,
Test_10,Test_11,Test_12,Test_13,Test_14,Test_15,Test_16,Test_17,Test_18,Test_19,Test_20,
Test_21,Test_22,Test_23,Test_24,Test_25,Test_26,Test_27,Test_28,Test_29,Test_30
having count(*)>1
The total count for the select query is 102100, I need the delete query for the same.
Try delete statement with subquery:
delete tt
from (
select row_number() over (partition by Test_1,Test_2,Test_3,Test_4,Test_5,Test_6,Test_7,Test_8,Test_9,
Test_10,Test_11,Test_12,Test_13,Test_14,Test_15,Test_16,Test_17,Test_18,Test_19,Test_20,
Test_21,Test_22,Test_23,Test_24,Test_25,Test_26,Test_27,Test_28,Test_29,Test_30 order by Test_1) rn
from Test_Table
where PROS_Test='236458'
) tt where rn > 1
you can create a cte with your query and use it in the delete command:
with cte as (
select Test_1,Test_2,Test_3,Test_4,Test_5,Test_6,Test_7,Test_8,Test_9,
Test_10,Test_11,Test_12,Test_13,Test_14,Test_15,Test_16,Test_17,Test_18,
Test_19,Test_20,Test_21,Test_22,Test_23,Test_24,Test_25,Test_26,
Test_27,Test_28,Test_29,Test_30 from Test_Table
where PROS_Test='236458'
group by Test_1,Test_2,Test_3,Test_4,Test_5,Test_6,Test_7,Test_8,Test_9,
Test_10,Test_11,Test_12,Test_13,Test_14,Test_15,Test_16,Test_17,Test_18,
Test_19,Test_20,Test_21,Test_22,Test_23,Test_24,Test_25,Test_26,
Test_27,Test_28,Test_29,Test_30
having count(*) > 1
)
delete t
from Test_Table as t
INNER JOIN cte as c ON t.Test_1 = c.Test_1 ...
And all the conditions you need besides the Test_1 matching.

Select duplicated data from table

Query
select * from table1
where having count(reference)>1
I want to select * the data which have duplicate data,any idea why my query is not working?
Below are my expect result..
You can make use of window function count to find number of rows per id and reference and then filter to get those which have count more than 1.
;with cte as (
select t.*, count(*) over (partition by id, reference) cnt
from table1 t
)
select * from cte where cnt > 1;
Demo
In the above solution, I have made an assumption that name and id has one to one correspondence (which is true as per your given data). If that's not the case, add name too in the partition by clause:
;with cte as (
select t.*, count(*) over (partition by name, id, reference) cnt
from table1 t
)
select * from cte where cnt > 1;
I might actually approach this by using a subquery with GROUP BY:
SELECT t1.*
FROM table1 t1
INNER JOIN
(
SELECT Name, ID, reference
FROM table1
GROUP BY Name, ID, reference
HAVING COUNT(*) > 1
) t2
ON t1.Name = t2.Name AND
t1.ID = t2.ID AND
t1.reference = t2.reference
Demo here:
Rextester
Try this ), first i get count by partition, after that i get row with count > 1
select No, Name, ID, Reference
from (select count(*) over (partition by name, ID, reference) cnt, table1.* from table1)
where cnt>1
The easy way (although maybe not the best for performance) would be:
select * from table1 where reference in (
select reference from table1 group by reference having count(*)>1
)
In a subselect you have the duplicated data, and in the outter select you have all the data for these references.

Optimized way to select distinct records

I have a table in hive with 23 columns out of which 5 columns make up the composite primary keys.
what is the best optimized way to select all the distinct records from the table.
select *
from (select t.*
,count(*) over (partition by Col1,Col2,Col3,Col4,Col5) as cnt
from tablename t
) t
where t.cnt = 1
;
Use the group by statements with where statements where count(1)>=1 this will give you the distinct records based on your composite key.
Like
Select Col1,Col2,Col3,Col4,Col5,Count(1) from tablename group by Col1,Col2,Col3,Col4,Col5 having Count(1)>=1

How to get duplicate text values from SQL query

I have to get table only with duplicate text values using SQL query. I have used Having count(columnname) > 1 but I'm not getting result, only with duplicate values instead getting all values.
Can anyone suggest whether I have to add anything to my query?
Thanks.
Use the below query. mention the column which is getting duplicated in the patition by clause..
with CTE_1
AS
(SELECT *,COUNT(1) OVER(PARTITION BY LTRIM(RTRIM(REPLACE(yourDuplicateColumn,' ',''))) Order by -anycolunm- ) cnt
FROM YourTable
)
SELECT *
FROM CTE_1
WHERE cnt>1
Assuming id is a primary key
select *
from myTable t1
where exists (select 1
from myTable t2
where t2.text = t1.text and t2.id != t1.id)
You can use similar to following query:
SELECT
column1, COUNT(*)
FROM table
GROUP BY column1
HAVING COUNT(*) > 1

How to get Original Rows filtered by a HAVING Condition?

What is the method in T-SQL to select the orginal values limited by a HAVING attribute. For example, if I have
A|B
10|1
11|2
10|3
How would I get all the values of B (Not An Average or some other summary stat), Grouped by A, having a Count (Occurrences of A) greater than or equal two 2?
Actually, you have several options to choose from
1. You could make a subquery out of your original having statement and join it back to your table
SELECT *
FROM YourTable yt
INNER JOIN (
SELECT A
FROM YourTable
GROUP BY
A
HAVING COUNT(*) >= 2
) cnt ON cnt.A = yt.A
2. another equivalent solution would be to use a WITH clause
;WITH cnt AS (
SELECT A
FROM YourTable
GROUP BY
A
HAVING COUNT(*) >= 2
)
SELECT *
FROM YourTable yt
INNER JOIN cnt ON cnt.A = yt.A
3. or you could use an IN statement
SELECT *
FROM YourTable yt
WHERE A IN (SELECT A FROM YourTable GROUP BY A HAVING COUNT(*) >= 2)
A self join will work:
select B
from table
join(
select A
from table
group by 1
having count(1)>1
)s
using(A);
You can use window function (no joins, only one table scan):
select * from (
select *, cnt=count(*) over(partiton by A) from table
) as a
where cnt >= 2