| Author |
Topic |
|
dchbiv
Starting Member
10 Posts |
Posted - 2012-02-16 : 09:37:40
|
| I have a problem.I have a table that has missing transaction numbers (TX_NUM) in it. I need to identify the missing values in these breaks.the table is laid out as such:company_id Unit_ID Device_id TX_DT_TM TX_NUM10 1 51 2/12/12 12:00 AM 78910 1 51 2/12/12 12:10 AM 79010 1 51 2/12/12 12:20 AM 79110 1 51 2/12/12 12:30 AM 79310 1 51 2/12/12 12:40 AM 79410 1 51 2/12/12 12:50 AM 79520 2 48 2/12/12 12:00 AM 59420 2 48 2/12/12 12:10 AM 59520 2 48 2/12/12 12:20 AM 59620 2 48 2/12/12 12:30 AM 59820 2 48 2/12/12 12:40 AM 59920 2 48 2/12/12 12:50 AM 60020 2 48 2/12/12 1:00 AM 60220 2 48 2/12/12 1:10 AM 60320 2 48 2/12/12 1:20 AM 60420 2 48 2/12/12 1:30 AM 60520 2 62 2/12/12 12:10 AM 999520 2 62 2/12/12 12:20 AM 999620 2 62 2/12/12 12:30 AM 999720 2 62 2/12/12 12:40 AM 999820 2 62 2/12/12 12:50 AM 999920 2 62 2/12/12 1:00 AM 120 2 62 2/12/12 1:20 AM 320 2 62 2/12/12 1:30 AM 420 2 62 2/12/12 1:40 AM 520 2 62 2/12/12 1:50 AM 620 2 62 2/12/12 2:00 AM 7I need to query this table by TX_DT_TM and identify missing TX_NUM values for each combination of Company_ID, Unit_IT and Device_ID. NOTE: the max value for the TX_NUM for each device is 9999, it then rolls over to 1 and starts over again.I am in an environment where there are no other DB people to bounce this off of. I hope someone can help.I am seriously suffering from some caffeine overload and balancing 7 other reports to get completed in a veryshort time. |
|
|
X002548
Not Just a Number
15586 Posts |
Posted - 2012-02-16 : 09:42:23
|
you need to use [ code] [ /code] tags to retain formating (with out the spacecompany_id Unit_ID Device_id TX_DT_TM TX_NUM10 1 51 2/12/12 12:00 AM 78910 1 51 2/12/12 12:10 AM 79010 1 51 2/12/12 12:20 AM 79110 1 51 2/12/12 12:30 AM 79310 1 51 2/12/12 12:40 AM 79410 1 51 2/12/12 12:50 AM 79520 2 48 2/12/12 12:00 AM 59420 2 48 2/12/12 12:10 AM 59520 2 48 2/12/12 12:20 AM 59620 2 48 2/12/12 12:30 AM 59820 2 48 2/12/12 12:40 AM 59920 2 48 2/12/12 12:50 AM 60020 2 48 2/12/12 1:00 AM 60220 2 48 2/12/12 1:10 AM 60320 2 48 2/12/12 1:20 AM 60420 2 48 2/12/12 1:30 AM 60520 2 62 2/12/12 12:10 AM 999520 2 62 2/12/12 12:20 AM 999620 2 62 2/12/12 12:30 AM 999720 2 62 2/12/12 12:40 AM 999820 2 62 2/12/12 12:50 AM 999920 2 62 2/12/12 1:00 AM 120 2 62 2/12/12 1:20 AM 320 2 62 2/12/12 1:30 AM 420 2 62 2/12/12 1:40 AM 520 2 62 2/12/12 1:50 AM 620 2 62 2/12/12 2:00 AM 7 Brett8-)Hint: Want your questions answered fast? Follow the direction in this linkhttp://weblogs.sqlteam.com/brettk/archive/2005/05/25/5276.aspxWant to help yourself?http://msdn.microsoft.com/en-us/library/ms130214.aspxhttp://weblogs.sqlteam.com/brettk/http://brettkaiser.blogspot.com/ |
 |
|
|
X002548
Not Just a Number
15586 Posts |
|
|
dchbiv
Starting Member
10 Posts |
Posted - 2012-02-16 : 09:58:09
|
Sorry, I left off the CODE tagsBTW the following are missing10 1 51 2/12/12 12:20 AM 79220 2 48 2/12/12 12:30 AM 59720 2 48 2/12/12 1:00 AM 60120 2 62 2/12/12 1:20 AM 2 I have a problem.I have a table that has missing transaction numbers (TX_NUM) in it. I need to identify the missing values in these breaks.the table is laid out as such:company_id Unit_ID Device_id TX_DT_TM TX_NUM10 1 51 2/12/12 12:00 AM 78910 1 51 2/12/12 12:10 AM 79010 1 51 2/12/12 12:20 AM 79110 1 51 2/12/12 12:30 AM 79310 1 51 2/12/12 12:40 AM 79410 1 51 2/12/12 12:50 AM 79520 2 48 2/12/12 12:00 AM 59420 2 48 2/12/12 12:10 AM 59520 2 48 2/12/12 12:20 AM 59620 2 48 2/12/12 12:30 AM 59820 2 48 2/12/12 12:40 AM 59920 2 48 2/12/12 12:50 AM 60020 2 48 2/12/12 1:00 AM 60220 2 48 2/12/12 1:10 AM 60320 2 48 2/12/12 1:20 AM 60420 2 48 2/12/12 1:30 AM 60520 2 62 2/12/12 12:10 AM 999520 2 62 2/12/12 12:20 AM 999620 2 62 2/12/12 12:30 AM 999720 2 62 2/12/12 12:40 AM 999820 2 62 2/12/12 12:50 AM 999920 2 62 2/12/12 1:00 AM 120 2 62 2/12/12 1:20 AM 320 2 62 2/12/12 1:30 AM 420 2 62 2/12/12 1:40 AM 520 2 62 2/12/12 1:50 AM 620 2 62 2/12/12 2:00 AM 7 I need to query this table by TX_DT_TM and identify missing TX_NUM values for each combination of Company_ID, Unit_IT and Device_ID. NOTE: the max value for the TX_NUM for each device is 9999, it then rolls over to 1 and starts over again.I am in an environment where there are no other DB people to bounce this off of. I hope someone can help.I am seriously suffering from some caffeine overload and balancing 7 other reports to get completed in a veryshort time. |
 |
|
|
X002548
Not Just a Number
15586 Posts |
|
|
dchbiv
Starting Member
10 Posts |
Posted - 2012-02-16 : 10:46:38
|
It was at the top of my second postthe following are missing rows:10 1 51 2/12/12 12:20 AM 79220 2 48 2/12/12 12:30 AM 59720 2 48 2/12/12 1:00 AM 60120 2 62 2/12/12 1:20 AM 2 |
 |
|
|
X002548
Not Just a Number
15586 Posts |
|
|
dchbiv
Starting Member
10 Posts |
Posted - 2012-02-16 : 11:51:16
|
| Its a break in the series. Imagine you are looking for missing checks from your check register. The TX_NUM could be thought of as a number of a check. |
 |
|
|
X002548
Not Just a Number
15586 Posts |
|
|
dchbiv
Starting Member
10 Posts |
Posted - 2012-02-16 : 12:29:27
|
| The data cannot be arbitrarily deleted. This represents transmitted data from an external log. If there are missing tx_nums then we need to identify them and fix the data in the table. This is for auditing purposes. I cannot use a join to a numbers table because in the tx_num can roll over from 9999 to 1 during a given day. I need to step through the days data for each company,unit and device to identify a break in a sequence by comparing each record s value of tx_num to the previous records TX_NUM to see if it is indeed 1 greater. I also need to allow for the rollover from 9999 to 1 during that comparison |
 |
|
|
X002548
Not Just a Number
15586 Posts |
Posted - 2012-02-16 : 12:46:12
|
quote: Originally posted by dchbiv The data cannot be arbitrarily deleted.
OKquote: This represents transmitted data from an external log. If there are missing tx_nums then we need to identify them and fix the data in the table.
What is the Process to "Fix" Themquote: This is for auditing purposes. I cannot use a join to a numbers table because in the tx_num can roll over from 9999 to 1 during a given day.
OK, So then Just add the day to the queryquote: I need to step through the days data for each company,unit and device to identify a break in a sequence by comparing each record s value of tx_num to the previous records TX_NUM to see if it is indeed 1 greater. I also need to allow for the rollover from 9999 to 1 during that comparison
If you say so, then use a CURSOR.BUT, I still think you could come up with a set based operation. How many days are you looking at?I imaging the most Current, because you would have fixed yesterday already..no?Brett8-)Hint: Want your questions answered fast? Follow the direction in this linkhttp://weblogs.sqlteam.com/brettk/archive/2005/05/25/5276.aspxWant to help yourself?http://msdn.microsoft.com/en-us/library/ms130214.aspxhttp://weblogs.sqlteam.com/brettk/http://brettkaiser.blogspot.com/ |
 |
|
|
dchbiv
Starting Member
10 Posts |
Posted - 2012-02-16 : 13:14:35
|
| The method to fix would be identify the failed transmissions and resubmit them hence inserting the missing data.How would a set based operation help me. The tx_num fieldsprobalby won't start at 1 for any given day, plus the likelyhood that they will roll over from 9999 to 1 in a given time period is very likely. |
 |
|
|
X002548
Not Just a Number
15586 Posts |
|
|
X002548
Not Just a Number
15586 Posts |
Posted - 2012-02-16 : 13:27:04
|
So Rule #1Which Day(S) does the missing sequences belong to in this Example?CREATE TABLE myTable99(company_id int, Unit_ID int, Device_id int, TX_DT_TM datetime, TX_NUM int)GOINSERT INTO myTable99 (company_id, Unit_ID, Device_id, TX_DT_TM, TX_NUM)SELECT SELECT 10, 1, 51, '2/11/12 11:50 PM', 787 UNION ALLSELECT 10, 1, 51, '2/12/12 12:00 AM', 789 UNION ALLSELECT 10, 1, 51, '2/12/12 12:10 AM', 791 UNION ALLSELECT 10, 1, 51, '2/12/12 11:50 PM', 792 UNION ALLSELECT 10, 1, 51, '2/13/12 12:10 AM', 794GODROP TABLE myTable99GO Brett8-)Hint: Want your questions answered fast? Follow the direction in this linkhttp://weblogs.sqlteam.com/brettk/archive/2005/05/25/5276.aspxWant to help yourself?http://msdn.microsoft.com/en-us/library/ms130214.aspxhttp://weblogs.sqlteam.com/brettk/http://brettkaiser.blogspot.com/ |
 |
|
|
dchbiv
Starting Member
10 Posts |
Posted - 2012-02-21 : 08:17:04
|
I will only be examining one day at a time, but for each combination of company_id,Unit_ID, and Device_id I am looking for records that do not have consecutive TX_NUMso in my example the following would be logged as missing:company_id Unit_ID Device_id TX_DT_TM TX_NUM10 1 51 2/12/12 12:20 AM 79220 2 48 2/12/12 12:30 AM 59720 2 48 2/12/12 1:00 AM 60120 2 62 2/12/12 1:20 AM 2 |
 |
|
|
Transact Charlie
Master Smack Fu Yak Hacker
3451 Posts |
Posted - 2012-02-21 : 13:17:33
|
maybe thisSELECT *FROM ( SELECT [company_Id] , [Unit_Id] , [Device_id] , [TX_num] , ROW_NUMBER() OVER ( PARTITION BY [CompanyID], [Unit_ID], [Device_ID] ORDER BY [TX_Num] DESC ) AS [rnk] FROM <TABLE> ) AS t1WHERE t1.[rnk] > 1 AND NOT EXISTS ( SELECT 1 FROM <TABLE> AS t2 WHERE t2.[company_id] = t1.[Company_ID] AND t2.[Unit_ID] = t1.[Unit_ID] AND t2.[Device_id] = t1.[Device_id] AND t2.[TX_Num] = t1.[TX_Num] + 1 ) Where you are looking at each row that isn't the latest for the combinations or company_id, unit_id, device_id and finding them if there is no consecutive match.This will find the rows with no parents - you just need to add 1 to find the next.it won't find all possible gaps -- just the ones immediately after the latest in a run.Charlie===============================================================Msg 3903, Level 16, State 1, Line 1736The ROLLBACK TRANSACTION request has no corresponding BEGIN TRANSACTION |
 |
|
|
dchbiv
Starting Member
10 Posts |
Posted - 2012-02-21 : 15:48:01
|
| I need to report every missing record. Plus remember, I have a rollover event where TX_NUM rolls from 9999 to 1 during the day. |
 |
|
|
|