I have created a stored procedure for bulk invoice generation. On execution of the stored procedure, the values are successfully returned for all the columns except FromDate and ToDate. For these 2 columns, NULL values are displaying.
This here is the procedure that I have created. Please let me know what I am doing wrong here?
SET #fromDt = CONVERT(VARCHAR, DATEADD(d, -(DAY(DATEADD(m, -1, #currentdate - 2))), DATEADD(m, -1, #currentdate - 1)), 106)
ALTER PROCEDURE Bulkinvoicegeneration_lko
#InvoiceMonth VARCHAR(20),
#PrjId BIGINT,
#CustomerId NVARCHAR(50)
AS
BEGIN
SET NOCOUNT ON;
DECLARE #fromDt DATETIME,
#monthdays INT,
#ToDate DATETIME,
#Currentdate DATETIME,
#Year INT
SET #currentdate = CONVERT(VARCHAR, #Year) + '-'
+ CONVERT(VARCHAR, #InvoiceMonth) + '-11'
SET #fromDt = CONVERT(VARCHAR, DATEADD(d, - (DAY(DATEADD(m, -1, #currentdate - 2))), DATEADD(m, -1, #currentdate - 1)), 106)
SET #ToDate = CONVERT(DATE, Dateadd(d, -(Day(#currentdate)), #currentdate))
SET #monthdays = DATEDIFF(d, #fromDt, #ToDate) + 1
SELECT *
INTO #consumerdata1
FROM
(SELECT
id, prjid, customerid, ucccatid, area, rate,
billingamount AS TotalAmt,
billingamount AS SubTotalAmt
FROM
consumermst_lko
WHERE
1 = 2) AS aa
BEGIN
INSERT INTO #consumerdata1
SELECT
prjid, customerid, ucccatid, area, rate,
billingamount AS TotalAmt,
billingamount AS SubTotalAmt
FROM
consumermst_lko
END
ALTER TABLE #consumerdata1
ADD invoiceno VARCHAR(20), invoicedate DATE, duedate DATE,
balanceamt NUMERIC(10, 2), roundoff NUMERIC(10, 2),
entrydate DATE, fromdate DATE, todate DATE,
userid INT, deleteflag INT, responsecode INT,
responsemessage VARCHAR(500), invoiceno_response VARCHAR(500)
UPDATE #consumerdata1
SET entrydate = Getdate()
UPDATE #consumerdata1
SET userid = 2
UPDATE #consumerdata1
SET balanceamt = 20
--update #CONSUMERDATA set billingdays = 30
UPDATE #consumerdata1
SET invoiceno = NULL,
invoicedate = CONVERT(DATE, GETDATE() - 1),
duedate = DATEADD(d, 15, CONVERT(DATE, GETDATE())),
roundoff = 0,
deleteflag = 0
DECLARE #id BIGINT,
#InvoiceNo NVARCHAR(150),
#InvoiceDate DATE,
#DueDate DATE,
#UccCatId BIGINT,
#rate NUMERIC(10, 2),
#Area NUMERIC(10, 2),
#TotalAmt NUMERIC(10, 2),
#BalanceAmt NUMERIC(10, 2),
#SubTotalAmt NUMERIC(10, 2),
#RoundOff NUMERIC(10, 2),
#EntryDate DATE,
#FromDate DATE,
#UserId BIGINT,
#deleteflag INT,
#RESPONSECODE INT,
#RESPONSEMESSAGE VARCHAR(255),
#INVOICENO_RESPONSE VARCHAR(20)
SET #prjid = 2
DECLARE consmr_cursor1 CURSOR FOR
SELECT
id, prjid, customerid, ucccatid, area, rate,
totalamt, subtotalamt, invoiceno, invoicedate, duedate,
balanceamt, roundoff, entrydate, fromdate, todate,
userid, deleteflag, responsecode, responsemessage,
invoiceno_response
FROM
#consumerdata1
OPEN consmr_cursor1
FETCH NEXT FROM consmr_cursor1
INTO #Id, #PrjId, #CustomerId, #UccCatId, #Area, #rate, #TotalAmt, #SubTotalAmt, #InvoiceNo, #InvoiceDate, #DueDate,
#BalanceAmt, #RoundOff, #EntryDate, #FromDate, #ToDate, #UserId, #deleteflag,
#ResponseCode, #ResponseMessage, #INVOICENO_RESPONSE
WHILE ##FETCH_STATUS = 0
BEGIN
EXEC [dbo].[Demoinvoice]
#Id = #Id,
#InvoiceNo =#InvoiceNo,
#PrjId = #PrjId,
#CustomerId = #CustomerId,
#InvoiceDate = #InvoiceDate,
#InvoiceMonth = #InvoiceMonth,
#DueDate = #DueDate,
#UccCatId = #UccCatId,
#Rate = #Rate,
#Quantity = #Area,
#TotalAmt = #TotalAmt,
#BalanceAMT = #BalanceAmt,
#SubTotalAmt = #SubtotalAmt,
#RoundOff = #RoundOff,
#EntryDate = #EntryDate,
#FromDate = #FromDate,
#ToDate = #ToDate,
#UserId = #UserId,
#deleteflag = #deleteflag,
#RESPONSECODE = #RESPONSECODE,
#RESPONSEMESSAGE = #RESPONSEMESSAGE output,
#INVOICENO_RESPONSE = #INVOICENO_RESPONSE output
UPDATE #consumerdata1
SET responsecode = #RESPONSECODE,
responsemessage = #RESPONSEMESSAGE,
invoiceno_response = #INVOICENO_RESPONSE
WHERE customerid = #CustomerId
FETCH NEXT FROM consmr_cursor1
INTO #Id, #PrjId, #CustomerId, #UccCatId, #Area, #rate, #TotalAmt, #SubTotalAmt, #InvoiceNo, #InvoiceDate,
#DueDate, #BalanceAmt, #RoundOff, #EntryDate, #FromDate, #ToDate, #UserId, #deleteflag,
#ResponseCode, #ResponseMessage, #INVOICENO_RESPONSE
END
CLOSE consmr_cursor1
DEALLOCATE consmr_cursor1
SELECT *
FROM #consumerdata1
-- WHERE #RESPONSECODE <> 200
END
EXECUTE Bulkinvoicegeneration_lko
1,
2,
'LKO00066801'
Check this section in your procedure
DECLARE #fromDt DATETIME,
#monthdays INT = 10,
#ToDate DATETIME,
#Currentdate DATETIME,
#Year INT , ---- you need to pass some value over there
#InvoiceMonth varchar(50) = '1' --- this is passed as parameter from your procedure
SET #currentdate = CONVERT(VARCHAR, #Year) + '-'
+ CONVERT(VARCHAR, #InvoiceMonth) + '-11'
Select #currentdate ---- this value is NULL, since #Year is not passed.
Since you are using #currentdate for generating your #frmdt and #todt so they are also NULL.
DECLARE #fromDt DATETIME,
#monthdays INT = 10,
#ToDate DATETIME,
#Currentdate DATETIME,
#Year INT = 2019,
#InvoiceMonth varchar(50) = '1' --- this is passed as parameter from your procedure
SET #currentdate = CONVERT(VARCHAR, #Year) + '-'
+ CONVERT(VARCHAR, #InvoiceMonth) + '-11'
Select #currentdate ---- this value is '2019-01-11 00:00:00.000'.
---- After that
SET #fromDt = CONVERT(VARCHAR, DATEADD(d, - (DAY(DATEADD(m, -1, #currentdate - 2))), DATEADD(m, -1, #currentdate - 1)), 106)
SET #ToDate = CONVERT(DATE, Dateadd(d, -(Day(#currentdate)), #currentdate))
SET #monthdays = DATEDIFF(d, #fromDt, #ToDate) + 1
RESULT
frmdt toDate Monthdays
2018-12-01 00:00:00.000 2018-12-31 00:00:00.000 31
Update your code for setting this #currentdate once it is done hope rest of the block will fit on right place for you.... ;)
One thing I noticed is that you are not initializing the variable #year before using it.
If you want to set it to the current year, you can
SET #Year = year(getdate())
Add year parameter on this procedure.
ALTER PROCEDURE Bulkinvoicegeneration_lko #InvoiceMonth VARCHAR(20),
#PrjId BIGINT,
#CustomerId NVARCHAR(50),
#InvoiceYear INT
Then SET #Year = #InvoiceYear
Or SET YOUR #Year INT = 2019
I can calculate RSI for a specific end date:
DECLARE #StartingDate smalldatetime
DECLARE #EndingDate smalldatetime
DECLARE #StockID char(15)
DECLARE #DAYS INT
DECLARE #AG FLOAT
DECLARE #AL FLOAT
DECLARE #RS FLOAT
SET #StartingDate = '20180301'
SET #EndingDate = '20180403'
SET #StockID = 'ACE'
SET #DAYS = 14
SET #AG =(
SELECT SUM([px_close]-[px_open])
FROM [dbo].[daily_data]
WHERE [Ticker] = #STOCKID
AND ([Date] BETWEEN #StartingDate AND #EndingDate)
AND ([px_close]-[px_open])>0)/#DAYS
SET #AL =(
SELECT SUM([px_close]-[px_open])
FROM [dbo].[daily_data]
WHERE [Ticker] = #STOCKID
AND ([Date] BETWEEN #StartingDate AND #EndingDate)
AND ([px_close]-[px_open])<0)/#DAYS
SET #RS = #AG/ABS(#AL)
SELECT #StockID AS Ticker, #EndingDate AS Date, 100 - (100/(1+#RS)) RSI
Here's my output:
Ticker Date RSI
ACE 2018-04-03 48.7307
How can I calculate RSI for multiple dates and multiple tickers?
You don't need to set all of these to variables. You can just add the date and ticker to the group by and avoid the redundant subqueries... something like:
SELECT
[Ticker]
,[Date]
,AG = SUM(case when (isnull([px_close],0)-isnull([px_open],0))>0 then (isnull([px_close],0)-isnull([px_open],0)) end) / #days
,AL = SUM(case when (isnull([px_close],0)-isnull([px_open],0))<0 then (isnull([px_close],0)-isnull([px_open],0)) end) / #days
,RS = (SUM(case when (isnull([px_close],0)-isnull([px_open],0))>0 then (isnull([px_close],0)-isnull([px_open],0)) end) / #days) / ABS(SUM(case when (isnull([px_close],0)-isnull([px_open],0))<0 then (isnull([px_close],0)-isnull([px_open],0)) end) / #days)
,RSI = 100 - (100/(1+(SUM(case when (isnull([px_close],0)-isnull([px_open],0))>0 then (isnull([px_close],0)-isnull([px_open],0)) end) / #days) / ABS(SUM( case when (isnull([px_close],0)-isnull([px_open],0))<0 then (isnull([px_close],0)-isnull([px_open],0)) end) / #days)))
FROM
[dbo].[daily_data]
WHERE
[Ticker] = #STOCKID
AND ([Date] BETWEEN #StartingDate AND #EndingDate)
group by
[Ticker],[Date]
Remove [Ticker] = #STOCKID from the where clause to return all Tickers
I have 3 tables with columns shown below,
Holidays : HolidayId, HolidayName, HolidayDate, Comment
Entitlements:
EntitlementId, EmployeeId, LeaveTypeId, LeavePeriodId, FromDate
, UptoDate, Entitlementdays
Weekend: WeekeindId, Weekends
TODO:
create a store procedure for insertion
Before insertion it should check and count the dates in holiday and weekend table
if it is between the date which is in the startdate and enddate of entitlement table should be subtract.
in sql
Try this one as you already have weekend table and holiday table
CREATE PROCEDURE
usp_LeaveDays (#employeeid int)
as
BEGIN
DECLARE #fromdate DATE
SET #fromdate =
SELECT convert( varchar(10),fromdate,120) fromdate
FROM Entitlement
WHERE employeeid = #employeeid
DECLARE #enddate DATE
SET #enddate =
SELECT convert ( varchar(10),enddate,120) enddate
FROM entitlement
WHERE employeeid = #employeeid
DECLARE #weekendcount INT
SET #weekendcount =
SELECT Count(*)
FROM Weekend
WHERE weekends BETWEEN #fromdate AND #enddate;
DECLARE #holidayCount INT
SET #holidaycount =
SELECT Count(*)
FROM holidays
WHERE holidaydate BETWEEN #fromdate AND #enddate
DECLARE #Totaldays INT
SET #Totaldays =
SELECT DATEDIFF(day, #fromdate, #enddate)
DECLARE #Actualdays INT
SET #Actualdays = #Totaldays - (#weekendcount + isnull(#holidayCount, 0))
DECLARE #recordCount INT
SELECT #recordCount = #Actualdays
IF (#recordCount > 0)
begin
insert into -- Enter your script here
END