Author |
Topic |
chenko
Starting Member
24 Posts |
Posted - 2007-09-27 : 07:32:44
|
I have some data like thisAMark Mark Section Opt1 Opt21 P3 203X203X45UB 0 0 *1 P3 203X203X45UB 0 0 *1 P3 203X203X45UB 0 01 P3 203X203X45UB 1 01 P3 203X203X45UB 1 02 C1 10X100FLAT 0 0 *2 C1 10X100FLAT 0 14 P32 115.5X5CF 0 0A19 C53 70X70X8RSA 1 0 The options are bit values.Nothing is unique. (But there is a unique ID for everything, even "matching" items)* - are items that should be deletedI want to be left with this result setAMark Mark Section Opt1 Opt21 P3 203X203X45UB 0 01 P3 203X203X45UB 1 01 P3 203X203X45UB 1 02 C1 10X100FLAT 1 14 P32 115.5X5CF 0 0A19 C53 70X70X8RSA 1 0 For every item that has ANY opt as 1, I want to delete one of the same items that has all opts as 0.If I can't delete htem, atleast a list of the itemsI've had a few ideas, along the lines of uses exists etc and where in, but I am totally lost right now. Any suggestions would be great.I hope I have explained well enough :) thanks |
|
SwePeso
Patron Saint of Lost Yaks
30421 Posts |
Posted - 2007-09-27 : 08:12:15
|
[code]-- Prepare sample dataDECLARE @Sample TABLE (ID INT IDENTITY(1, 1), AMark VARCHAR(3), Mark VARCHAR(3), Section VARCHAR(12), Opt1 BIT, Opt2 BIT)INSERT @SampleSELECT '1', 'P3', '203X203X45UB', 0, 0 UNION ALLSELECT '1', 'P3', '203X203X45UB', 0, 0 UNION ALLSELECT '1', 'P3', '203X203X45UB', 0, 0 UNION ALLSELECT '1', 'P3', '203X203X45UB', 1, 0 UNION ALLSELECT '1', 'P3', '203X203X45UB', 1, 0 UNION ALLSELECT '2', 'C1', '10X100FLAT', 0, 1 UNION ALLSELECT '2', 'C1', '10X100FLAT', 0, 0 UNION ALLSELECT '4', 'P32', '115.5X5CF', 0, 0 UNION ALLSELECT 'A19', 'C53', '70X70X8RSA', 1, 0-- Show all dataSELECT * FROM @Sample-- Stage dataDECLARE @Stage TABLE (ID INT, o2 TINYINT, Yak INT)INSERT @StageSELECT ID, Opt2, CASE WHEN Opt1 = 1 THEN CHECKSUM(ID, AMark, Mark, Section, Opt1, Opt2) ELSE CHECKSUM(AMark, Mark, Section) ENDFROM @Sample-- Show which to keepSELECT x.ID, x.AMark, x.Mark, x.Section, x.Opt1, x.Opt2FROM @Sample AS xINNER JOIN ( SELECT MAX(s.ID) AS mx FROM ( SELECT MAX(o2) AS o2, Yak FROM @Stage GROUP BY Yak ) AS e INNER JOIN @Stage AS s ON s.o2 = e.o2 AND s.Yak = e.Yak GROUP BY s.Yak ) AS y ON y.mx = x.ID-- Show which to deleteSELECT x.ID, x.AMark, x.Mark, x.Section, x.Opt1, x.Opt2FROM @Sample AS xLEFT JOIN ( SELECT MAX(s.ID) AS mx FROM ( SELECT MAX(o2) AS o2, Yak FROM @Stage GROUP BY Yak ) AS e INNER JOIN @Stage AS s ON s.o2 = e.o2 AND s.Yak = e.Yak GROUP BY s.Yak ) AS y ON y.mx = x.IDWHERE y.mx IS NULL[/code] E 12°55'05.25"N 56°04'39.16" |
 |
|
SwePeso
Patron Saint of Lost Yaks
30421 Posts |
Posted - 2007-09-27 : 08:24:39
|
With SQL Server 2005, this will be easier-- Prepare sample dataDECLARE @Sample TABLE (ID INT IDENTITY(1, 1), AMark VARCHAR(3), Mark VARCHAR(3), Section VARCHAR(12), Opt1 BIT, Opt2 BIT)INSERT @SampleSELECT '1', 'P3', '203X203X45UB', 0, 0 UNION ALLSELECT '1', 'P3', '203X203X45UB', 0, 0 UNION ALLSELECT '1', 'P3', '203X203X45UB', 0, 0 UNION ALLSELECT '1', 'P3', '203X203X45UB', 1, 0 UNION ALLSELECT '1', 'P3', '203X203X45UB', 1, 0 UNION ALLSELECT '2', 'C1', '10X100FLAT', 0, 1 UNION ALLSELECT '2', 'C1', '10X100FLAT', 0, 0 UNION ALLSELECT '4', 'P32', '115.5X5CF', 0, 0 UNION ALLSELECT 'A19', 'C53', '70X70X8RSA', 1, 0-- Show all dataSELECT * FROM @Sample-- Show which to keepSELECT TOP 1 WITH TIES ID, AMark, Mark, Section, Opt1, Opt2FROM @SampleORDER BY CASE WHEN Opt1 = 1 THEN 1 ELSE ROW_NUMBER() OVER (PARTITION BY AMark, Mark, Section ORDER BY Opt2 DESC) END-- Show which to deleteSELECT ID, AMark, Mark, Section, Opt1, Opt2FROM ( SELECT ID, AMark, Mark, Section, Opt1, Opt2, CASE WHEN Opt1 = 1 THEN 1 ELSE ROW_NUMBER() OVER (PARTITION BY AMark, Mark, Section ORDER BY Opt2) END AS Yak FROM @Sample ) AS dWHERE Yak > 1 E 12°55'05.25"N 56°04'39.16" |
 |
|
chenko
Starting Member
24 Posts |
Posted - 2007-09-27 : 09:54:45
|
Hi thanks, I can see what you are doing, but this wont fully work.The first thing, that I should have mentioned, is that there is many more of these options (I didnt put them all in for the point of the example). Currently there would be 7 of them.I've tried sorting it myself, but I dont fully understand your last SELECT statment yet. :/ |
 |
|
SwePeso
Patron Saint of Lost Yaks
30421 Posts |
Posted - 2007-09-27 : 10:01:59
|
Feel free to post back sample data with ALL POSSIBLE combinations, together with your expected result. E 12°55'05.25"N 56°04'39.16" |
 |
|
chenko
Starting Member
24 Posts |
Posted - 2007-09-27 : 10:23:50
|
ExampleAMark Mark Section Grade Bevel SF Opt1 Opt2 Opt3 Opt4 Opt5 Opt6 Opt71 P3 203X203X45UB S275JR 0 S 0 0 0 0 0 0 01 P3 203X203X45UB S275JR 0 S 0 0 0 0 0 0 01 P3 203X203X45UB S275JR 0 S 0 0 0 0 0 0 01 P3 203X203X45UB S275JR 0 S 0 0 0 0 0 1 01 P3 203X203X45UB S275JR 0 S 1 0 0 0 0 0 02 C1 10X100FLAT S275JR 0 S 0 0 0 0 0 0 02 C1 10X100FLAT S275JR 0 S 0 1 0 0 0 0 04 P32 115.5X5CF S275JR 0 S 0 0 0 0 0 0 0A19 C53 70X70X8RSA S275JR 0 F 1 0 0 0 0 0 0 ...to save you the hassle...DECLARE @Sample TABLE (ID INT IDENTITY(1, 1), AMark VARCHAR(3), Mark VARCHAR(3), Section VARCHAR(12), Grade CHAR(6), Bevel BIT, SF CHAR(1), Opt1 BIT, Opt2 BIT, Opt3 BIT, Opt4 BIT, Opt5 BIT, Opt6 BIT, Opt7 BIT)INSERT @SampleSELECT '1', 'P3', '203X203X45UB', 'S275JR', 0, 'S', 0, 0, 0, 0, 0, 0, 0 UNION ALLSELECT '1', 'P3', '203X203X45UB', 'S275JR', 0, 'S', 0, 0, 0, 0, 0, 0, 0 UNION ALLSELECT '1', 'P3', '203X203X45UB', 'S275JR', 0, 'S', 0, 0, 0, 0, 0, 0, 0 UNION ALLSELECT '1', 'P3', '203X203X45UB', 'S275JR', 0, 'S', 0, 0, 0, 0, 0, 1, 0 UNION ALLSELECT '1', 'P3', '203X203X45UB', 'S275JR', 0, 'S', 1, 0, 0, 0, 0, 0, 0 UNION ALLSELECT '2', 'C1', '10X100FLAT', 'S275JR', 0, 'S', 0, 1, 0, 0, 0, 0, 0 UNION ALLSELECT '2', 'C1', '10X100FLAT', 'S275JR', 0, 'S', 0, 0, 0, 0, 0, 0, 0 UNION ALLSELECT '4', 'P32', '115.5X5CF', 'S275JR', 0, 'S', 0, 0, 0, 0, 0, 0, 0 UNION ALLSELECT 'A19', 'C53', '70X70X8RSA', 'S275JR', 0, 'F', 1, 0, 0, 0, 0, 0, 0 As you can see, if ANY of the opt columns have 1, I want it to remove a record which has ALL 0 in it.example resultAMark Mark Section Grade Bevel SF Opt1 Opt2 Opt3 Opt4 Opt5 Opt6 Opt71 P3 203X203X45UB S275JR 0 S 0 0 0 0 0 0 01 P3 203X203X45UB S275JR 0 S 0 0 0 0 0 1 01 P3 203X203X45UB S275JR 0 S 1 0 0 0 0 0 02 C1 10X100FLAT S275JR 0 S 0 1 0 0 0 0 04 P32 115.5X5CF S275JR 0 S 0 0 0 0 0 0 0A19 C53 70X70X8RSA S275JR 0 F 1 0 0 0 0 0 0 Code to delete the records above would be better than to list the ones I want to delete. :)The amount of records is around 2000-3000 a time.Along with AMark, etc, there are more columns that need to be all matching, I didnt list htem above because I think it will confuse the example (and im sure it wont be hard to modify any code to do that). If its still a problem I will do them all (about 30-40 more columns...):) thanks |
 |
|
SwePeso
Patron Saint of Lost Yaks
30421 Posts |
Posted - 2007-09-27 : 10:31:55
|
Why should this record be deleted?A19 C53 70X70X8RSA S275JR 0 F 1 0 0 0 0 0 0 it is very simple to replace the SELECT <to delete> with a DELETE. E 12°55'05.25"N 56°04'39.16" |
 |
|
chenko
Starting Member
24 Posts |
Posted - 2007-09-27 : 10:51:18
|
That record shouldn't be deleted.If there isnt an item with ALL options = 0, then it can be left.The result set, is a list of what I would like to have left in the database, not what is to be deleted. |
 |
|
SwePeso
Patron Saint of Lost Yaks
30421 Posts |
Posted - 2007-09-27 : 10:55:13
|
We'll start over again.With this set of sample dataDECLARE @Sample TABLE (ID INT IDENTITY(1, 1), AMark VARCHAR(3), Mark VARCHAR(3), Section VARCHAR(12), Grade CHAR(6), Bevel BIT, SF CHAR(1), Opt1 BIT, Opt2 BIT, Opt3 BIT, Opt4 BIT, Opt5 BIT, Opt6 BIT, Opt7 BIT)INSERT @SampleSELECT '1', 'P3', '203X203X45UB', 'S275JR', 0, 'S', 0, 0, 0, 0, 0, 0, 0 UNION ALLSELECT '1', 'P3', '203X203X45UB', 'S275JR', 0, 'S', 0, 0, 0, 0, 0, 0, 0 UNION ALLSELECT '1', 'P3', '203X203X45UB', 'S275JR', 0, 'S', 0, 0, 0, 0, 0, 0, 0 UNION ALLSELECT '1', 'P3', '203X203X45UB', 'S275JR', 0, 'S', 0, 0, 0, 0, 0, 1, 0 UNION ALLSELECT '1', 'P3', '203X203X45UB', 'S275JR', 0, 'S', 1, 0, 0, 0, 0, 0, 0 UNION ALLSELECT '2', 'C1', '10X100FLAT', 'S275JR', 0, 'S', 0, 1, 0, 0, 0, 0, 0 UNION ALLSELECT '2', 'C1', '10X100FLAT', 'S275JR', 0, 'S', 0, 0, 0, 0, 0, 0, 0 UNION ALLSELECT '4', 'P32', '115.5X5CF', 'S275JR', 0, 'S', 0, 0, 0, 0, 0, 0, 0 UNION ALLSELECT 'A19', 'C53', '70X70X8RSA', 'S275JR', 0, 'F', 1, 0, 0, 0, 0, 0, 0 which records do you want to keep and which records do you want to delete.What are the rules for your decisions? E 12°55'05.25"N 56°04'39.16" |
 |
|
chenko
Starting Member
24 Posts |
Posted - 2007-09-27 : 11:08:45
|
Look for all records, which has ANY option = 1For each record, look for a matching record that has ALL option = 0, delete that record (if one exists)records need to match on all columns except the options (amark, mark, section, grade, bevel, sf) |
 |
|
SwePeso
Patron Saint of Lost Yaks
30421 Posts |
Posted - 2007-09-27 : 11:16:58
|
So what is the final expected result for this sample data?DECLARE @Sample TABLE (ID INT IDENTITY(1, 1), AMark VARCHAR(3), Mark VARCHAR(3), Section VARCHAR(12), Grade CHAR(6), Bevel BIT, SF CHAR(1), Opt1 BIT, Opt2 BIT, Opt3 BIT, Opt4 BIT, Opt5 BIT, Opt6 BIT, Opt7 BIT)INSERT @SampleSELECT '1', 'P3', '203X203X45UB', 'S275JR', 0, 'S', 0, 0, 0, 0, 0, 0, 0 UNION ALLSELECT '1', 'P3', '203X203X45UB', 'S275JR', 0, 'S', 0, 0, 0, 0, 0, 0, 0 UNION ALLSELECT '1', 'P3', '203X203X45UB', 'S275JR', 0, 'S', 0, 0, 0, 0, 0, 0, 0 UNION ALLSELECT '1', 'P3', '203X203X45UB', 'S275JR', 0, 'S', 0, 0, 0, 0, 0, 1, 0 UNION ALLSELECT '1', 'P3', '203X203X45UB', 'S275JR', 0, 'S', 1, 0, 0, 0, 0, 0, 0 UNION ALLSELECT '2', 'C1', '10X100FLAT', 'S275JR', 0, 'S', 0, 1, 0, 0, 0, 0, 0 UNION ALLSELECT '2', 'C1', '10X100FLAT', 'S275JR', 0, 'S', 0, 0, 0, 0, 0, 0, 0 UNION ALLSELECT '4', 'P32', '115.5X5CF', 'S275JR', 0, 'S', 0, 0, 0, 0, 0, 0, 0 UNION ALLSELECT 'A19', 'C53', '70X70X8RSA', 'S275JR', 0, 'F', 1, 0, 0, 0, 0, 0, 0 E 12°55'05.25"N 56°04'39.16" |
 |
|
chenko
Starting Member
24 Posts |
Posted - 2007-09-27 : 11:25:49
|
[code]DECLARE @Sample TABLE (ID INT IDENTITY(1, 1), AMark VARCHAR(3), Mark VARCHAR(3), Section VARCHAR(12), Grade CHAR(6), Bevel BIT, SF CHAR(1), Opt1 BIT, Opt2 BIT, Opt3 BIT, Opt4 BIT, Opt5 BIT, Opt6 BIT, Opt7 BIT)INSERT @SampleSELECT '1', 'P3', '203X203X45UB', 'S275JR', 0, 'S', 0, 0, 0, 0, 0, 0, 0 UNION ALL *SELECT '1', 'P3', '203X203X45UB', 'S275JR', 0, 'S', 0, 0, 0, 0, 0, 0, 0 UNION ALL *SELECT '1', 'P3', '203X203X45UB', 'S275JR', 0, 'S', 0, 0, 0, 0, 0, 0, 0 UNION ALLSELECT '1', 'P3', '203X203X45UB', 'S275JR', 0, 'S', 0, 0, 0, 0, 0, 1, 0 UNION ALLSELECT '1', 'P3', '203X203X45UB', 'S275JR', 0, 'S', 1, 0, 0, 0, 0, 0, 0 UNION ALLSELECT '2', 'C1', '10X100FLAT', 'S275JR', 0, 'S', 0, 1, 0, 0, 0, 0, 0 UNION ALLSELECT '2', 'C1', '10X100FLAT', 'S275JR', 0, 'S', 0, 0, 0, 0, 0, 0, 0 UNION ALL *SELECT '4', 'P32', '115.5X5CF', 'S275JR', 0, 'S', 0, 0, 0, 0, 0, 0, 0 UNION ALLSELECT 'A19', 'C53', '70X70X8RSA', 'S275JR', 0, 'F', 1, 0, 0, 0, 0, 0, 0[/code]* - records that should be deletednb. There are 5 203X203X45UB, 2 with options, 3 without.You should finish with 2 with options, 1 without. It doesnt matter which 2 are deleted. |
 |
|
SwePeso
Patron Saint of Lost Yaks
30421 Posts |
Posted - 2007-09-27 : 13:43:02
|
It doesn't make sense.Why is line 1 & 2 marked for deletion together with line 7?Both line 1 & 2 has duplicates (3) and are "distinct" with lines 4 & 5.Both so has line 7 too! It is "distinct" with line 6.Or this way...Why save line 3 together with 4 & 5 and NOT save line 6 together with line 7? E 12°55'05.25"N 56°04'39.16" |
 |
|
chenko
Starting Member
24 Posts |
Posted - 2007-09-27 : 14:24:59
|
Every time you find an item with options set, I want to delete a line that matches that doesnt have any options set.So if you have 2 with options, I want to delete 2 without. |
 |
|
cat_jesus
Aged Yak Warrior
547 Posts |
Posted - 2007-09-27 : 15:07:22
|
Are the records with options being inserted? If so it might be easier to write a trigger that updates a corresponding record that has no options(if one exists) rather than inserting the one with options. |
 |
|
chenko
Starting Member
24 Posts |
Posted - 2007-09-27 : 15:23:32
|
No, but almost.It's the ones without the options that are being inserted (in bulk).I forgot to mention, that I need to also know, the records with options that are left without a corresponding item. (As these items need to be reviewed).I will explain what it is that I am actually doing overall, I left it out before to save complication.CAD package outputs a list (as a file) which has all these marks and section sizes etc. (its not a plain import, things have to be changed as it goes in etc).Then, as the peices (peices of steel) are moved through the factory the database needs to know when its passed through certain items, this is where the list of options comes from (and no, a single field with some kind of status number wont work).Problem is, that they can start working on the data from one of these files, when an update is produced that then has to be imported again, some items might have been processed (options marked).Currently I was deleting everything that wasnt processed, then easily I can get a list of stuff that was processed. I then just imported the new file again, and I was trying to delete an item that was due to be processed, for every item that had already been started. (and anything that was processed that now didnt exist, needs to be delt with, some how)I was trying to get away without having to use 2 seperate import processes for a new file vs trying to merge it. Though I am starting to feel now that im going to have to deal with this durning the import. |
 |
|
cat_jesus
Aged Yak Warrior
547 Posts |
Posted - 2007-09-27 : 15:27:58
|
How are the records without options being inserted in bulk? A stored procedure? BCP? DTS? |
 |
|
chenko
Starting Member
24 Posts |
Posted - 2007-09-27 : 15:30:27
|
No, it is a pretty manual process using an ASP script, some of the changes to the data is a little too complicated (and hardcoded, as much as it sucks but cant help it) for DTS |
 |
|
cat_jesus
Aged Yak Warrior
547 Posts |
Posted - 2007-09-27 : 16:06:15
|
Too complicated for DTS? You can use activeX in DTS. DTS is the epitome of complicated.It sounds to me like you're going to have to create two temp files and give each group of like records a sequence number.so to be very simplistic about this. A 1A 1A 1B 1B 1A 0A 0A 0A 0B 0Those are your records which match (on A or on B) the zeros are no options and the ones are records with options. In your temp tables you'll need to number each set thuslyA 1 1A 1 2 A 1 3B 1 1B 1 2A 0 1A 0 2A 0 3A 0 4B 0 1Once you do that it's a simple matter of running one delete statement that ties it all together and deletes just the records you want, matching on your key fields and your new sequence numbers. You'll have to tie back to your primary id of course.I don't have time to code it for you, but that's one way to solve the problem in very general terms. |
 |
|
SwePeso
Patron Saint of Lost Yaks
30421 Posts |
Posted - 2007-09-28 : 02:11:15
|
quote: Originally posted by chenko So if you have 2 with options, I want to delete 2 without.
AHA!That is the missing link... E 12°55'05.25"N 56°04'39.16" |
 |
|
SwePeso
Patron Saint of Lost Yaks
30421 Posts |
Posted - 2007-09-28 : 02:39:07
|
See how easy things are when you provide ALL business rules!Please enjoy!And next time you have a problem, be sure to post ALL relevant information.Good luck.-- Prepare sample dataDECLARE @Sample TABLE (ID INT IDENTITY(1, 1), AMark VARCHAR(3), Mark VARCHAR(3), Section VARCHAR(12), Grade CHAR(6), Bevel BIT, SF CHAR(1), Opt1 BIT, Opt2 BIT, Opt3 BIT, Opt4 BIT, Opt5 BIT, Opt6 BIT, Opt7 BIT)INSERT @SampleSELECT '1', 'P3', '203X203X45UB', 'S275JR', 0, 'S', 0, 0, 0, 0, 0, 0, 0 UNION ALLSELECT '1', 'P3', '203X203X45UB', 'S275JR', 0, 'S', 0, 0, 0, 0, 0, 0, 0 UNION ALLSELECT '1', 'P3', '203X203X45UB', 'S275JR', 0, 'S', 0, 0, 0, 0, 0, 0, 0 UNION ALLSELECT '1', 'P3', '203X203X45UB', 'S275JR', 0, 'S', 0, 0, 0, 0, 0, 1, 0 UNION ALLSELECT '1', 'P3', '203X203X45UB', 'S275JR', 0, 'S', 1, 0, 0, 0, 0, 0, 0 UNION ALLSELECT '2', 'C1', '10X100FLAT', 'S275JR', 0, 'S', 0, 1, 0, 0, 0, 0, 0 UNION ALLSELECT '2', 'C1', '10X100FLAT', 'S275JR', 0, 'S', 0, 0, 0, 0, 0, 0, 0 UNION ALLSELECT '4', 'P32', '115.5X5CF', 'S275JR', 0, 'S', 0, 0, 0, 0, 0, 0, 0 UNION ALLSELECT 'A19', 'C53', '70X70X8RSA', 'S275JR', 0, 'F', 1, 0, 0, 0, 0, 0, 0-- Show the ID for the records to deleteSELECT s1.ID--DELETEFROM @Sample AS s1WHERE NOT 1 IN (s1.Opt1, s1.Opt2, s1.Opt3, s1.Opt4, s1.Opt5, s1.Opt6, s1.Opt7) AND ( SELECT COUNT(*) FROM @Sample AS s2 WHERE s2.AMark = s1.AMark AND s2.AMark = s1.AMark AND s2.Mark = s1.Mark AND s2.Section = s1.Section AND s2.Grade = s1.Grade AND s2.Bevel = s1.Bevel AND s2.SF = s1.SF AND 1 IN (s2.Opt1, s2.Opt2, s2.Opt3, s2.Opt4, s2.Opt5, s2.Opt6, s2.Opt7) ) >= ( SELECT COUNT(*) FROM @Sample AS s3 WHERE s3.AMark = s3.AMark AND s3.AMark = s1.AMark AND s3.Mark = s1.Mark AND s3.Section = s1.Section AND s3.Grade = s1.Grade AND s3.Bevel = s1.Bevel AND s3.SF = s1.SF AND NOT 1 IN (s3.Opt1, s3.Opt2, s3.Opt3, s3.Opt4, s3.Opt5, s3.Opt6, s3.Opt7) AND s3.ID <= s1.ID ) E 12°55'05.25"N 56°04'39.16" |
 |
|
Next Page
|
|
|