Look Back Through Records To Apply Funds Appropriately

Dec 19, 2011

I have a table holding transaction data: money owed / money paid. The vast majority link together, however, a small % do not. I need to apply money paid to money owed. I want to give the client the benefit of the doubt, while using some logic in the application of funds.

I know when the transactions occurred, so I decided to work from newest to oldest, applying available funds from any money paid that is newer than the money owed. When the process completes the look back for an account, there may be money paid left over. This is okay, as it could apply to money owed transactions prior to the look back period.

I have code in place to accomplish this. My problem is the amount of time the process takes to run. At the current pace, it will take well over a day to complete. Here is an example of what the data may look like for one account:

Code:
TranDate TranType Amount
01/05/2008Owed $50.00
01/24/2008Paid$75.00
04/15/2008 Owed $43.75
04/28/2008Paid $50.00
05/23/2008Owed$43.75
08/19/2008Owed $35.20
02/10/2009Paid$14.15
03/16/2009Paid $9.50
06/14/2009Owed $63.45
09/24/2009Paid$40.00

Here is what I want to accomplish:

Code:
OwedTranDateTtlOwed PaidTranDatePaidTranAmtPaidAmtAppliedPaidAmtRemainingOwedAmtRemaining
06/14/2009$63.4509/24/2009$40.00 $40.00$0.00$23.45
08/19/2008$35.2003/16/2009$9.50$9.50$0.00$25.70
08/19/2008$35.2002/10/2009$14.15$14.15$0.00$21.05
05/23/2008$43.75$43.75
04/15/2008$43.7504/28/2008$50.00$43.75$6.25$0.00
01/05/2008$50.0004/28/2008$6.25$6.25$0.00$43.75
01/05/2008$43.7501/24/2008$75.00$43.75$31.25$0.00

Here is the code to accomplish the look back:

Code:
DECLARE
@AcctNumDECIMAL(10,0),
@TranDateDECIMAL(8,0),
@TranSeqDECIMAL(4,0),
@TranODINT,
@TranTypeVARCHAR(20),
@TranAmtDECIMAL(7,2),

[Code] .....

View 1 Replies


ADVERTISEMENT

Tables Not Clearing Appropriately...

Mar 5, 2007

Having issues with a table clearing out properly which is causing problems in the program.

By design there should only be one row in this table which updates with the most current record. However on this one system the table does not clear/update like it is supposed to, and instead starts adding multiple rows.

I have tried truncating the table but the problem does not resolve itself. Does anyone have any ideas as to why this might be happening?

Thanks!
Shawn

View 6 Replies View Related

SQL Server 2012 :: CROSS APPLY Returning Records From Left Recordset Even When No Matching Record In Right One

Oct 7, 2014

Following is the query that I'm running:

create table a (id int, name varchar(10));
create table b(id int, sal int);
insert into a values(1,'John'),(1,'ken'),(2,'paul');
insert into b values(1,400),(1,500);

select *
from a
cross apply( select max(sal) as sal from b where b.id = a.id)b;

Below is the result for the same:

idname sal
1John500
1ken500
2paulNULL

Now I'm not sure why the record with ID 2 is coming using CROSS APPLY, shouldn't it be avoided in case of CROSS APPLY and only displayed when using OUTER APPLY.

One thing that I noticed was that if you remove the Aggregate function MAX then the record with ID 2 is not shown in the output. I'm running this query on SQL Server 2012.

View 6 Replies View Related

CROSS APPLY Vs OUTER APPLY Example Messed Up?

Nov 27, 2007

Hi... I'm reading the MS Press 70-442 Self-Paced Training kit, and I'm having problems with this example.
I'd like help getting it to work correctly, or understand why it is isn't working the way I planned.

On page 67, the lab is about the APPLY operator (CROSS APPLY and OUTER APPLY). I first have to input a sample table-valued function into the AdventureWorks database:




Code Block
CREATE FUNCTION fnGetAvgCost(@ProdID int)
RETURNS @RetTable TABLE (AvgCost money)
AS
BEGIN
WITH Product(stdcost)
AS
(
SELECT avg(standardcost) as AvgCost
FROM Production.ProductCostHistory
WHERE ProductID = @ProdID
)
INSERT INTO @RetTable
SELECT * FROM Product
RETURN
END



and then run a sample T-SQL statement





Code Block
SELECT p.Name, p.ProductNumber,
Convert(varchar, cost.AvgCost,1) AS 'Average Cost'
FROM Production.Product p
CROSS APPLY fnGetAvgCost(p.ProductID) AS cost
WHERE cost.AvgCost IS NOT NULL
ORDER BY cost.AvgCost desc

My problem is with the WHERE clause... According to page 56, CROSS APPLY returns only rows from the outer table that produces a result set, so why do I need to explicitly filter NULL values?

When I remove the WHERE clause, the query retrieves lots of NULL AvgCost values.

Again, according to page 56, it is the OUTER APPLY that returns all rows that return a result set and will include NULL values in the columns that are returned from the table-valued function.

So, in short, I don't see the difference between CROSS APPLY and OUTER APPLY, using this example, when I remove the WHERE clause?

(Please refrain from introducing another example into this question.)

View 8 Replies View Related

Query Bringing Back Too Many Records

May 1, 2015

I'm written a query to pull adjustments made on customer accounts, and I am attempting to pull in the payment associated with the adjustments. I'm using a single account to test my query and somewhere in my final output select statement I'm doing something that is bringing me back more records than I want. The code I have written is:

IF OBJECT_ID('TEMPDB..#TMP1O') IS NOT NULL DROP TABLE #TMP1O

select a.batch
,a.acctcorp
,a.house
,a.cust
--,RIGHT (REPLICATE ('0',5) + CONVERT(VARCHAR(5),A.acctcorp),5) +

[Code] ....

I've tried making changes within my where statement and parameters, but so far I've come up emtpy.

View 9 Replies View Related

SQL Back - Access Front - Replication - Records 'jump'

May 12, 2007

Hi all :



I hope this is an easy one for some of you - this is the scenario :



1) I have an Access Application that has been upsized to SQL

2) We are now replicating it with a second office

3) When we started doing the Merge Replication, the ROWGUID was added to each table

4) When we attempt to add a new record to the table, it allows us to add 3 fields, then the record 'jumps' to the indexed location of the ROWGUID

5) The 'jump' is undesirable as we then have to resort the table by key and finish its input.

5) We have our own primary identity field that SQL seems to be ignoring.



Any help would be really great

View 4 Replies View Related

Transact SQL :: Insert Records And Write Back Identity Value

Jun 19, 2015

I'm trying to insert records into "holding" table and write back identity column value (Entry_Key) to the original table. So my setup is I have two tables; tblEWPBulk and tbleFormsUploadEWP. Users will enter records into tblEWPBulk and use BatchID to group records, once batch entry has been completed (usually less than 30 records) user will click on UploadAll button and insert records (not all fields) into tbleFormsUploadEWP. One record in tblEWPBulk can be sent multiple times to the holding table but tblEWPBulk will need to have latest Entry_Key captured. Records are sent from holding table to DB2 z/VSE using SQL stored procedure and based on certain logic records are marked uploaded or certain error capture... that part works fine.

So for example I want to send 

BatchID, AccountNumber, Period, ReceiveDate, AccountType, ReturnType, NetProfitOrLoss, TaxCredit FROM tblEWPBulk to the holding table and write back Entry_Key (identity column) back to the record in tblEWPBulk (field called UploadEntryKey). As I said one record could be sent to the holding table multiple times until uploaded or deleted and UploadEntryKey always needs to be updated so that when results are processed response from the DB2 can be inserted into table and presented to the user.

No foreign key relationship exists since records in the holding table get sent to the archive table and table is truncated and entry_key starting value reset back to 2000... just some DB2 restrictions. 

View 5 Replies View Related

Transact SQL :: Undo Update And Bring Back Records To Their Previous Values

Aug 7, 2015

I have a table with 1 million records. I want to update only 400 records. The update statement is provided by a 3rd party vendor. Once i run the update statement it will update all the 400 records. Once the table is updated the users will validate the table

if the update is successful or not. What i'm looking for is:

1) Is there a way to identify what records were updated.
2) If the update done is not what the users wanted i need to undo and bring back the 400 records to their previous values.

I'm on sql server 2008.

View 34 Replies View Related

Having Difficulty Setting Back Up To Back Up File Wihout Datetime Stamp SQL 2K

Apr 24, 2007

Hello,I'm trying to create a simple back up in the SQL Maintenance Plan that willmake a single back up copy of all database every night at 10 pm. I'd likethe previous nights file to be overwritten, so there will be only a singleback up file for each database (tape back up runs every night, so each daysback up will be saved on tape).Every night the maintenance plan makes a back up of all the databases to anew file with a datetime stamp, meaning the previous nights file stillexists. Even when I check "Remove files older than 22 hours" the previousnights file still exists. Is there any way to create a back up file withoutthe date time stamp so it overwrites the previous nights file?Thanks!Rick

View 5 Replies View Related

Mirroring :: Principal Database Get Role Back After Being Back On Line

May 14, 2015

New to Database Mirroring and I have a question about the Principal database server. I have a Database Mirroring setup configured for High-safety with automatic fail over mode using a witness.

When a fail over occurs because of a lost of communication between the principal and mirror, the mirror server takes on the roll of Principal. When communication is returned to the Principal server, at some point does the database that was the previous Principal database automatically go back to being the Principal server?

View 2 Replies View Related

Reporting Services :: Run Two Reports Back To Back Without Page Eject?

Jun 9, 2015

I need to run two reports each of A5 Size to run back to page and print on single A4 paper means in 1st half Sale bill will be printed and in second half Gate Pass Will Be Printed both report will be on same page and size and shape should be maintained. How to do it.

View 4 Replies View Related

How Do I Back-up &> Truncate &> Shrink &> Back-up SQL 2000

Jul 20, 2005

Hello,I am hoping you can help me with the following problem; I need to process the following steps every couple of hours in order to keep our Sql 2000 database a small as possible (the transaction log is 5x bigger than the db).1.back-up the entire database2.truncate the log3.shrink the log4.back-up once again.As you may have determined, I am relatively new to managing a sql server database and while I have found multiple articles online about the topics I need to accomplish, I cannot find any actual examples that explain where I input the coded used to accomplish the above-mentioned steps. I do understand the theory behind the steps I just do not know how to accomplish them!If you know of a well-documented tutorial, please point me in the right direction.Regards.

View 2 Replies View Related

Rolling Back SQL Server 2005 Databases Back To SQL Server 2000

Sep 22, 2006

Does anybody know of a way to rollback SQL Server 2005
databases back to SQL Server 2000? Is there a way of doing it without
resorting to Copy Database Wizard? I love to find a way of attaching a SS 2005 database
to a SS 2000 instance without any issues.



I recently upgraded to SS 2005 and I am very unhappy with the SS 2005 and I
want to rollback to SS 2000, which was a lot more stable. I am having
several major issues that are affecting my whole company's day-to-day
operations and the managers are not happy. Some of the issues include
night time batch running very sluggish for no apparent reason. This is a
biggest problem because it only occurs once or so a week and causes a disturbance
with the daily activities when the night time processing isn€™t completed on
time. The rest of the time, the batch processing runs great, even a little better then on SS 2000. I
don't believe it is a matter of my application needing to be retuned because if
that was the case, then why isn't it running sluggish every night? Also,
it's never the same day that the sluggish behavior occurs. If it was occurring
on the same night, then I would have something to investigate within our
application, but it doesn't. Another issue that I am having involves a
night time job that restores a copy of the production database to the Data
Warehouse server to be used for updating the data warehouse. Again, most
of the time it runs great (~2 1/2 hours), but once or twice a week, it goes
stupid and takes 6 1/2 hours for no apparent reason. Again, it is not happening
the same day either, which could give me something to invesigate. On SS 2000, this same job ran flawlessly. Never I did I run into situation that the
database restoration took that long to run. Even another issue involves a SQL Server Agent Job that was put into suspended
state. What's a suspended state and how can I get it out of suspended
state? I can find no information about suspended state in BOL. I
did a Google and nothing came up. If this suspended state was put
in for security reasons, great, but then tell me how I can remove the suspended
state. I am also not happy with the
fact that I can't get accurate information about the queries that are actively
running at that particular moment. In SS 2000, when I noticed high CPU
usage on the server, I would run the sp_who2 active stored proc and it would
show me all the active thread and how much CPU it was consuming. I would
then find the running threads with the highest CPU numbers and investigate the
query and see if we could improve it. Now in SS 2005, I get in the same
situation and run the sp_who2 stored proc, and there is no smoking gun.
All of the active threads are showing very little CPU usage, which I am very
suspect of. What the heck happen to sp_who2? I looked at some of
the other ways of looking at running processes (i.e... sys.sysprocesses) and
they don't appear to be giving the information that I need.



I am very unhappy and I just want to roll back to SS 2000 and wait a couple of
years before I upgrade to SS 2005.


Dave Brown

View 1 Replies View Related

SQL 2012 :: Query To Make Single Records From Multiple Records Based On Different Fields Of Different Records?

Mar 20, 2014

writing the query for the following, I need to collapse the continuity. If the termdate for an ID is one day less than the effdate of the next id (for the same ID) i need to collapse the records. See below example .....how should i write the query which will give me the desired output. i.e., get min(effdate) and max(termdate) if termdate is one day less than the effdate of next record.

ID effdate termdate
556868 1999-01-01 1999-06-30
556868 1999-07-01 1999-10-31
556869 2002-10-01 2004-01-31
556872 1999-02-01 2000-08-31
556872 2000-11-01 2004-01-31
556872 2004-02-01 2004-02-29

output should be ......

ID effdate termdate
556868 1999-01-01 1999-10-31
556869 2002-10-01 2004-01-31
556872 1999-02-01 2000-08-31
556872 2000-11-01 2004-02-29

View 0 Replies View Related

How To Apply Hot Fix?

Mar 26, 2007

I have a sql server problem for which the solution is at,

http://support.microsoft.com/kb/889521

The article talks about a hot fix, but there is no link for download the hot fix.

Can anyone have a look and guide me.

Thanks



------------------------
I think, therefore I am - Rene Descartes

View 4 Replies View Related

Does SETROWCOUNT 10 Apply To Only The Sp In Which It Used Or Outside It Also?

May 23, 2007

I am using SETROWCOUNT 10 in my stored procedure. At end of thi sp I use SETROWCOUNT 0.
Will all other sp's that are executing at the same time as the above sp, get affected by the above SETROWCOUNT statement?

View 1 Replies View Related

Using Like And Apply To 3 Columns

Sep 7, 2007

 I have a table called Courses with columns "courseCode" "courseName" and "TeacherName"Im passing in a value @prefixText ...I have the following...Select courseModuleCode from courses Where courseModuleCode like @prefixTextwhat I actually want to do is return one list, i.e. one column that will do the same for  "courseName" and "TeacherName" aswell based on what the @prefix tag is...so i will end up with one column that may contains some or all of the following courseModuleCode, courseName, TeacherName. Im basically implementing a search on this table and want the users to search by any of the column nameshope this makes sense.   

View 6 Replies View Related

DTS Globalvariable Not Apply ......

Jun 24, 2004

I am new to DTS.
In ExecuteSQLTask:
want the original sql statement of <<backup database databaeABC to backup_databaseABC with init>> to be

backup database ? to 'backup_' + ? with init

But it looks the parameter could not place a globalVariable to it and parse it with error.
One alternate way probably need to create a proc to pass a parm to it....

Also, how could pass a globalVariable to a FTP task?

thanks
-D

View 4 Replies View Related

Best Way To Apply SP3 In Replication

Aug 27, 2004

We have a Pub, Dist and Sub Server all running under SP2 with worm update and are looking to apply SP3a . Replication is transactional with heavy usage . What is the best sequence to apply SP3

can it be spread out in 3 weekends or has to be done all together ?



Thanks

View 5 Replies View Related

Apply Lock On A Row?

Sep 12, 2013

I want to apply lock on a row, where by when another application is trying to access the locked row the application will automatically move to the next row instead of waiting for the locked row

View 3 Replies View Related

Sp3a Does Not Seem To Apply

Feb 26, 2004

I am trying to install sql server sp3a. I checked the version before and after the sp is applied, and it shows:

8.00.194

Now, according to the sp3a readme:

http://support.microsoft.com/default.aspx?scid=/support/servicepacks/SQL/2000/SP3ReadMe.asp

the following versions should show:

SQL Server 2000 Original Release 8.00.194 RTM
Database Components SP1 or Desktop Engine SP1 8.00.384 SP1
Database Components SP2 or Desktop Engine SP2 8.00.534 SP2
Database Components SP3 or SP3a, or Desktop Engine SP3 or SP3a 8.00.760 SP3

This would imply that after installing sp3a and restarting, the version reported should now be 8.00.760. However, it still shows 8.00.194. So it appears to have not really installed, althought the sp3a installation ran without incident. So, how can I get the sp3a to not just act like it installed, but actually get it to install? Also, I have shut down all mssql services and rebooted, applied patch, and rebooted again. Doesn't help.
Also, if it is really installing, is there another way to tell?

Anyway, I really need to be able to install sp3a to see if it fixes another problem I'm having. Unfortunetly, since I can't even tell if the sp3a is installing, I can't tell if it really doesn't help or not. Thanks.

Tony F

View 2 Replies View Related

Cross Apply

May 22, 2008

What is Cross Apply, when it will be used ?

View 2 Replies View Related

Where To Apply My SQL Knowledge...

Jul 22, 2007

Hello everyone...
i am new to Structured Query Language. I am learning it for the past 6 months form a book by myself. I don't have a teacher available to me. Now when i have finished that with all of the exercises completed. i want to make sure what i've learned is i've really learned it. the problem is that there is no such organization here that i join and have some hand on practical. So now i am asking u guys to help me. Tell me some way so that i make sure of whatever knowledge i have about SQL.

don't worry be happy

View 1 Replies View Related

Which Hotfix To Apply?

Oct 14, 2007

We would like to apply a hotfix to solve the issue that brought our server down. The hotfix we are trying to apply is in Build 3175. But the latest Cumulative Update 3 Package is Build 3186. Should we apply 3175? Or we can go for the latest build 3186? Which is recommended?

View 6 Replies View Related

Apply Top &&amp; Group By

Nov 20, 2007

I want to show top selling items for each store. Sample SQL Query is "SELECT TOP n ItemNumber FROM Inventory Group By Store" n - is the dynamic value. I am accepting this n parameter from the user. If the user is giving 5 for n then, I want to show top 5 selling items for each store. It will be greatfull, If anybodies giving me an idea of how to do this?.

Thanks

View 5 Replies View Related

Apply SP2 After Applying CTP?

Feb 20, 2007

Should I apply SP2 even though I have already installed the community preview?

View 1 Replies View Related

How Sql Connection Apply With Class

Apr 17, 2008

HI,
I am going to make a big web application. so im going to use sql connection with class for example i have 100 web pages and i will make a slq connection in class.vb
Class1.vb(see the below code)
Imports System.Data
Imports System.Data.SqlClient
Imports Microsoft.VisualBasicPublic Class Class1
Private conn As New SqlConnection("Data Source=DANISHSQLEXPRESS;Initial Catalog=ARBASHHR.MDF;Integrated Security=True")Private cmd As New SqlCommand()
Private da As New SqlDataAdapter(cmd)
End Class
 default.aspx(here is asp.net page i want to use the sql connection from that class.)
but this below page saying that Name "cmd" is not declared and connection
Try
cmd.Connection = conn
conn.Open()Dim ds As New DataSet
cmd.CommandText = "SELECT * FROM Users where (UserName = N'" & TextBox1.Text & "')" 'And (Password = N '" & TextBox2.Text & "')"da.Fill(ds, "data")
conn.Close()
 For Each dt As DataTable In ds.Tables
For Each dr As DataRow In dt.RowsTextBox3.Text = dr.Item("Password")
Next drNext dt
If TextBox2.Text = TextBox3.Text ThenResponse.Redirect("Welcome.aspx")
Else
Label3.Text = "Invalid Username/password"
End IfCatch ex As Exception
' MsgBox(ex.Message) '("Error Loading Information From Database..", MsgBoxStyle.Critical, "Error")
End Try
End Sub
please let me know where im doing mistake.....
Thank in Advance
 
 

View 2 Replies View Related

Permissions / Apply Button

Aug 20, 1999

Is anyone else having trouble with the Apply button under the permissions tab?
I can change permissions, click apply, the changes are shown, but when I access the database, the changes arn't made. If I close the permissions windows and re open the changes arn't made.

Will SP1 fix this?(it's not listed in the fix text file)

View 2 Replies View Related

Apply Your Thought On Char To Int

Feb 7, 2004

HI FRIENDS,

IS THERE ANY PERFORMANCE IMPACT WHEN I USE "CHAR" AS A DATA TYPE INSTEAD OF USING "INT" FOR RETRIEVING DATA OR FOR SOME COMPLEX QUERY.

THANKS
WITH BEST REGARDS,
DHIRAJ

View 3 Replies View Related

Cross Apply And Newid

Apr 28, 2008

Hi,

Why am I getting a different numbers of distinct ids in those queries?


USE AdventureWorks
go
Declare @myXml as xml
set @myXml = '
<lol>omg</lol>
<lol>rofl</lol>
';

select locations.*, T.c.value('.','nvarchar(max)') from
(
select newid() as Id
from Production.ProductModel
where ProductModelID in (7, 8)
) as locations cross apply @myXml.nodes('(/lol)') T(c);

select mytable.* , T.c.value('.','nvarchar(max)') from
(
select newid() as Id
union
select newid()
) as mytable cross apply @myXml.nodes('(/lol)') T(c);


Thanks,

Victor

View 8 Replies View Related

CROSS APPLY Equivalent

Apr 29, 2008

I have a question, is there any equivalent for the CROSS APPLY operator in SQL server 2000?



I have the following code in SQL Server 2005 and it works fine, but I need an equivalent code in SQL server 2000.





SELECT *

FROM Customers Cust CROSS APPLY dbo.GetAccountAttributes(Cust.AccountNo) Att





what I need is to join a function and passing it a dynamic parameter.



I need it urgently





Thanks in advance,

Imad Elayyan

View 1 Replies View Related

How I Apply Unique Constraint

Mar 2, 2007

how can i apply unique cosntraint in an existing table's column

i want to do it like this as below

sql server enterprise manager ==> DB==>table (R.click)==>design table

View 1 Replies View Related

How To Apply Complex Constraints

Jul 23, 2005

Hi all!I want to create a constraint that uses data from other tables,specifically i want to make sure that a varchar has exactly the lengthspecified in an integer-column in a table that I pointed out with aforeign key.I would like this to be solved something like this:create table string_size_limits(row_id INTEGER PRIMARY KEYstring_size INTEGER)create table strings(string_size_limit_row_id INTEGERREFERENCES string_size_limits(row_id)string varcher(50)CONSTRAINT check_string_size CHECK ???)Is it possible to solve this problem without SP using above model?Is it possible to solve this problem with SP using above model?Must the above problem be solved using triggers?Any help appreciated

View 4 Replies View Related







Copyrights 2005-15 www.BigResource.com, All rights reserved