Need store procedure to calculate from these two tables - sql

I should do this in xampp need an sp structure for this as i am a beginner if possible help to give complete sp for this scenario with detailed explanantion. Other structure like crud operations has been done with php,javascript
1. SELECT `cid`, `parent_cat`, `category_name`, `category_life`, `status` FROM `categories`
2. SELECT `pid`, `cid`, `bid`, `product_name`, `product_price`, `product_stock`, `added_date`, `p_status`, `loc`, `in_no`, `gst_no`, `cgst`, `sgst`, `igst`, `total`, `depre`, `pur_from` FROM `products`
From the above two table's i should fetch record and calculate those fields and insert in the below table:
3. SELECT `elapsed_yend`, `remaining_days`, `depreciation_cur`, `current_wdv`, `depreciation_next`, `next_wdv`, `accumulate_depre`, `sale_amount`, `pro_los`, `end_date` FROM `calculation`
Example, these calculation should happen in products table:
CGST:
cgst = product_price * 09
sgst = product_price * 09
igst = product_price * 09
if status is 1 it should calculate igst other cgst,sgst must be 0(zero)
0 it should calculate cgst,sgst other igst must be 0(zero)
Depreciation Amount:
depre = product_price + .5(cgst+sgst+igst)
Elapsede year end :
elapsed_yend = added_date(from products table (No:2)) till every year 31/March/xxxx.
Number of days between these two dates
remaining_days = category_life - (date difference between added_date(from products (No:1)) till date)
category_life(from categories table (No:1))Ex:mobile it's life will be 1080 days.
Current Year depreciation:
depreciation_cur = (depre/category_life)*elapsed_yend
Current Year Written down value:
current_wdv = depre - depreciation_cur
Next Year depreciation :
depreciation_next = (depre / category_life) * D
D = days difference between every year 01/April/xxxx till end_date(from calculation table)
Next Year Written down value:
next_wdv = current_wdv - depreciation_next
Accumulate Depreciation :
accumulate_depre = depreciation_cur + depreciation_next

I have cracked it and it is working fine as of now.
BEGIN
DECLARE cur_depre,cur_wtvalue,next_depre,next_wtvalue,accum_depre,pro_loss double(20,2);
DECLARE days,next_diff int;
CREATE TABLE IF NOT EXISTS calc AS (SELECT p.pid,p_date,days,next_diff,cur_depre,cur_wtvalue,next_depre,next_wtvalue,accum_depre,pro_loss,c.cid,p.added_date,c.category_life,p.depre,p.sale_status,p.sale_date,p.sale_amount from products p,products d,categories c WHERE p.pid=d.pid AND p.cid=c.cid
);
INSERT INTO calc (PID)
SELECT PID FROM products WHERE PID NOT IN (SELECT PID FROM calc);
UPDATE calc set days = datediff(p_date,added_date);
UPDATE calc set days = 0 WHERE datediff(p_date,added_date) < 0;
UPDATE calc set next_diff = datediff(sale_date,DATE_ADD(p_date, INTERVAL 1 DAY) );
UPDATE calc set next_diff = datediff('2019-03-31',added_date) WHERE added_date>p_date;
UPDATE calc set cur_depre = (depre/category_life)*datediff(p_date,added_date);
UPDATE calc set cur_depre = 0 where (depre/category_life)*datediff(p_date,added_date)<0;
UPDATE calc set cur_wtvalue =(depre-(depre/category_life)*datediff(p_date,added_date));
UPDATE calc set cur_wtvalue = 0 WHERE added_date>p_date;
UPDATE calc set next_depre =( (depre/category_life)*datediff(sale_date,DATE_ADD(p_date, INTERVAL 1 DAY) )) where added_date < p_date;
UPDATE calc set next_depre =( (depre/category_life) * datediff('2019-03-31',added_date)) WHERE added_Date > p_date;
UPDATE calc SET next_wtvalue = depre -(depre/category_life)*datediff(p_date,added_date) - (( (depre/category_life)*datediff(sale_date,DATE_ADD(p_date, INTERVAL 1 DAY) )) );
UPDATE calc SET next_wtvalue = depre - (( (depre/category_life) * datediff('2019-03-31',added_date))) WHERE added_date>p_date;
UPDATE calc SET accum_depre = ((depre/category_life)*datediff(p_date,added_date))+( (depre/category_life)*datediff(sale_date,DATE_ADD(p_date, INTERVAL 1 DAY) )) ;
UPDATE calc SET accum_depre =( (depre/category_life) * datediff('2019-03-31',added_date)) WHERE added_Date > p_date;
UPDATE calc SET pro_loss = sale_amount-(depre -(depre/category_life)*datediff(p_date,added_date) - (( (depre/category_life)*datediff(sale_date,DATE_ADD(p_date, INTERVAL 1 DAY) )) ) );
UPDATE calc SET pro_loss = 0 WHERE sale_status=0;
SELECT * from calc;
END

Related

Make a case with several results SQL TERADATA

I am looking to make a case in a SQL query and assign according to the condition several results.
For example :
Code :
INSERT INTO DESTINATION_TABLE (DT_TRT, NU_QUARTER, NU_YEAR) VALUES
(SELECT
CASE
WHEN #P_DT_TRT# = '1900-00-00'
THEN MAX(TT.DT_CTTT)
ELSE #P_DT_TRT#
END AS DT_TRT,
CASE
WHEN EXTRACT (MONTH FROM DT_TRT) < 4
THEN NU_QUARTER = 4 AND NU_YEAR = EXTRACT (YEAR FROM DT_TRT) - 1
ELSE NU_YEAR = EXTRACT (YEAR FROM DT_TRT)
END
CASE
WHEN EXTRACT (MONTH FROM DT_TRT) < 7
THEN 1
ELSE (CASE WHEN EXTRACT (MONTH FROM DT_TRT) < 10 THEN 2 ELSE 3 END AS NU_QUARTER)
END AS NU_QUARTER
FROM TARGET_TABLE TT);
Algorithm :
-> A date will be given in the programme to enable the calculation (#P_DT_TRT#)
If the parameter is not supplied (value = 1900-00-00)
DT_TRT = the largest constitution date (DT_CTTT) in the target table (TARGET_TABLE TT)
Otherwise DT_TRT = date given in parameter
If DT_TRT month < 4
Quarter = 4
Year = Year of DT_TRT - 1
Otherwise Year = Year of DT_TRT
If DT_TRT month < 7
Quarter = 1
Otherwise
If DT_TRT < 10
Quarter = 2
Otherwise Quarter = 3
Question : Is it possible to integrate several results (DT_TRT, NU_QUARTER, NU_YEAR) in one case ? And if so, what is the syntax ?
I work in Teradata Studio.
Thank you for your answers. :)
This seems to be your logic:
INSERT INTO DESTINATION_TABLE (DT_TRT, NU_QUARTER, NU_YEAR)
VALUES
(
-- If the parameter is not supplied (value = 1900-00-00)
-- DT_TRT = the largest constitution date (DT_CTTT) in the target table (TARGET_TABLE TT)
-- Otherwise DT_TRT = date given in parameter
CASE
WHEN #P_DT_TRT# = '1900-00-00'
THEN (SELECT Max(DT_CTTT) FROM TARGET_TABLE)
ELSE #P_DT_TRT#
END,
-- shift back year/quarter by three months to adjust for company's business year
td_quarter_of_year(Add_Months(DT_TRT, -3)),
Extract(YEAR From Add_Months(DT_TRT, -3))
)
;

Continous update variable value based on previous value

I am trying a update value of the sales column based on previously updated value. for example -
Table A
Day growth sales
1 1.1 1
2 1.2 NULL
3 1.3 NULL
I want to update the sales value based on growth.
So, for day 2 it would be day1 sales * growth.
and day 3 would be day 2 updated sales * growth.
Is this possible without a loop in netezza ?
Thanks everyone.
You can use a cumulative sum and logs to get a cumulative product:
select t.*,
(case when day = 1 then sales
else (exp(sum(ln(growth)) over (order by day) / growth) *
max(case when day = 1 then sales end) over ()
end) as new_sales
from t;
DECLARE #_i NUMERIC(18,2) = (SELECT Sales FROM A WHERE Day = '1')
UPDATE A
SET #_i = Sales = #_i * CASE WHEN _Day = '1' THEN (SELECT Sales FROM A WHERE Day = '1')
ELSE GROWTH END
SELECT * FROM A
Here is my answer, try this

Summary field values from some period

In one table I have currency rate in some time period(five years). In another table, I have calculated data.
I have the task. I need to get statistics by every week summary from joined tables
I am having this at the moment:
DECLARE #Category_RentHouse INT = 3;
DECLARE #Category_Parents INT = 5;
DECLARE #Category_Salary INT = 9;
DECLARE #TestDateStart DATE = '2012-01-01';
DECLARE #TestDateFinish DATE = '2012-01-07';
select Weeks, SUM(Cash_Usd) TotalMoney
from (select CAST(RateDate AS DATE) Weeks,
CASE WHEN CategoryID = #Category_RentHouse THEN (TransactionAmount*(-1))
WHEN CategoryID = #Category_Parents THEN TransactionAmount
WHEN CategoryID = #Category_Salary THEN CAST((TransactionAmount /
RateValue) AS MONEY)
ELSE CAST((TransactionAmount*(-1) / RateValue) AS MONEY)
END AS Cash_Usd
FROM (select * from Marathon.dbo.Transactions T
LEFT JOIN IntermediateM.dbo.Rates R ON T.TransactionDate = R.RateDate) Y
) RR
WHERE Weeks BETWEEN #TestDateStart AND #TestDateFinish
GROUP BY DATEPART(week, Weeks), Weeks
ORDER BY Weeks
And result of run this small code is
But it would better if in fields Weeks and TotalSumm I will get the next:
Weeks TotalSumm
2012-01-07 -552...
2012-01-14 ....
I think you just need to fix the select and group by:
SELECT MIN(Dte) as Weeks, SUM(Cash_Usd) TotalMoney
FROM (SELECT CAST(RateDate AS DATE) as Dte,
(CASE WHEN CategoryID = #Category_RentHouse THEN (TransactionAmount*(-1))
WHEN CategoryID = #Category_Parents THEN TransactionAmount
WHEN CategoryID = #Category_Salary THEN CAST((TransactionAmount / RateValue) AS MONEY)
ELSE CAST((TransactionAmount*(-1) / RateValue) AS MONEY)
END) AS Cash_Usd
FROM Marathon.dbo.Transactions T LEFT JOIN
IntermediateM.dbo.Rates R
ON T.TransactionDate = R.RateDate
WHERE Weeks BETWEEN #TestDateStart AND #TestDateFinish
) RT
GROUP BY DATEPART(week, Weeks)
ORDER BY Weeks

Subtract value in sql

I have two table,as per requirement we need to subtract the values as given below. kindly assist me how to solve this puzzle....
Table1
MONTH Fee(Advance)
APRIL 5000
Table2
MONTH Fee
MAY 2000
JUNE 300
JULY 1800
AUG 1200
Sep 1500
Expecting Result
ROW 1 MAY 5000(TABLE1.ADVANCE)-2000(TABLE2.FEE)= 3000
ROW 2 JUNE 3000-300 = 2700
ROW 3 JULY 2700-1800 = 900
ROW 4 AUG 900-1200 = -300
ROW 5 Sep -300-1500 = -1800
I am not sure if this is the right way of doing this. Instead of SQL statement, maybe stored procedure will help?
DECLARE #AdvanceFee INT;
DECLARE #cnt INT = 1;
DECLARE #cnt_total INT = (Select Count(MONTH) From Table2);
SET #AdvanceFee = (Select Fee(Advance) from Table1)
WHILE #cnt <= #cnt_total
BEGIN
Select MONTH, #AdvanceFee = #AdvanceFee - Fee
FROM Table2
WHERE ROW_NUMBER = #cnt
SET #cnt = #cnt + 1;
END
You need a table containing month names and numbers ('MAY' = 5, 'Sep' = 9, ...). Then use SUM OVER to get a running total:
select
t.month,
(select fee from table1 where month = 'APRIL') - sum(t.fee) over (order by m.monthnum)
as balance
from table2 t
join months m on m.name = t.month
order by m.monthnum;

SQL Server: check if certain date in calendar table falls on a weekend

I have created a calendar table that contains all the calendar dates of a year, incl. the corresponding quarter / week / month / day etc. information.
The following Select gives me a specific date, here the 17th of March.
How can I extend the below to check if this falls on a Saturday or Sunday (weekDayCal = 7 or 1) and, if true, return the date for the following Monday, otherwise return the 17th ?
SELECT *
FROM Calendar
WHERE (yearCal = 2014) AND (monthCal = 3) AND (dayCal = 17)
Many thanks in advance for any help with this, Mike.
Assuming you have a day_of_calendar style id field, where every date is sequentially in order, then this works...
SELECT *
FROM Calendar
WHERE id = (SELECT id + CASE weekDayCal WHEN 7 THEN 2 WHEN 1 THEN 1 ELSE 0 END
FROM Calendar
WHERE (yearCal = 2014) AND (monthCal = 3) AND (dayCal = 17)
)
If not, then you're going to have to return to using dates in one way or another.
For example...
SELECT *
FROM Calendar
WHERE realDate = (SELECT realDate + CASE weekDayCal WHEN 7 THEN 2 WHEN 1 THEN 1 ELSE 0 END
FROM Calendar
WHERE (yearCal = 2014) AND (monthCal = 3) AND (dayCal = 17)
)
But then you may as well just use real date calculations.
I think this should work, if you do indeed have a weekDayCal column where 1=Sunday, 2 = Monday and 7 = Saturday:
SELECT *
FROM Calendar
WHERE (yearCal = 2014) AND (monthCal = 3) AND (
(dayCal = 17 and weekDayCal not in (1,7)) OR
(dayCal = 17 + 1 and weekDayCal = 2) OR
(dayCal = 17 + 2 and weekDayCal = 2))
This fetches rows who fall on sunaday or saturday
SELECT *
FROM Calender
WHERE DATEPART(dw, CAST(
CAST(monthCal as VARCHAR(2))+ '-'+
CAST(dayCal as VARCHAR(2))+'-'+
CAST(yearCal as VARCHAR(4))
AS DATETIME
)
) IN(1,7)