Deleteing double-entries [duplicate] - sql-server

This question already has answers here:
Delete duplicate records in SQL Server?
(10 answers)
Closed 6 years ago.
I got the problem that on one table it sometimes makes a double entry. I want to delete the doubled entry. I only want to delete a row when both values are the same than on another row. How is this possible?
My DB structure:
Example for the double entries:

One way to do it is using exists:
DELETE t0
FROM Password_Department t0
WHERE EXISTS
(
SELECT 1
FROM Password_Department t1
WHERE t0.PasswordFK = t1.PasswordFK
AND t0.DepartmentFK = t1.DepartmentFK
AND t0.Id > t1.Id
)
If you prefer the row number method -
delete x
from (
select *,
rn = row_number() over (partition by DepartmentFK, PasswordFK order by Id)
from Password_Department
) x
where rn > 1
After you deleted the duplicate entries, you should add a unique constraint on PasswordFK and DepartmentFK:
ALTER TABLE Password_Department
ADD Constraint UC_Password_Department UNIQUE (PasswordFK , DepartmentFK)

Related

Show 'Total' after the end of each type of records [duplicate]

This question already has answers here:
How to add a subtotal row in sql [closed]
(4 answers)
Closed 3 years ago.
I have a table which contains two columns, ie.
1. ClassName
2. Student Name
Use ROLLUP and add an union to your table
declare #mytable as table(classname varchar(100),studentname varchar(100))
insert into #mytable values
('10th','Hakim'),
('10th','Fathi'),
('9th','Saber'),
('9th','Wahid'),
('9th','Isamïl')
select * from
(select classname,total=convert(varchar(100),count(1)) from #mytable
GROUP BY ROLLUP(classname)
union
select * from #mytable) v
where classname is not null
order by classname,isnumeric(total)

SQL Server Temp Table of Sequential Integers As Lookup [duplicate]

This question already has answers here:
Create Temp Table with Range of Numbers
(2 answers)
Closed 6 years ago.
I am trying to create a temporary table that has sequential integer values from 1 to 1,000 for use as a LEFT JOIN on my table that has the following sub-set of records:
USRNO EVSTRING
1 John Doe
10 Jane Doe
13 Jason Doe
16 Jeremy Doe
I'm attempting to identify the user numbers (USRNO) that do NOT appear in the records returned above (ie: find the missing user numbers).
I'd like to do this with a single SQL statement if possible, and would like to create the temp table "on the fly" (not create the table in advance).
How can I create a temporary table that is populated with an integer field with values from 1 to 1,000 sequentially?
Using SQL Server 2012.
The canonical way uses a recursive CTE:
with n as (
select 1 as n
union all
select n + 1
from n
where n < 1000
)
select n.n
from n
where not exists (select 1 from t where t.userno = n.n)
option (MAXRECURSION 0);

The column 'TRANSACTION_EVENT_ID' was specified multiple times for 'YY' [duplicate]

This question already has answers here:
SQL 2005 - The column was specified multiple times
(5 answers)
Closed 7 years ago.
I have written a query like this:
SELECT
YY.ACCOUNT_ID,
YY.TRANSACTION_EVENT_ID
FROM
(SELECT *
FROM dbo.TRANSACTION TE
JOIN dbo.FUND_TRANSACTION FT
ON TE.TRANSACTION_EVENT_ID = FT.TRANSACTION_EVENT_ID
JOIN ACCOUNT A
ON FT.ACCOUNT_ID = A.ACCOUNT_ID) AS YY
and getting error like this:
The column 'TRANSACTION_EVENT_ID' was specified multiple times for 'YY'
Checked this : SQL The column 'Id' was specified multiple times
However, the issue is not resolved. What am I doing wrong?
The issue is with the SELECT *. Since the same column names can be repeat in the selection.
Explicitly mention the column names. The below query will work.
SELECT YY.ACCOUNT_ID, YY.TRANSACTION_EVENT_ID
FROM ( SELECT FT.ACCOUNT_ID, FT.TRANSACTION_EVENT_ID
FROM dbo.[TRANSACTION] TE
JOIN dbo.FUND_TRANSACTION FT ON TE.TRANSACTION_EVENT_ID = FT.TRANSACTION_EVENT_ID
JOIN ACCOUNT A ON FT.ACCOUNT_ID = A.ACCOUNT_ID
) AS YY
You have to specify from which table TRANSACTION_EVENT_ID you want to select. You have to specify column list with aliases / table names instead of *. Something like:
SELECT
YY.ACCOUNT_ID,
YY.TRANSACTION_EVENT_ID
FROM
(SELECT A.ACCOUNT_ID,
TE.TRANSACTION_EVENT_ID
FROM dbo.TRANSACTION TE
JOIN dbo.FUND_TRANSACTION FT
ON TE.TRANSACTION_EVENT_ID = FT.TRANSACTION_EVENT_ID
JOIN ACCOUNT A
ON FT.ACCOUNT_ID = A.ACCOUNT_ID) AS YY

Select Top row from each group [duplicate]

This question already has answers here:
Get top 1 row of each group
(19 answers)
Closed 8 years ago.
I have a table called DynamicText with the following fields: DynamicID, Content, Timestamp, and DynamicTextEnum.
I'm trying to do design a query that would select the most recent record based on Timestamp from each of the groups that are grouped by DynamicTextEnum.
For example:
Enum Timestamp
-----------------
1 1/10/2012
1 2/10/2012
2 1/10/2012
3 3/10/2012
2 3/10/2012
3 4/10/2012
So the results would look like this:
Enum Timestamp
-----------------
1 2/10/2012
2 3/10/2012
3 4/10/2012
My current simply SELECTS TOP 1 them based on Enum and orders them in DESC order based on
Timestamp but it doesn't work when I need all Enums. Any ideas?
;WITH cte AS
(
SELECT *,
ROW_NUMBER() OVER (PARTITION BY DynamicTextEnum ORDER BY Timestamp DESC) AS rn
FROM DynamicText
)
SELECT *
FROM cte
WHERE rn = 1
Take a look at this question and answer:
Efficiently select top row for each category in the set
You can do a sub-query to get the enum/timestamp pair, and join that with your full row. That assumes you don't have duplicate timestamps for any given enum.

Please help me turn this into an update query [duplicate]

This question already has an answer here:
TSQL A recursive update?
(1 answer)
Closed 9 years ago.
with T1 as
( select tree.* from tree where parent_id = 2
union all
select tree.* from tree
join T1 on (tree.parent_id=T1.id)
)
select * from T1
This query selects all children nodes in a hierarchical tree.
What I need to do is, with all the results returned from the query above, is update a field called [level] by an increment of 1.
I have tried myself with a few permutations but I get errors about not being able to update a derived table
; with T1 as
(
select tree.*
from tree
where parent_id = 2
union all
select tree.*
from tree
join T1
on tree.parent_id=T1.id
)
update tree
set level = level + 1
where id in
(
select id
from t1
)

Resources