delete all rows where column_id is xyz except one randomly selected - sql-server-2008

I have a record in table something like this
ID Name Value
1 abc 123
2 abc 152
3 cde 574
4 def 153
5 abc 777
How to delete row from the above column based on this algorithm,
We have same name for 3 rows (ID: 1,2,5). Delete all these rows except any one selected randomly. Same applied for other Names
Possible using sql or T-SQL?

;with d as (
select *, rowNum = row_number() over (partition by Name order by checksum(newid()))
from TableName
)
delete d
where rowNum > 1

Related

Fetching Data Anomalies in SQL (2 Columns)

I have a table with two columns and following data
ID NAME
1 ALPHA
1 ALPHA
2 BETA
1 BETA
First three rows are correct data, but in the last row someone accidentally entered ID 1 instead of ID 2, can anyone help me with a query to fetch multiple rows of ID for distinct names. I have tried the query below but its not yielding the correct result
SELECT F1.ID FROM myTable F1 WHERE F1.Name in
(SELECT DISTINCT F2.Name FROM myTable F2)
Actually, you need names that have multiple IDs; right?
For sample data:
SQL> select * From test;
ID NAME
---------- -----
1 alpha
1 alpha
2 beta
1 beta
Query, using group by and having clauses:
SQL> select name
2 from test
3 group by name
4 having count(distinct id) > 1;
NAME
-----
beta
SQL>

MySql , How to select the not repeated rows only

I have a table with 100 000 record, I want to select only the none repeated.
In another word, if the row are duplicated did not show it at all
ID Name Reslut
1 Adam 10
2 Mark 10
3 Mark 10
result
ID Name Reslut
1 Adam 10
any ideas ?
You could join a query on the table with a query that groups by the name only returns the unique names:
SELECT *
FROM mytable t
JOIN (SELECT name
FROM mytable
GROUP BY name
HAVING COUNT(*) = 1) s ON t.name = s.name
Using the same set :
ID Name Result
1 Adam 10
2 Mark 10
3 Mark 10
4 Mark 20
I'm guessing the final solution would be:
ID Name Result
1 Adam 10
4 Mark 20
Using the above query previously suggested I modified it to take the result into consideration:
SELECT t1.*
FROM myTable t1
JOIN
(
SELECT name, result
FROM myTable
GROUP BY name, result
HAVING COUNT(*) = 1
) t2
WHERE
t1.name=t2.name and
t1.result = t2.result;

Insert into a column using group by value of another column of the same table

id name count
------------
1 abc
2 xyz
3 xyz
4 xyz
The following query "select count(name) from temp group by name;" gives me:
count(name)
--------
1
3
I want this result to be updated to the column 'count'. To be precise I want my table to look like :
id name count
------------
1 abc 1
2 xyz 3
3 xyz 3
4 xyz 3
You can get those values with a COUNT / GROUP BY. You can do an UPDATE statement which joins your table with the sub query:-
UPDATE temp a
INNER JOIN
(
SELECT name, COUNT(*) AS name_count
FROM temp
GROUP BY name
) b
ON a.name = b.name
SET a.name_count = b.name_count;

Update Order column with number increasing (depend on ID column)

I'm using the SQL Server 2008.
Now I have the scenario as description below:
1 table with 3 columns: ID, Name, Order.
They has 8 records.
5 records have the same data: ID='1', Name='AAA'
3 records have the same data: ID='2', Name='BBB'
Now I want to update Order column with number increasing (start from 1) for each ID,Name:
No Name Order
1 AAA 1
1 AAA 2
1 AAA 3
1 AAA 4
1 AAA 5
2 BBB 1
2 BBB 2
2 BBB 3
How can I get this result without using Cursors?
I'm very appriciated for your help.
Thanks.
You can try the following. I use CTE to drive the update statement
WITH data AS
(
SELECT Order
, ROW_NUMBER() OVER (PARTITION BY ID, NAME
ORDER BY ID, NAME) AS Seq
FROM TableA
)
UPDATE data
SET Order = Seq
Substitute TableA with your table name

Selecting rows with reference id's to same table from mysql

I have a table such as:
id name ref_id order data_obj
-- ---- ------ ----- --------
1 Sam 0 15 [binary data]
2 Jack 0 20 [binary data]
3 Sue 0 25 [binary data]
4 Sam2 1 - [no data]
5 Sue2 3 - [no data]
6 Sam3 1 - [no data]
The idea is that I have more columns other than data_obj which can be common, so I don't want to insert them again, just want to insert a reference id to the same data.
Is it possible to write a query and select this:
1 - Sam - binary data from id 1
4 - Sam2 - binary data from id 1
6 - Sam3 - binary data from id 1
2 - Jack - binary data from id 2
3 - Sue - binary data from id 3
5 - Sue2 - binary data from id 3
Please note that I'm ordering according to column named order and there's no actual data for this column for referenced rows.
SELECT t1.id, t1.name, t2.data_obj
FROM your_table t1
LEFT JOIN your_table t2 ON t1.ref_id = t2.id
ORDER BY t1.order
Other version, which doesn't return rows without ref
SELECT t1.id, t1.name, t2.data_obj
FROM your_table t1, your_table t2
WHERE t1.ref_id = t2.id
ORDER BY t1.order
Here's a modification of #vartec's answer. This modification uses COALESCE() to combine the data_obj from either the primary row or the referenced row.
SELECT t1.id, t1.name, COALESCE(t1.data_obj, t2.data_obj)
FROM your_table t1
LEFT JOIN your_table t2 ON t1.ref_id = t2.id
ORDER BY COALESCE(t1.order, t2.order), ref_id;
COALESCE() is a standard SQL function that returns its first non-NULL argument.
Why aren't you using more than one table?
CREATE TABLE user (
user_id number not null (some form of auto increment or sequence),
name varchar(50) not null,
otherdata type,
primary key (id));
CREATE TABLE common (
common_id number not null (autoinc),
user_id number not null,
commondata type,
primary key (common_id),
unique index (user_id, common_id));
SELECT u.name, u.otherdata, c.commondata
FROM user u, common c
WHERE u.user_id = c.user_id
TABLE user
user_id name otherdata
1 Sam abc
2 Jack def
3 Sue ghi
Table common
common_id user_id commondata
1 1 AAA
2 1 BBB
3 1 CCC
4 2 DDD
5 3 EEE
6 3 FFF
Output
name otherdata commondata
Sam abc AAA
Sam abc BBB
Sam abc CCC
Jack def DDD
Sue ghi EEE
Sue ghi FFF