February 14, 2010 at 11:37 pm
All,
I am not very good with T-SQL, although I am learning. I have a query that I am trying to write but it is kickin my butt.
Process Participants looking for terminations. Terminations are defined by 3 consecutive absences
or exceeding MAX absences for Program.
An absence is recorded by adding a NoShow Fee record to the Ledger Table.
I know that there has to be a way to do most if not all of this in SQL and just return a table to the report. Any guidence would be GREATLY appreciated.
USE [ClientManager]
GO
/****** Object: StoredProcedure [dbo].[usp_GetAllNoShows] Script Date: 02/14/2010 22:05:59 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
-- =============================================
-- Author:--------------
-- Create date: 2/11/2010
-- Description:Get all No Show Records for the Participant
-- =============================================
ALTER PROCEDURE [dbo].[usp_GetAllNoShows]
AS
BEGIN
SET NOCOUNT ON;
SELECT TransDate,l.FeeID, p.PartFName + ' ' + p.PartLName as FullName, l.PartID
FROM ledger l
INNER JOIN fees f on f.feeid = l.feeid
INNER JOIN Participants p on l.PartID = p.PartID
WHERE l.feeid = '1'
Order by l.PartID, TransDate
END
-----DDL-----
USE [ClientManager]
GO
/****** Object: Table [dbo].[Ledger] Script Date: 02/14/2010 22:32:23 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE TABLE [dbo].[Ledger](
[TransID] [int] IDENTITY(1,1) NOT NULL,
[TransDate] [datetime] NULL,
[CR] [money] NULL,
[DB] [money] NULL,
[ReasonCodeID] [int] NULL,
[PartID] [int] NULL,
[CaseID] [int] NULL,
[ReceiptNumber] [nvarchar](15) NULL,
[Description] [nvarchar](50) NULL,
[FeeID] [int] NULL,
CONSTRAINT [PK_Ledger] PRIMARY KEY CLUSTERED
(
[TransID] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY]
USE [ClientManager]
GO
/****** Object: Table [dbo].[Participants] Script Date: 02/14/2010 22:33:24 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
SET ANSI_PADDING ON
GO
CREATE TABLE [dbo].[Participants](
[PartID] [int] IDENTITY(1,1) NOT NULL,
[DateEntered] [datetime] NULL,
[PartDLNum] [nvarchar](15) NULL,
[PartLName] [nvarchar](30) NULL,
[PartFName] [nvarchar](30) NULL,
[PartAddr] [nvarchar](30) NULL,
[PartCity] [nvarchar](30) NULL,
[PartState] [nvarchar](2) NULL,
[PartZip] [nvarchar](5) NULL,
[PartPhone] [nvarchar](14) NULL,
[PartDOB] [datetime] NULL,
[Ethnicity] [int] NULL,
[AgeCategory] [varchar](10) NULL,
[CourtCountyID] [int] NULL,
[ClassTime] [nvarchar](50) NULL,
[Counselor] [int] NULL,
[Gender] [nvarchar](1) NULL,
[ClassDay] [int] NULL,
[PartEmail] [nvarchar](50) NULL,
[ClassID] [int] NULL,
[PaymentScheduleID] [int] NULL,
[MInitial] [nvarchar](1) NULL,
CONSTRAINT [PK_Participant] PRIMARY KEY CLUSTERED
(
[PartID] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY]
GO
SET ANSI_PADDING OFF
USE [ClientManager]
GO
/****** Object: Table [dbo].[Fees] Script Date: 02/14/2010 22:33:46 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE TABLE [dbo].[Fees](
[FeeID] [int] IDENTITY(1,1) NOT NULL,
[FeeDesc] [nvarchar](50) NOT NULL,
[FeeAmount] [money] NOT NULL,
CONSTRAINT [PK_Fees] PRIMARY KEY CLUSTERED
(
[FeeID] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY]
February 14, 2010 at 11:57 pm
Can you give us some sample data (as Insert statements, please), and expected output from that data? That would help a lot.
[font="Times New Roman"]-- RBarryYoung[/font], [font="Times New Roman"] (302)375-0451[/font] blog: MovingSQL.com, Twitter: @RBarryYoung[font="Arial Black"]
Proactive Performance Solutions, Inc. [/font][font="Verdana"] "Performance is our middle name."[/font]
February 15, 2010 at 9:22 am
All Fixed. Sorry and here you go.
INSERT INTO [ClientManager].[dbo].[Fees]
([FeeDesc],[FeeAmount])
VALUES
('No Show',15.00)
INSERT INTO [ClientManager].[dbo].[Ledger]
([TransDate],[CR],[DB],[ReasonCodeID],[PartID],[CaseID],[ReceiptNumber],[Description],[FeeID])
VALUES
('1/1/2010 7:08:01 AM','0.00','15.00','1','1','1','001','No Show Fee','1')
INSERT INTO [ClientManager].[dbo].[Ledger]
([TransDate],[CR],[DB],[ReasonCodeID],[PartID],[CaseID],[ReceiptNumber],[Description],[FeeID])
VALUES
('1/8/2010 7:08:01 AM','0.00','15.00','1','1','1','001','No Show Fee','1')
INSERT INTO [ClientManager].[dbo].[Ledger]
([TransDate],[CR],[DB],[ReasonCodeID],[PartID],[CaseID],[ReceiptNumber],[Description],[FeeID])
VALUES
('1/15/2010 7:08:01 AM','0.00','15.00','1','1','1','001','No Show Fee','1')
INSERT INTO [ClientManager].[dbo].[Ledger]
([TransDate],[CR],[DB],[ReasonCodeID],[PartID],[CaseID],[ReceiptNumber],[Description],[FeeID])
VALUES
('1/29/2010 7:08:01 AM','0.00','15.00','1','1','1','001','No Show Fee','1')
INSERT INTO [ClientManager].[dbo].[Ledger]
([TransDate],[CR],[DB],[ReasonCodeID],[PartID],[CaseID],[ReceiptNumber],[Description],[FeeID])
VALUES
('1/1/2010 7:08:01 AM','0.00','15.00','1','2','1','001','No Show Fee','1')
INSERT INTO [ClientManager].[dbo].[Ledger]
([TransDate],[CR],[DB],[ReasonCodeID],[PartID],[CaseID],[ReceiptNumber],[Description],[FeeID])
VALUES
('1/15/2010 7:08:01 AM','0.00','15.00','1','2','1','001','No Show Fee','1')
INSERT INTO [ClientManager].[dbo].[Ledger]
([TransDate],[CR],[DB],[ReasonCodeID],[PartID],[CaseID],[ReceiptNumber],[Description],[FeeID])
VALUES
('1/29/2010 7:08:01 AM','0.00','15.00','1','2','1','001','No Show Fee','1')
INSERT INTO [ClientManager].[dbo].[Participants]
([DateEntered],[PartDLNum],[PartLName],[PartFName]
,[PartAddr],[PartCity],[PartState],[PartZip]
,[PartPhone],[PartDOB],[Ethnicity],[AgeCategory]
,[CourtCountyID],[ClassTime],[Counselor],[Gender]
,[ClassDay],[PartEmail],[ClassID],[PaymentScheduleID]
,[MInitial])
VALUES
('1/1/2010 7:08:01 AM','C1235544','Brown','John','123 Any Street','Yourtown'
,'CA','92395','7609998888','04/17/1964','1','1','1','02:30','1','M','3'
,'john@seitmc.com','1','1','D')
INSERT INTO [ClientManager].[dbo].[Participants]
([DateEntered],[PartDLNum],[PartLName],[PartFName]
,[PartAddr],[PartCity],[PartState],[PartZip]
,[PartPhone],[PartDOB],[Ethnicity],[AgeCategory]
,[CourtCountyID],[ClassTime],[Counselor],[Gender]
,[ClassDay],[PartEmail],[ClassID],[PaymentScheduleID]
,[MInitial])
VALUES
('1/1/2010 7:08:01 AM','C1235544','Brown','Sue','123 Any Street','Yourtown'
,'CA','92395','7609998888','04/17/1964','1','1','1','02:30','1','F','3'
,'john@seitmc.com','1','1','D')
PartID = 1 should get a termination record
PartID = 2 should not
Output should be
PartID, CaseID, Total Number of Absences, TotalConsecutiveAbsences if >= 3 and a list of all absences that qualified.
Thanks for the help.
February 15, 2010 at 5:43 pm
john-902052 (2/15/2010)
...PartID = 1 should get a termination record
PartID = 2 should not
I don't understand this. PartID 1 has 4 absences in a row and PartID has 3 in a row. Shouldn't they both get terminations?
Output should be
PartID, CaseID, Total Number of Absences, TotalConsecutiveAbsences if >= 3 and a list of all absences that qualified.
We need you to show us exactly what the output should be from your test/sample data.
[font="Times New Roman"]-- RBarryYoung[/font], [font="Times New Roman"] (302)375-0451[/font] blog: MovingSQL.com, Twitter: @RBarryYoung[font="Arial Black"]
Proactive Performance Solutions, Inc. [/font][font="Verdana"] "Performance is our middle name."[/font]
February 15, 2010 at 6:19 pm
No, Part 2 has 3 absences but they are not consecutive.
I should see the rows from Part 1 as the exact output.
February 15, 2010 at 6:29 pm
john-902052 (2/15/2010)
No, Part 2 has 3 absences but they are not consecutive.
What constitutes "consecutive"? There are no other intervening fees.
[font="Times New Roman"]-- RBarryYoung[/font], [font="Times New Roman"] (302)375-0451[/font] blog: MovingSQL.com, Twitter: @RBarryYoung[font="Arial Black"]
Proactive Performance Solutions, Inc. [/font][font="Verdana"] "Performance is our middle name."[/font]
February 15, 2010 at 7:07 pm
john-902052 (2/15/2010)
Sorry and here you go.
INSERT INTO [ClientManager].[dbo].[Fees]
([FeeDesc],[FeeAmount])
VALUES
('No Show',15.00)
INSERT INTO [ClientManager].[dbo].[Ledger]
([TransDate],[CR],[DB],[ReasonCodeID],[PartID],[CaseID],[ReceiptNumber],[Description],[FeeID])
VALUES
('1/1/2010 7:08:01 AM','0.00','15.00','1','1','1','001','No Show Fee','1')
('1/8/2010 7:08:01 AM','0.00','15.00','1','1','1','001','No Show Fee','1')
('1/15/2010 7:08:01 AM','0.00','15.00','1','1','1','001','No Show Fee','1')
('1/29/2010 7:08:01 AM','0.00','15.00','1','1','1','001','No Show Fee','1')
('1/1/2010 7:08:01 AM','0.00','15.00','1','2','1','001','No Show Fee','1')
('1/15/2010 7:08:01 AM','0.00','15.00','1','2','1','001','No Show Fee','1')
('1/29/2010 7:08:01 AM','0.00','15.00','1','2','1','001','No Show Fee','1')
INSERT INTO [ClientManager].[dbo].[Participants]
([DateEntered]
,[PartDLNum]
,[PartLName]
,[PartFName]
,[PartAddr]
,[PartCity]
,[PartState]
,[PartZip]
,[PartPhone]
,[PartDOB]
,[Ethnicity]
,[AgeCategory]
,[CourtCountyID]
,[ClassTime]
,[Counselor]
,[Gender]
,[ClassDay]
,[PartEmail]
,[ClassID]
,[PaymentScheduleID]
,[MInitial])
VALUES
('1/1/2010 7:08:01 AM','C1235544','Brown','John','123 Any Street','Yourtown'
,'CA','92395','7609998888','04/17/1964','1','1','1','02:30','1','M','3'
,'john@seitmc.com','1','1','D')
('1/1/2010 7:08:01 AM','C1235544','Brown','Sue','123 Any Street','Yourtown'
,'CA','92395','7609998888','04/17/1964','1','1','1','02:30','1','F','3'
,'john@seitmc.com','1','1','D')
PartID = 1 should get a termination record
PartID = 2 should not
Output should be
PartID, CaseID, Total Number of Absences, TotalConsecutiveAbsences if >= 3 and a list of all absences that qualified.
Thanks for the help.
Heh... 2k8 code posted on a 2k5 forum. Just a tip... Lots of folks just won't take the time to rework the code when something like that is done. And, a lot of folks who only have 2k5 can still help with a lot of 2k8 problems if you give them the chance.
--Jeff Moden
Change is inevitable... Change for the better is not.
February 15, 2010 at 9:14 pm
Not sure what you are talking about. I am using 2005 not 2008 SQL Server.
February 15, 2010 at 9:15 pm
Consecutive <= less than 7 days apart
February 16, 2010 at 6:03 am
john-902052 (2/15/2010)
Not sure what you are talking about. I am using 2005 not 2008 SQL Server.
Hmmm... sorry about that, John. How did you get the INSERT/VALUES statement to work like that in 2k5? It gives me an error. I'm using 2k5 sp3... no CU's.
--Jeff Moden
Change is inevitable... Change for the better is not.
February 16, 2010 at 6:52 am
Jeff Moden (2/16/2010)
john-902052 (2/15/2010)
Not sure what you are talking about. I am using 2005 not 2008 SQL Server.Hmmm... sorry about that, John. How did you get the INSERT/VALUES statement to work like that in 2k5? It gives me an error. I'm using 2k5 sp3... no CU's.
The posted sample data code doesn't work on any version of SQL Server.
It would run on 2008 if commas were added after each set in the VALUES clauses...;-)
Paul
Paul White
SQLPerformance.com
SQLkiwi blog
@SQL_Kiwi
February 16, 2010 at 7:40 am
Ahhh, I just added the subesquent data rows. I guess my assumption was a little of cut and paste action. I can fix it?
February 16, 2010 at 7:52 am
john-902052 (2/16/2010)
Ahhh, I just added the subesquent data rows. I guess my assumption was a little of cut and paste action. I can fix it?
Sure, just hit the edit button next to your post. I'm sure it'd be appreciated - especially if 2005 syntax was used 😀
Paul White
SQLPerformance.com
SQLkiwi blog
@SQL_Kiwi
February 16, 2010 at 7:58 am
All Fixed.
Viewing 14 posts - 1 through 13 (of 13 total)
You must be logged in to reply to this topic. Login to reply