How to remove duplicate values from a table - sql

I'm working on pulling data to a table which has two data sets of data loaded to temp table and finally inserts into table. There are 2 records which are duplicates but i need both the records if their value on one column is same else delete it. Col1 and col4 are primary keys
col1 col2 col3 col4
--------------------
a ab abc x
a ab abc y
b ab abc y
b ab abc z
what i want is forget about col 2 and col3, check in col1 if row 1 and row 2 are same it should go check col4 and if row1 and row 2 are different it should display both row1 and row 2 even if row 2 and row 3 in col4 are same. so if the records are as i mentioned it should allow all 4 values but with the logic i wrote it is returning row1,row3,row4 because it is considering row2 and row3 of col 1 with row 2 row 3 of col 4 and displaying only 3 records but i want all records. Please help me how to write a logic in sql for this situation.

Based on what I have understood from your question, you could do something like this..
DELETE i
FROM YourTable i INNER JOIN
(
SELECT col1,col4
FROM YourTable
GROUP BY col1,col4
HAVING count(col2)>1
) t ON t.col1 = i.col1 AND t.col4 = i.col4
Live Demo Here
Or if ou want to keep only one record and remove other duplicate records, you could do like this..
;with cte as
(
SELECT *,row_number() over(partition by col1,col4 order by col1,col4) as rn
FROM YourTable
)
DELETE from cte where rn>1
Live Demo Here

Related

updating a table by moving data from one row to another multiple times

have an sql table and I need to take row 2 and move data in column 4 & 5 and move it to row 1. The values in column 4 & 5 are dynamic (ie not the same each time)
background on the table. every 8 rows are grouped together (by an action_id ascending) to another entity in another table (but the other table isnt needed for this task)
for example
action_id = 839283 col 4 = space col 5 = space
action_id = 839284 col 4 = SMS col 5 = L1
i need to move the SMS & L1 to the row above and blank out the row where action_id = 839284
this will repeat multiple times.
I was thinking of creating a select into a temp table to get the rows i need to change (using 2 other tables as links) but i caon't work out how to move data from the one row to the other dynamically
Assumptions:
sql server
action_id values are always one apart
3 stpes:
select action_id as To_Empty
into #EmptyMe
from MyTable
where col4 is not null
with CTE as
(
select action_id,
col4,
col5,
lag(col4) over (order by action_id) as n_col4,
lag(col5) over (order by action_id) as n_col5
from MyTable
where col4 is not null
)
update CTE
set col4 = n_col4, col5 = n_col5
update MyTable
set col4 = null, col5 = null
where exists (select 1 from #EmptyMe where action_id = To_Empty)

sql query help, don't know how to term this

I'm not too sure how to term this question. Stay with me.
I want to create a query to retrieve the rows with x's in the row column in the table(this is a dummy table):
row col1 col2 col3
x 1 a c
x 2 b c
x 3 a c
4 b d
x 5 f g
So the way I want my query to work is to retrieve all rows where the value for col2 doesn't have a row in col3 where the value is d. Ie. value 'a' will be retrieved because it only has c's for col3, but value 'b' wont be retrieved because it has a d in col3 on the 4th row down.
I hope this is easy to understand.
Ps. Once I know how to do the query I expect I'll know how to phrase the title and will redo it. (although now I think about it, maybe this title is best for all those with questions like mine)
Based on everything you've provided, I'll have to make a few assumptions. Short of doing a self-join on the table, you could take advantage of an identity key on the table to use a simple query as such:
SELECT * FROM TABLE_NAME
WHERE id NOT IN (SELECT id FROM TABLE_NAME WHERE col3 = 'd');
If you don't have an identity key on the table, you could do something more like this:
SELECT * FROM TABLE_NAME
WHERE col2 NOT IN (SELECT col2 FROM TABLE_NAME WHERE col3 = 'd');
Both of these queries will return all tuples in the relation that have elements of col2 that are not in tuples where col3 contains 'd'.
Think of how you might do this manually, and put that into your query.
To get all the col3 values you'd write:
SELECT DISTINCT col3 FROM TABLE_NAME
Use that to filter rows from your selection via a not in clause:
SELECT
col1, col2, col3 FROM TABLE_NAME
WHERE col2 not in (SELECT DISTINCT col3 FROM TABLE_NAME);

Select all cell values of a single row table and list it as separate row in SQL

I've a table like this
ID col1 col2 col3 col4
---------------------------------------------------
1 a b c d
2 e f g h
So if I pass the ID 2 it should return all the colum values as separate rows as this
Colum Value
---------------------
ID 2
Col1 e
col2 f
col3 g
col4 h
So all the cells of that single rows been splitted as separate rows.
How can I accomplish this
One way to do it with unpivot and union all.
select 'id' as colu,cast(id as varchar(255)) as val from t where id=2
union all
select colu,val
from t
unpivot (val for colu in (col1,col2,col3,col4)) u
where id=2
You can use cross apply as below:
Select Id as [Column], [Value] from yourcols
cross apply (values(col1), (col2), (col3), (col4)) rws([Value])
where Id = 2
Please refer the below stack over flow link SQL query to split column data into rows
found the question identical as yours.

Recursive select statement

I have the following table entries:
Col1 | Col2
------------------
3RDT | 3R9H
3R9H |
3R9J | 3RDT
3R9J | 3R9H
3HHA | 3ABC
3XXZ | 3HHA
I have a value 3R9J.
Now I want all records where Col1 has that value, in this case record 3 and 4.
Now I take the Col2 values of this records (3RDT and 3R9H) and consider those as my new Col1 values to get all records with the Col1 value 3RDT or 3R9H.
This should be done recursively. In addition it should also select all records in the other direction. If I start with 3RDT for Col1 I get the records where Col2 is 3RDT (3), then I have 3R9J for Col1 and get all records where I have 3R9J as Col1 value.
The expected output data in my example above should be the first 4 records.
If I understood your question correctly, that this query will do the job.
SQLFiddle
select *
from test
connect by col1 = prior col2
start with col1 = '3R9J'
union
select *
from test
connect by prior col1 = col2
start with col1 = '3RDT';
Please note that the union causes that each line is shown once.

SQL table search

I'm new to SQL and was just wondering how I would a value from a table only when it meets a certain condition.
I have a table that looks like the following (sorry for the rubbish formatting)
COL1 - COL2
1 - 3
1 - 2
2 - 2
3 - 3
3 - 4
I want to get values from COL1, but only if they don't have a specific value in COL2.
So for example if I didn't want the values where COL2 was 3, the only value that would be returned from COL1 would be 2.
Thanks for any help
To clarify, the two columns just store id's that reference other tables. I only want the values from COL1 that don't reference a specific values in COL2.
so when I saw I don't want the values where COL2 is equal to 3, this means the value '1' from COL1 won't be returned as on row 1 COL2 is 3 and 3 won't be returned from COL1 because on row4 COL2 is equal to 3
I think you look for something like this.
select COL1
from TABLE_FOO
where COL1 not in (
select COL1
from TABLE_FOO
where COL2 = 3
)
I think Specific value means the record having only one corresponding value in the col2.
2-2 is specific because it has a specific value.
select col1 from c group by col1 having count(col2) > 1
The above query gives all the col1 values which does not have a specific values.