Please start any new threads on our new
site at https://forums.sqlteam.com. We've got lots of great SQL Server
experts to answer whatever question you can come up with.
Author |
Topic |
noblemfd
Starting Member
38 Posts |
Posted - 2014-05-16 : 05:59:49
|
I HAVE A TABLE AND WANT TO CREATE STORED PROCEDURE FROM IT. SEE THE TABLE SCRIPT BELOW.CREATE TABLE tblreconcile(DOCID NVARCHAR(50),RECONDATE DATETIME,TOTALDR MONEY,TOTALCR MONEY,CONTROL1 BIGINT IDENTITY(1,1))I WANT TO CREATE A STORED PROCEDURE CALLED spRECON FROM THIS TABLE. IT SHOULD GROUP IT BY DOCID & ORDER BY CONTROL1. IT SHOULD HAVE AN ADDITIONAL FIELD CALLED BEGDATE DATETIME. IF FIRST ROW IS SELECTED THE VALUE OF BEGDATE SHOULD BE 01/01/YEAR ELSE IT SHOULD BE VALUE OF RECONDATE FROM PREVIOUS ROW.THE FIELDS FOR THE STORED PROCEDURE ARE:- RECID,BEGDATE,RECONDATE,TOTALDR,TOTALCR,CONTROL1. THANKS |
|
tkizer
Almighty SQL Goddess
38200 Posts |
Posted - 2014-05-16 : 11:55:55
|
Show us what you have so far. With the description all in caps, I'm unable to read it.Tara KizerSQL Server MVP since 2007http://weblogs.sqlteam.com/tarad/ |
|
|
noblemfd
Starting Member
38 Posts |
Posted - 2014-05-22 : 02:51:33
|
quote: Originally posted by tkizer Show us what you have so far. With the description all in caps, I'm unable to read it.Tara KizerSQL Server MVP since 2007http://weblogs.sqlteam.com/tarad/
Am very sorry please. What I want to do is that I want to access previous row from the field "ReconDate" and put in a field call "BegDate". This is my initial table:CREATE TABLE tblreconcile(DocID NVARCHAR(50),ReconDate DATETIME,TotalDR MONEY,TotalCR MONEY,control1 BIGINT IDENTITY(1,1))DocID ReconDate TotalDR TotalCR control1001 07/01/2014 20,000 13,000 1002 15/01/2014 15,000 17,000 2001 03/02/2014 14,000 21,000 3001 03/03/2014 4,000 26,000 4002 07/03/2014 19,000 11,000 5001 03/04/2014 42,000 26,000 6002 13/04/2014 34,000 31,000 7EXPECTED RESULT:An extra column BegDate should be created, and everything should be sorted by DocID, ReconDate and control1See the Expected result below:DocID BegDate ReconDate TotalDR TotalCR control1001 NULL 07/01/2014 20,000 13,000 1001 07/01/2014 03/02/2014 14,000 21,000 3001 03/02/2014 03/03/2014 4,000 26,000 4001 03/03/2014 03/04/2014 42,000 26,000 6002 NULL 15/01/2014 15,000 17,000 2002 15/01/2014 07/03/2014 19,000 11,000 5002 07/03/2014 13/04/2014 34,000 31,000 7 Also where you have NULL in the BegDate should be replaced by the date - firstday/firstmonth/currentyear e.g 01/01/2014 |
|
|
|
|
|
|
|