Level Extreme platform
Subscription
Corporate profile
Products & Services
Support
Legal
Français
Delete records where 2 col are the same
Message
 
 
To
24/09/2017 13:29:36
General information
Forum:
Visual FoxPro
Category:
Databases,Tables, Views, Indexing and SQL syntax
Miscellaneous
Thread ID:
01654526
Message ID:
01654531
Views:
51
>>Hi,
>>
>>Here is another Sunday challenge I am trying to resolve. Here is a simplified case, but close to what I am trying to do.
>>
>>Table has the following 2 columns/fields, sorted by ID, and the values as shown below:
>>
>>
>>ID                 CATEGORY
>>0001             ABC
>>0002             NBC
>>0002             NBC
>>0002             NBC
>>0003             CBS
>>0004             123
>>0004              ABC
>>0005             567
>>0005             567
>>0006             009
>>0006             123
>>0006             123
>>
>>I want to delete all records where the ID and CATEGORY are matching, it does not matter 1 records or more. So the recult would be as follows:
>>
>>ID                 CATEGORY
>>0004             123
>>0004              ABC
>>0006             009
>>0006             123
>>0006             123
>>
>>
>>Because at least one of the records in the group by ID is not matching column CATEGORY.
>>
>>Can this be done?
>>
>>TIA
>
>Here it goes (hopefully):
>
>
>SET DELETED ON
>
>CREATE CURSOR Original (ID Character(4), Category Character(3))
>
>LOCAL SourceData AS String
>LOCAL ARRAY SourceLines(1)
>LOCAL LoopIndex AS Integer
>
>TEXT TO m.SourceData NOSHOW
>0001             ABC
>0002             NBC
>0002             NBC
>0002             NBC
>0003             CBS
>0004             123
>0004              ABC
>0005             567
>0005             567
>0006             009
>0006             123
>0006             123
>ENDTEXT
>
>FOR m.LoopIndex = 1 TO ALINES(m.SourceLines, m.SourceData)
>	INSERT INTO Original VALUES (LEFT(m.SourceLines(m.LoopIndex), 4), RIGHT(m.SourceLines(m.LoopIndex), 3))
>ENDFOR
>
>DELETE Original ;
>	FROM (SELECT ID, COUNT(DISTINCT Category) AS Categories FROM Original GROUP BY ID HAVING Categories = 1) AS Matcher ;
>	WHERE Original.Id = Matcher.Id
>
>BROWSE
>
Thank you, Antonio. I did it using some "crude" force (of DO WHILE). But I will try your - much better - approach too.
"The creative process is nothing but a series of crises." Isaac Bashevis Singer
"My experience is that as soon as people are old enough to know better, they don't know anything at all." Oscar Wilde
"If a nation values anything more than freedom, it will lose its freedom; and the irony of it is that if it is comfort or money that it values more, it will lose that too." W.Somerset Maugham
Previous
Reply
Map
View

Click here to load this message in the networking platform