Author |
Topic |
johanm
Starting Member
5 Posts |
Posted - 2014-09-06 : 13:55:19
|
Hallo I need some help to make a correct SQl script.I have a table, FrmAcc, which have the following columns:Frm, FrmGrExampleFrm FrmGr1001 11001 101001 111002 11002 101002 991003 11003 99I would like to delete the followingf records:Alle Frm which do have FrmGr=99, but not delete the FrmGr = 99 and FrmGr = 1 |
|
SwePeso
Patron Saint of Lost Yaks
30421 Posts |
Posted - 2014-09-06 : 14:53:50
|
[code]DELETE fFROM ( SELECT MAX(CASE WHEN FrmGr = 99 THEN 1 ELSE 0 END) OVER (PARTITION BY Frm) AS Valid FROM dbo.FrmAcc ) AS fWHERE Valid = 1 AND FrmGr NOT IN (1, 99);[/code] Microsoft SQL Server MVP, MCT, MCSE, MCSA, MCP, MCITP, MCTS, MCDBA |
|
|
johanm
Starting Member
5 Posts |
Posted - 2014-09-06 : 15:00:11
|
Thanks for Your suggestion, however some of the records have FrmGr=99999 so I do not think MAX wil work |
|
|
tkizer
Almighty SQL Goddess
38200 Posts |
Posted - 2014-09-06 : 15:12:54
|
quote: Originally posted by johanm Thanks for Your suggestion, however some of the records have FrmGr=99999 so I do not think MAX wil work
Post representative sample data.Tara KizerSQL Server MVP since 2007http://weblogs.sqlteam.com/tarad/ |
|
|
Chris_Kelley
Posting Yak Master
114 Posts |
Posted - 2014-09-06 : 19:39:03
|
Sorry had a little more time after the kids when to bedSELECT * FROM #JOHANMResults --FRM FRMGR1001 11001 101001 111002 11002 101002 991003 11003 991001 999991001 999991001 99999first solution --BEGIN TRANDELETE #JOHANMFROM ( SELECT MAX(CASE WHEN FrmGr = 99 THEN 1 ELSE 0 END) OVER (PARTITION BY Frm) AS Valid FROM #JOHANM ) AS fWHERE Valid = 1 AND FrmGr NOT IN (1, 99);results --FRM FRMGR1001 11002 11002 991003 11003 99It looks like Tara Kizer solution still works,mine solution works, but it seems silly to do, but for giggles BEGIN TRANDELETE #JOHANMFROM ( SELECT MAX(CASE WHEN FrmGr like '[9]%' THEN 1 ELSE 0 END) OVER (PARTITION BY Frm) AS Valid FROM #JOHANM ) AS fWHERE Valid = 1 AND FrmGr NOT IN (1, 99);Results --FRM FRMGR1001 11002 11002 991003 11003 99Since "The MAX() function returns the largest value of the selected column."Question -I would like to delete the followingf records:Alle Frm which do have FrmGr=99, but not delete the FrmGr = 99 and FrmGr = 1I am not sure I understand the question.....It seems like it readS delete * frm where frmgr = 99, but then it says BUT not delete frmgr = 99.....Sorry I guess I did not fully understand the question |
|
|
johanm
Starting Member
5 Posts |
Posted - 2014-09-07 : 02:58:14
|
Thanks for your suggestions, how can I make a "selection" of the same, so I can se which rows will be deleted before I delete them? |
|
|
johanm
Starting Member
5 Posts |
Posted - 2014-09-07 : 04:34:17
|
I tried the different solutions, but it did not work. I think this is because I did not give you all information.The table consist of 36000 rowsIn column FrmGr are a lot of different numbers (ex. 1, 10, 99, 1011230, 12345, 999999)Any help for solutions? |
|
|
Chris_Kelley
Posting Yak Master
114 Posts |
Posted - 2014-09-07 : 12:29:08
|
c |
|
|
johanm
Starting Member
5 Posts |
Posted - 2014-09-07 : 14:54:32
|
The script does not work |
|
|
|