| Author |
Topic |
|
SQLJunior
Starting Member
34 Posts |
Posted - 2007-04-16 : 03:57:19
|
| Hi,I have a table containing a list of pbxid's, called tblPBXIDTemp.DDL:if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[tblPBXIDTemp]') and OBJECTPROPERTY(id, N'IsUserTable') = 1)drop table [dbo].[tblPBXIDTemp]GOCREATE TABLE [dbo].[tblPBXIDTemp] ( [PBXID] [int] NULL ) ON [PRIMARY]GODML:INSERT INTO tblPBXIDTemp(pbxID)SELECT 543 UNION ALLSELECT 10961 UNION ALLSELECT 1012 UNION ALLSELECT 83 and a DialupLog table which holds all dialups for pbx's (therfore has more than one entry for a pbxid).DDL:if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[tblDialupLog]') and OBJECTPROPERTY(id, N'IsUserTable') = 1)drop table [dbo].[tblDialupLog]GOCREATE TABLE [dbo].[tblDialupLog] ([RegionalID] [int] NOT NULL ,[PBXID] [int] NULL ,[DialupDT] [datetime] NULL ,[DongleAccessNum] [varchar] (64) NULL ,[CLI] [varchar] (64) NULL ,[RegionalDialup] [varchar] (50) NULL ) ON [PRIMARY]GODML:INSERT INTO tblDialupLog(PBXID,DialupDT ,DongleAccessNum,CLI,RegionalID,RegionalDialup )SELECT 83,'8/8/2006 8:58:11 AM' ,'T2 UQ 28924','0132493700',0 , '0800003554' UNION ALLSELECT 543,'8/8/2006 8:55:44 AM','T0 UA 33902','0123623500',0,'0800003554' UNION ALLSELECT 1012, '8/8/2006 9:02:54 AM', 'T0 UA 41261', '0173011050', 0 ,'0800003554' UNION ALLSELECT 1219, '8/8/2006 8:59:03 AM' ,'T3 ZD 02031', '0152958095', 0,'0800003554' UNION ALLSELECT 543,'8/8/2007 8:55:44 AM','T0 UA 33902','0123623500',0,'0800003554' UNION ALLSELECT 1012, '8/8/2007 9:02:54 AM', 'T0 UA 41261', '0173011050', 0 ,'0800003554' UNION ALLSELECT 16499, '8/8/2006 8:51:28 AM', 'T2 ZD 32892', '0543375100', 0, '0800004249' Expected Result after i run the script, and on completion, do a Select * from tblDialupLog:83,'8/8/2006 8:58:11 AM' ,'T2 UQ 28924','0132493700',0 , '0800003554'1219, '8/8/2006 8:59:03 AM' ,'T3 ZD 02031', '0152958095', 0,'0800004249'543,'8/8/2007 8:55:44 AM','T0 UA 33902','0123623500',0,'0800003554'1012, '8/8/2007 9:02:54 AM', 'T0 UA 41261', '0173011050', 0 ,'0800003554'16499, '8/8/2006 8:51:28 AM', 'T2 ZD 32892', '0543375100', 0, '0800004249'My script needs to delete all entries from tblDialupLog where the pbxid is in tblPBXIDTemp except the TOP 1 (latest dialup according to DialupDT) entry for that particular PBXID.Any Ideas would be great?Thanks;o) |
|
|
harsh_athalye
Master Smack Fu Yak Hacker
5581 Posts |
Posted - 2007-04-16 : 04:06:35
|
Something like this?Delete dlfrom tblDialupLog dl join tblPBXIDTemp t1on dl.pbxID = t1.pbxIDJoin ( Select PbxID, max(DialupDt) as DialupDt from tblDialupLog Group by PbxID) as t2on dl.pbxid = t2.pbxid and dl.dialupdt < t2.dialupdt Harsh AthalyeIndia."The IMPOSSIBLE is often UNTRIED" |
 |
|
|
SwePeso
Patron Saint of Lost Yaks
30421 Posts |
Posted - 2007-04-16 : 04:16:32
|
| [code]SELECT DISTINCT x.PBXID, x.MaxDT AS Dialup_DT, l.DongleAccessNum, l.CLI, l.RegionalID, l.RegionalDialupINTO #TempFROM ( SELECT l.PBXID, MAX(DialupDT) AS MaxDT FROM tblDialupLog AS l INNER JOIN tblPBXIDTemp AS t ON t.pbxID = l.PBXID GROUP BY l.PBXID ) AS xINNER JOIN tblDialupLog AS l ON l.pbxID = x.PBXID AND l.Dialup_DT = x.MaxDTDELETE lFROM tblDialupLog AS lINNER JOIN tblPBXIDTemp AS t ON t.pbxID = l.PBXIDINSERT tblDialupLog ( PBXID, DialupDT, DongleAccessNum, CLI, RegionalID, RegionalDialup )SELECT PBXID, DialupDT, DongleAccessNum, CLI, RegionalID, RegionalDialupFROM #TempDROP TABLE #Temp[/code]Peter LarssonHelsingborg, Sweden |
 |
|
|
SwePeso
Patron Saint of Lost Yaks
30421 Posts |
Posted - 2007-04-16 : 04:19:06
|
| Harsh, there are two of theseSELECT 1012, '8/8/2007 9:02:54 AM', 'T0 UA 41261', '0173011050', 0 ,'0800003554' UNION ALLSELECT 1012, '8/8/2007 9:02:54 AM', 'T0 UA 41261', '0173011050', 0 ,'0800003554' UNION ALLand two of theseSELECT 543,'8/8/2006 8:55:44 AM','T0 UA 33902','0123623500',0,'0800003554' UNION ALLSELECT 543,'8/8/2007 8:55:44 AM','T0 UA 33902','0123623500',0,'0800003554' UNION ALLPeter LarssonHelsingborg, Sweden |
 |
|
|
harsh_athalye
Master Smack Fu Yak Hacker
5581 Posts |
Posted - 2007-04-16 : 04:27:49
|
quote: Originally posted by Peso Harsh, there are two of theseSELECT 1012, '8/8/2007 9:02:54 AM', 'T0 UA 41261', '0173011050', 0 ,'0800003554' UNION ALLSELECT 1012, '8/8/2007 9:02:54 AM', 'T0 UA 41261', '0173011050', 0 ,'0800003554' UNION ALLand two of theseSELECT 543,'8/8/2006 8:55:44 AM','T0 UA 33902','0123623500',0,'0800003554' UNION ALLSELECT 543,'8/8/2007 8:55:44 AM','T0 UA 33902','0123623500',0,'0800003554' UNION ALLPeter LarssonHelsingborg, Sweden
No, Peter.Both dates are for different years. One is for 2006 and one is for 2007.Harsh AthalyeIndia."The IMPOSSIBLE is often UNTRIED" |
 |
|
|
SwePeso
Patron Saint of Lost Yaks
30421 Posts |
Posted - 2007-04-16 : 04:29:30
|
Great spot!Are you using SQL Server 2005?DELETE tFROM ( SELECT pbxID, ROW_NUMBER() OVER (PARTITION BY pbxID ORDER BY dialupDT DESC) AS RecID FROM tblDialupLog ) AS tINNER JOIN tblPBXIDTemp AS l ON l.pbxID = t.pbxIDWHERE t.RecID > 1 Peter LarssonHelsingborg, Sweden |
 |
|
|
SQLJunior
Starting Member
34 Posts |
Posted - 2007-04-16 : 04:56:35
|
| Thank you so much to both of you for your help... and no Peter, this is going to be run on sql server 7, it is a very old system.Harsh your query works wonderfully... Kind Regards, |
 |
|
|
SwePeso
Patron Saint of Lost Yaks
30421 Posts |
Posted - 2007-04-16 : 05:35:50
|
| Yes, and it is fast too with a large number of records.However, it don't handle possible duplicate records over pbxID and dialupDT combination.Peter LarssonHelsingborg, Sweden |
 |
|
|
SQLJunior
Starting Member
34 Posts |
Posted - 2007-04-16 : 05:44:34
|
| Hi Peter,In my table, there will never be duplicate records over a pbxID and dialupDT combination, so i don't need to worry about that.Thanks! |
 |
|
|
harsh_athalye
Master Smack Fu Yak Hacker
5581 Posts |
Posted - 2007-04-16 : 05:46:44
|
quote: Originally posted by Peso Yes, and it is fast too with a large number of records.However, it don't handle possible duplicate records over pbxID and dialupDT combination.Peter LarssonHelsingborg, Sweden
That was not in the original requirements specs ! Harsh AthalyeIndia."The IMPOSSIBLE is often UNTRIED" |
 |
|
|
SwePeso
Patron Saint of Lost Yaks
30421 Posts |
Posted - 2007-04-16 : 05:48:52
|
Peter LarssonHelsingborg, Sweden |
 |
|
|
SQLJunior
Starting Member
34 Posts |
Posted - 2007-04-17 : 03:52:37
|
| i need to make a bit of a modifictaion to Harsh's query above.i now have another table called tblPBXif exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[tblPBX]') and OBJECTPROPERTY(id, N'IsUserTable') = 1)drop table [dbo].[tblPBX]GOCREATE TABLE [dbo].[tblPBX] ( [PBXID] [int] NOT NULL , [RegionalID] [int] NOT NULL , [CurrentPBXMode] [varchar] (5) COLLATE SQL_Latin1_General_CP1_CI_AS NULL) ON [PRIMARY]GOINSERT INTO tblPBX(PBXID,RegionalID, CurrentPBXMode )SELECT 83, 0 , '1' UNION ALLSELECT 543, 0 ,'0' UNION ALLSELECT 1012, 0 ,'1' UNION ALLSELECT 1219,0,'1' UNION ALLSELECT 16499,0, '0' Take note of the CurrentPBXMode Column, 0's represent commissioned sites. What my query now has to do is delete all entries from tblDialupLog where the pbxid is in tbPBXIDTemp except iftblPBX.CurrentPBXMode = 0 (for that particular pbxid), further more,if tblPBX.CurrentPBXMode = 0 then only the latest entry (according to tbldialupLog.DialupDT) must be kept. So my new expected result would be:83,'8/8/2006 8:58:11 AM' ,'T2 UQ 28924','0132493700',0 , '0800003554'1219, '8/8/2006 8:59:03 AM' ,'T3 ZD 02031', '0152958095', 0,'0800004249'543,'8/8/2007 8:55:44 AM','T0 UA 33902','0123623500',0,'0800003554'16499, '8/8/2006 8:51:28 AM', 'T2 ZD 32892', '0543375100', 0, '0800004249'N.B. both entries for pbxid 1012 are deleted because it's currentPBXMode <> 0. and only the topentry for 543 is left behind.pbxid 16499 CurrentPBXMode = 0, but it does not exist in tblPBXIDTemp.Regards, |
 |
|
|
SwePeso
Patron Saint of Lost Yaks
30421 Posts |
Posted - 2007-04-17 : 04:30:59
|
| Do you really expect us to do your work for you, twice, for free?Next time, please post ALL your problems at once.You can easily solve your problem with a LEFT JOIN to the new table.Peter LarssonHelsingborg, Sweden |
 |
|
|
SQLJunior
Starting Member
34 Posts |
Posted - 2007-04-18 : 04:52:50
|
| Yeah well thanks for that then. |
 |
|
|
SwePeso
Patron Saint of Lost Yaks
30421 Posts |
Posted - 2007-04-18 : 04:57:29
|
| You're welcome. And good luck.Peter LarssonHelsingborg, Sweden |
 |
|
|
|
|
|