dbTalk Databases Forums  

SQL to find missing entries

comp.databases.rdb comp.databases.rdb


Discuss SQL to find missing entries in the comp.databases.rdb forum.



Reply
 
Thread Tools Display Modes
  #1  
Old   
Alder
 
Posts: n/a

Default SQL to find missing entries - 01-28-2004 , 05:01 PM






I'm hoping someone could drop me a few hints for finding missing data with
SQL. We have an accounting table that is supposed to keep a running record
of each year that a particular item remains 'active'. Dates are stored as
8-character strings, so the data looks something like this:

ITEM DATE_STR
xxx 20041221
xxx 20031221 <-- 2002 is a
xxx 20011221 <-- missing entry
yyy 20040618
yyy 20030618
zzz 20050712
zzz 20040712
zzz 20030712
....

What we'd like to be able to find is all ITEMs where one or more years are
missing. Any idea where to begin would be great...!

Thanks,
Terry

--
Disclaimer: Any comments made are personal and do not reflect the thoughts
or policies of my employer.



Reply With Quote
  #2  
Old   
Michael Austin
 
Posts: n/a

Default Re: SQL to find missing entries - 01-28-2004 , 08:57 PM






Alder wrote:

Quote:
I'm hoping someone could drop me a few hints for finding missing data with
SQL. We have an accounting table that is supposed to keep a running record
of each year that a particular item remains 'active'. Dates are stored as
8-character strings, so the data looks something like this:

ITEM DATE_STR
xxx 20041221
xxx 20031221 <-- 2002 is a
xxx 20011221 <-- missing entry
yyy 20040618
yyy 20030618
zzz 20050712
zzz 20040712
zzz 20030712
...

What we'd like to be able to find is all ITEMs where one or more years are
missing. Any idea where to begin would be great...!

Thanks,
Terry

--
Disclaimer: Any comments made are personal and do not reflect the thoughts
or policies of my employer.


which database engine and version?




Reply With Quote
  #3  
Old   
Alder
 
Posts: n/a

Default Re: SQL to find missing entries - 01-28-2004 , 11:57 PM



Michael Austin wrote::
Quote:
which database engine and version?


Sorry, I have this bad habit of leaving out the important bits :-(

Here's our environment:

-- Network and OS versions --------
Process Software MultiNet V4.4 Rev A-X
AlphaServer ES40
OpenVMS AXP V7.3-1

-- Rdb and SQL-client versions ------
Executing RMU for Oracle Rdb V7.0-7
Current version of SQL is: Oracle Rdb SQL V7.0-7

Thanks,
Terry



Reply With Quote
  #4  
Old   
David
 
Posts: n/a

Default Re: SQL to find missing entries - 02-13-2004 , 04:10 PM



See if this works:
select distinct
t.item
from
item_table t
where
-- number of distinct years for this item
(select count(distinct(cast(t1.date_str as char(4))))
from item_table t1
where t1.item = t.item)
-- different from year span (max minus min plus 1)
<> (select cast(cast(max(t2.date_str) as char(4)) as integer)
from item_table t2)
- (select cast(cast(min(t3.date_str) as char(4)) as integer)
from item_table t3)
+ 1
;



"Alder" <MUNDDGNTDYTV (AT) spammotel (DOT) com> wrote

Quote:
I'm hoping someone could drop me a few hints for finding missing data with
SQL. We have an accounting table that is supposed to keep a running record
of each year that a particular item remains 'active'. Dates are stored as
8-character strings, so the data looks something like this:

ITEM DATE_STR
xxx 20041221
xxx 20031221 <-- 2002 is a
xxx 20011221 <-- missing entry
yyy 20040618
yyy 20030618
zzz 20050712
zzz 20040712
zzz 20030712
...

What we'd like to be able to find is all ITEMs where one or more years are
missing. Any idea where to begin would be great...!

Thanks,
Terry

Reply With Quote
  #5  
Old   
Alder
 
Posts: n/a

Default Re: SQL to find missing entries - 02-15-2004 , 02:08 AM



David wrote::

Quote:
See if this works:
select distinct
t.item
from
item_table t
where
-- number of distinct years for this item
(select count(distinct(cast(t1.date_str as char(4))))
from item_table t1
where t1.item = t.item)
-- different from year span (max minus min plus 1)
(select cast(cast(max(t2.date_str) as char(4)) as integer)
from item_table t2)
- (select cast(cast(min(t3.date_str) as char(4)) as integer)
from item_table t3)
+ 1
;
Looks nice, David. I'll run it against my table when I get back to work
Monday/Tuesday. Thanks for the great effort!

Alder


Reply With Quote
  #6  
Old   
Alder
 
Posts: n/a

Default Re: SQL to find missing entries - 02-16-2004 , 03:48 PM



David,

I tried the SQL SELECT statement you provided, and it produced a result for
almost every record. Looking at the SELECT statemetns to the right of the
inequality operator it looked like these statements required their own WHERE
clause to limit them to the same record as was being processed by the SELECT
statement to the left of the inequality.

If this is correct in theory, I obviously got something wrong in the
practice, because the query is taking hours. This is what I tried:

---
select distinct
t.acct_title_no
from
title_accounting t
where
-- number of distinct years for this item
(select count(distinct(cast(t1.acct_pd_to_dt as char(4))))
from title_accounting t1
where t1.acct_title_no = t.acct_title_no)

-- different from year span (max minus min plus 1)
<> (select cast(cast(max(t2.acct_pd_to_dt) as char(4)) as integer)
from title_accounting t2
where t2.acct_title_no = t.acct_title_no)
- (select cast(cast(min(t3.acct_pd_to_dt) as char(4)) as integer)
from title_accounting t3
where t3.acct_title_no = t.acct_title_no)
+ 1
;

Thanks again,
Alder

"Alder" <PGDEHMKOKIMD (AT) spammotel (DOT) com> wrote

Quote:
David wrote::

See if this works:
select distinct
t.item
from
item_table t
where
-- number of distinct years for this item
(select count(distinct(cast(t1.date_str as char(4))))
from item_table t1
where t1.item = t.item)
-- different from year span (max minus min plus 1)
(select cast(cast(max(t2.date_str) as char(4)) as integer)
from item_table t2)
- (select cast(cast(min(t3.date_str) as char(4)) as integer)
from item_table t3)
+ 1
;

Looks nice, David. I'll run it against my table when I get back to work
Monday/Tuesday. Thanks for the great effort!

Alder



Reply With Quote
  #7  
Old   
Dr. Dweeb
 
Posts: n/a

Default Re: SQL to find missing entries - 02-20-2004 , 11:54 AM



Alder wrote:
Quote:
David,

I tried the SQL SELECT statement you provided, and it produced a
result for almost every record. Looking at the SELECT statemetns to
the right of the inequality operator it looked like these statements
required their own WHERE clause to limit them to the same record as
was being processed by the SELECT statement to the left of the
inequality.

If this is correct in theory, I obviously got something wrong in the
practice, because the query is taking hours. This is what I tried:

I have not reviewed the SQL, however I point out to you that performance and
logical correctness have no relationship to each other.

Dr. Dweeb
Quote:
---
select distinct
t.acct_title_no
from
title_accounting t
where
-- number of distinct years for this item
(select count(distinct(cast(t1.acct_pd_to_dt as char(4))))
from title_accounting t1
where t1.acct_title_no = t.acct_title_no)

-- different from year span (max minus min plus 1)
(select cast(cast(max(t2.acct_pd_to_dt) as char(4)) as
integer) from title_accounting t2
where t2.acct_title_no = t.acct_title_no)
- (select cast(cast(min(t3.acct_pd_to_dt) as char(4)) as
integer) from title_accounting t3
where t3.acct_title_no = t.acct_title_no)
+ 1
;

Thanks again,
Alder

"Alder" <PGDEHMKOKIMD (AT) spammotel (DOT) com> wrote in message
news:tOFXb.5622$Hy3.5465 (AT) edtnps89 (DOT) ..
David wrote::

See if this works:
select distinct
t.item
from
item_table t
where
-- number of distinct years for this item
(select count(distinct(cast(t1.date_str as char(4))))
from item_table t1
where t1.item = t.item)
-- different from year span (max minus min plus 1)
(select cast(cast(max(t2.date_str) as char(4)) as integer)
from item_table t2)
- (select cast(cast(min(t3.date_str) as char(4)) as integer)
from item_table t3)
+ 1
;

Looks nice, David. I'll run it against my table when I get back to
work Monday/Tuesday. Thanks for the great effort!

Alder



Reply With Quote
Reply




Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off



Powered by vBulletin Version 3.5.3
Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.