Please start any new threads on our new
site at https://forums.sqlteam.com. We've got lots of great SQL Server
experts to answer whatever question you can come up with.
Author |
Topic |
teamjai
Yak Posting Veteran
52 Posts |
Posted - 2013-08-13 : 01:59:48
|
Table Design --> Componenet (table name)ID.Country IDBT-IDComponentActivationi need to delete the duplicate values from Componenet tableID CountryID BT-ID Componenet ActivationA3CD GD58 TR77 RX 1BER2 GD58 TR77 RX 1XEW7 GD58 TR77 MX 1O4T4 GD58 TR77 MX 1PE78 GD58 TR77 GX 1Expected OutputID CountryID BT-ID Componenet ActivationA3CD GD58 TR77 RX 1XEW7 GD58 TR77 MX 1PE78 GD58 TR77 GX 1Please help me |
|
SwePeso
Patron Saint of Lost Yaks
30421 Posts |
Posted - 2013-08-13 : 02:22:28
|
DELETE fFROM (SELECT ROW_NUMBER() OVER (PARTITION BY CountryID, [BT-ID], Component, Activation) AS rnFROM dbo.Table1) AS fWHERE rn > 1; Microsoft SQL Server MVP, MCT, MCSE, MCSA, MCP, MCITP, MCTS, MCDBA |
|
|
SwePeso
Patron Saint of Lost Yaks
30421 Posts |
Posted - 2013-08-13 : 02:23:13
|
SELECT ID, CountryID, [BT-ID], Component, ActivationFROM (SELECT ID, CountryID, [BT-ID], Component, Activation, ROW_NUMBER() OVER (PARTITION BY CountryID, [BT-ID], Component, Activation) AS rnFROM dbo.Table1) AS fWHERE rn = 1; Microsoft SQL Server MVP, MCT, MCSE, MCSA, MCP, MCITP, MCTS, MCDBA |
|
|
teamjai
Yak Posting Veteran
52 Posts |
Posted - 2013-08-13 : 02:42:35
|
quote: Originally posted by SwePeso DELETE fFROM (SELECT ROW_NUMBER() OVER (PARTITION BY CountryID, [BT-ID], Component, Activation) AS rnFROM dbo.Table1) AS fWHERE rn > 1; Microsoft SQL Server MVP, MCT, MCSE, MCSA, MCP, MCITP, MCTS, MCDBA
Thank you for your help..i got below error msgMsg 4112, Level 15, State 1, Line 11The function 'ROW_NUMBER' must have an OVER clause with ORDER BY.please guide me.. |
|
|
SwePeso
Patron Saint of Lost Yaks
30421 Posts |
Posted - 2013-08-13 : 02:45:31
|
[code]DELETE fFROM ( SELECT ROW_NUMBER() OVER (PARTITION BY CountryID, [BT-ID], Component, Activation ORDER BY CountryID) AS rn FROM dbo.Table1 ) AS fWHERE rn > 1;[/code] Microsoft SQL Server MVP, MCT, MCSE, MCSA, MCP, MCITP, MCTS, MCDBA |
|
|
teamjai
Yak Posting Veteran
52 Posts |
Posted - 2013-08-13 : 04:18:23
|
quote: Originally posted by SwePeso
DELETE fFROM ( SELECT ROW_NUMBER() OVER (PARTITION BY CountryID, [BT-ID], Component, Activation ORDER BY CountryID) AS rn FROM dbo.Table1 ) AS fWHERE rn > 1; Microsoft SQL Server MVP, MCT, MCSE, MCSA, MCP, MCITP, MCTS, MCDBA
Thank you . now its work. |
|
|
teamjai
Yak Posting Veteran
52 Posts |
Posted - 2013-08-13 : 07:50:08
|
quote: Originally posted by SwePeso
DELETE fFROM ( SELECT ROW_NUMBER() OVER (PARTITION BY CountryID, [BT-ID], Component, Activation ORDER BY CountryID) AS rn FROM dbo.Table1 ) AS fWHERE rn > 1; Microsoft SQL Server MVP, MCT, MCSE, MCSA, MCP, MCITP, MCTS, MCDBA
i have one more clarification. i need to check Activation valueif Activation is 0 or is NuLL then delete the rows , if Activation is 1 then dont delete that corresponding rowsPlease help me. |
|
|
bandi
Master Smack Fu Yak Hacker
2242 Posts |
Posted - 2013-08-13 : 08:00:45
|
[code]DELETE fFROM ( SELECT ROW_NUMBER() OVER (PARTITION BY CountryID, [BT-ID], Component, Activation ORDER BY CountryID) AS rn, Activation FROM dbo.Table1 ) AS fWHERE rn > 1 AND ISNULL(f.Activation, 0) !=0 ;[/code]--Chandu |
|
|
MuMu88
Aged Yak Warrior
549 Posts |
Posted - 2013-08-13 : 10:52:11
|
Minor correction:quote: Originally posted by bandi
DELETE fFROM ( SELECT ROW_NUMBER() OVER (PARTITION BY CountryID, [BT-ID], Component, Activation ORDER BY CountryID) AS rn, Activation FROM dbo.Table1 ) AS fWHERE rn > 1 AND ISNULL(f.Activation, 0) = 0 ; --Chandu
|
|
|
teamjai
Yak Posting Veteran
52 Posts |
Posted - 2013-08-14 : 00:19:28
|
quote: Originally posted by MuMu88 Minor correction:quote: Originally posted by bandi
DELETE fFROM ( SELECT ROW_NUMBER() OVER (PARTITION BY CountryID, [BT-ID], Component, Activation ORDER BY CountryID) AS rn, Activation FROM dbo.Table1 ) AS fWHERE rn > 1 AND ISNULL(f.Activation, 0) = 0 ; --Chandu
Thank you.Now i need to check activation using case conditionActivation 1 and 0 then delete Activation 0 rowsActivation 1 and 1 then delete any rowsActivation 1 and NUll then delete NULL rowsActivation 0 and NULL then delete any rows.is it possible.. using case condition..Please help. |
|
|
teamjai
Yak Posting Veteran
52 Posts |
Posted - 2013-08-14 : 04:19:03
|
quote: Originally posted by teamjai
quote: Originally posted by MuMu88 Minor correction:quote: Originally posted by bandi
DELETE fFROM ( SELECT ROW_NUMBER() OVER (PARTITION BY CountryID, [BT-ID], Component, Activation ORDER BY CountryID) AS rn, Activation FROM dbo.Table1 ) AS fWHERE rn > 1 AND ISNULL(f.Activation, 0) = 0 ; --Chandu
Thank you.Now i need to check activation using case conditionActivation 1 and 0 then delete Activation 0 rowsActivation 1 and 1 then delete any rowsActivation 1 and NUll then delete NULL rowsActivation 0 and NULL then delete any rows.is it possible.. using case condition..Please help.
Any suggestion for above issue. Please help me |
|
|
SwePeso
Patron Saint of Lost Yaks
30421 Posts |
Posted - 2013-08-14 : 06:48:19
|
Post some more samples with ALL expected combinations. Then post the expected output with ALL the rules above applied and make a comment which rule did what. Microsoft SQL Server MVP, MCT, MCSE, MCSA, MCP, MCITP, MCTS, MCDBA |
|
|
teamjai
Yak Posting Veteran
52 Posts |
Posted - 2013-08-14 : 07:45:58
|
Query:DELETE fFROM ( SELECT ROW_NUMBER() OVER (PARTITION BY CountryID, [BT-ID], Component, Activation ORDER BY CountryID) AS rn, Activation FROM dbo.Table1 ) AS fWHERE rn > 1 AND ISNULL(f.Activation, 0) = 0 ;The above query rremove the duplicates values from table (dbo.Table1)Table Values:ID CountryID BT-ID Componenet Activation------------------------------------------A3CD GD58 TR77 RX 1BER2 GD58 TR77 RX 0------------------------------------------XEW7 GD58 TR77 MX 1O4T4 GD58 TR77 MX 1-------------------------------------------PE78 GD58 TR77 GX NuLLGE44 GD58 TR77 GX 0---------------------------------------------PR98 GD58 TR75 TX 0WE47 GD58 TR75 TX 0------------------------------------------------Expected OutputID CountryID BT-ID Componenet Activation--------------------------------------------------A3CD GD58 TR77 RX 1--------------------------------------------------O4T4 GD58 TR77 MX 1 or 1 (Delete any one)------------------------------------------------------GE44 GD58 TR77 GX 0 or NULL (Delete any one)--------------------------------------------------------PR98 GD58 TR75 TX 0 or 0 (Delete any one)----------------------------------------------is it possible using a Query ?Guide me.. |
|
|
SwePeso
Patron Saint of Lost Yaks
30421 Posts |
Posted - 2013-08-14 : 07:58:50
|
[code]DELETE fFROM ( SELECT ROW_NUMBER() OVER (PARTITION BY CountryID, [BT-ID], Component ORDER BY [Activation] DESC) AS rn FROM dbo.Table1 ) AS fWHERE rn > 1;[/code] Microsoft SQL Server MVP, MCT, MCSE, MCSA, MCP, MCITP, MCTS, MCDBA |
|
|
SwePeso
Patron Saint of Lost Yaks
30421 Posts |
Posted - 2013-08-14 : 08:01:15
|
My above query prioritizes the Activation column. Keep (in order) 1, 0 and NULL.If there is a row with 1 for Activation, the code will keep that row and omit other 1 rows and 0 rows and NULL rows.If there are no rows with 1, then the code keep the zero row and omit other zero rows and NULL rows.If there are no rows with 0, then the code keep the NULL row and omit other NULL rows. Microsoft SQL Server MVP, MCT, MCSE, MCSA, MCP, MCITP, MCTS, MCDBA |
|
|
|
|
|
|
|