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.

 All Forums
 SQL Server 2000 Forums
 SQL Server Development (2000)
 query help to delete obsolete records

Author  Topic 

thanksfor help
Posting Yak Master

106 Posts

Posted - 2008-10-14 : 16:59:37
Hi,

Hi have data table in which past and current period records are there. I need to delete the customer who have only past period records with zero ammount even if there are any past period records with value then the customer should not be deleted.

Here is sample data,

custid custname month ppflag amount
1234 ABCD Jan 08 True 900
1234 ABCD MAR 08 True 0
1234 ABCD Jun 08 True 0
1234 ABCD Aug 08 True 0

2345 XYZ Jan 08 True 0
2345 XYZ FEB 08 True 0
2345 XYZ SEP 08 True 0

6789 PQRS Jan 08 True 0
6789 PQRS Jun 08 True 100
6789 PQRS OCT 08 FALSE 1000
6789 PQRS NOV 08 False 500

the query should only return XYZ

Thanks in advance

hanbingl
Aged Yak Warrior

652 Posts

Posted - 2008-10-14 : 19:22:24
select custid from @datatable where amount = 0 and ppflag = 'true'
except
select custid from @datatable where amount <> 0 or ppflag = 'false'
Go to Top of Page

hanbingl
Aged Yak Warrior

652 Posts

Posted - 2008-10-14 : 19:23:59
oops sql 2000:


select custid from @datatable where amount = 0 and ppflag = 'true'
and custid not in (
select custid from @datatable where amount <> 0 or ppflag = 'false'
)
Go to Top of Page

madhivanan
Premature Yak Congratulator

22864 Posts

Posted - 2008-10-15 : 03:34:31
or
select custid from @datatable as t1 where amount = 0 and ppflag = 'true'
and not exists
(select * from @datatable where (amount <> 0 or ppflag = 'false') and custid=t1.custid)


Madhivanan

Failing to plan is Planning to fail
Go to Top of Page

SwePeso
Patron Saint of Lost Yaks

30421 Posts

Posted - 2008-10-15 : 03:46:47
1/8th of the reads required for previous suggestions
SELECT		custID
FROM @Sample
GROUP BY custID
HAVING MIN(CASE WHEN amount = 0 AND ppFlag = 'true' THEN 1 ELSE 0 END) = 1


E 12°55'05.63"
N 56°04'39.26"
Go to Top of Page

SwePeso
Patron Saint of Lost Yaks

30421 Posts

Posted - 2008-10-15 : 04:01:25
1/11th of the reads

CREATE TABLE	#Sample
(
custID INT,
custName VARCHAR(4),
mnth CHAR(6),
ppFlag VARCHAR(5),
amount INT
)

INSERT #Sample
SELECT 1234, 'ABCD', 'Jan 08', 'True', 900 UNION ALL
SELECT 1234, 'ABCD', 'MAR 08', 'True', 0 UNION ALL
SELECT 1234, 'ABCD', 'Jun 08', 'True', 0 UNION ALL
SELECT 1234, 'ABCD', 'Aug 08', 'True', 0 UNION ALL
SELECT 2345, 'XYZ', 'Jan 08', 'True', 0 UNION ALL
SELECT 2345, 'XYZ', 'FEB 08', 'True', 0 UNION ALL
SELECT 2345, 'XYZ', 'SEP 08', 'True', 0 UNION ALL
SELECT 6789, 'PQRS', 'Jan 08', 'True', 0 UNION ALL
SELECT 6789, 'PQRS', 'Jun 08', 'True', 100 UNION ALL
SELECT 6789, 'PQRS', 'OCT 08', 'FALSE', 1000 UNION ALL
SELECT 6789, 'PQRS', 'NOV 08', 'False', 500

create index ix_sample on #sample (custid, ppflag, amount)

-- Habingl
select custid from #Sample where amount = 0 and ppflag = 'true'
and custid not in (
select custid from #Sample where amount <> 0 or ppflag = 'false'
)

-- Madhi
select custid from #Sample as t1 where amount = 0 and ppflag = 'true'
and not exists
(select * from #Sample where (amount <> 0 or ppflag = 'false') and custid=t1.custid)

-- Peso
SELECT custID
FROM #Sample
GROUP BY custID
HAVING MIN(CASE WHEN amount = 0 AND ppFlag = 'true' THEN 1 ELSE 0 END) = 1

drop table #sample



E 12°55'05.63"
N 56°04'39.26"
Go to Top of Page

thanksfor help
Posting Yak Master

106 Posts

Posted - 2008-10-15 : 11:58:15
Thank you all for your support. I have got the idea.
Go to Top of Page

SwePeso
Patron Saint of Lost Yaks

30421 Posts

Posted - 2008-10-15 : 12:08:41
Good luck!



E 12°55'05.63"
N 56°04'39.26"
Go to Top of Page
   

- Advertisement -