Thanks for replying. If you can suggest a better and more efficient way of writing the query, I would greatly appreciate it. I shall try and explain what I am doing (and it is not particularly complex), so hopefully you can give me a better way of writing it:TablesThere are other tables, but for simplicity, I shall include only those that are relevant to the problem.CREATE TABLE [dbo].[Cycle] ( [CycleID] [smallint] IDENTITY (1, 1) NOT NULL , [CycleCode] [varchar] (12) COLLATE SQL_Latin1_General_CP1_CI_AS NOT NULL , [ClientCode] [varchar] (20) COLLATE SQL_Latin1_General_CP1_CI_AS NOT NULL , [GenerateSVD] [bit] NOT NULL , [GenerateTextJournal] [bit] NOT NULL ) ON [CPWEBilling_Reporting_data]GOCREATE TABLE [dbo].[CycleInstance] ( [CycleInstanceID] [bigint] IDENTITY (1, 1) NOT NULL , [CycleID] [smallint] NOT NULL , [CycleInstanceDate] [datetime] NOT NULL ) ON [CPWEBilling_Reporting_data]GOCREATE TABLE [dbo].[EBill] ( [EBillID] [bigint] IDENTITY (1, 1) NOT NULL , [CustomerID] [bigint] NOT NULL , [InvoiceNumber] [varchar] (12) COLLATE SQL_Latin1_General_CP1_CI_AS NOT NULL , [EmailAddress] [varchar] (50) COLLATE SQL_Latin1_General_CP1_CI_AS NOT NULL , [CycleInstanceID] [bigint] NOT NULL ) ON [CPWEBilling_Reporting_data]GOCREATE TABLE [dbo].[BounceBack] ( [BounceBackID] [bigint] IDENTITY (1, 1) NOT NULL , [EbillID] [bigint] NOT NULL , [BounceDate] [datetime] NOT NULL , [BounceReasonID] [int] NOT NULL ) ON [CPWEBilling_Reporting_data]GOCREATE TABLE [dbo].[ExtractBounceBackInstance] ( [ScheduleID] [bigint] NOT NULL , [StartDateTime] [datetime] NOT NULL ) ON [CPWEBilling_Reporting_data]ExtractBounceBackInstance does not have any relationships with any tables, it is used to record when the program is run against the database
Requirement:A Program is to be run against the database to extract all the EBills that have bounced and not reached the recipient within a certain timeframeIt pulls out the customer information relative to the EBill and inserts their details into either an SVD or TEXT fileThe Cycle table determines what format the customer details should be output in:CycleID CycleCode ClientCode GenerateSVD GenerateTextJournal ------- ------------ -------------------- ----------- ------------------- 0 CPW0113 CPW001BTC 0 125 CPW0105 CPW005VTM 0 133 CPF0107 CPW007FIX 1 041 CPW0101 CPW001BTC 0 1
Purpose of the queryGenerate a report to count the number of Ebills that have bounced (count each one only the one time) and have been inserted into either the SVD or Text journalThis is where the WHERE clause gets a bit tricky because the dates that I am using are from when the application was last run upto to the current run. The scheduleID increments for each time the app is run.ExampleExtractBounceBacksInstanceScheduleID StartDateTime1 1 Jan 20062 8 Jan 20063 15 Jan 2006The program is executed, and it inserts a datetime stamp into the StartDateTime column. In this example, we are currently executing under ScheduleID 3.StartDateTime = The dateTime of when the app was last run ( SELECT TOP 1 StartDateTime FROM ExtractBounceBackInstance WHERE ScheduleID < ( SELECT MAX(ScheduleID) FROM ExtractBounceBackInstance ) ORDER BY ScheduleID DESC )
SO in this scenario, the query would return ScheduleID 2EndDateTime = The DateTimeStamp inserted during the current execution of the program ( SELECT StartDateTime FROM ExtractBounceBackInstance WHERE ScheduleID = ( SELECT MAX(ScheduleID) FROM ExtractBounceBackInstance) )
Oh, and I know why it was throwing a wobbly - it didn't like the Order By clause if the nested select statement.Sooooo, any better ways of doing this? The output at the moment looks like this:EBillsBounced Count ------------ ---------------- SVD 4Text Journal 2
Hearty head pats