Getting Data From Yahoo - stock

Could you please help me to fix code, which is taken partly from a book, below. It gives an error "The server returned the status 401 with message "Unauthorized" in response to the request to URL https://query1.finance.yahoo.com/v7/finance/download/AAPL?period1=%EF%BF%BF&period2=%EF%BF%BF&interval=1devents=history&crumb=GAAvT/4ue2V."
Thanks a lot.
function [date, open, high, low, close, vol, adjclose]= downloadyahoo(symbol, period, start_date, end_date)
% download the data from finance.yahoo.com
% period can be '1d' for daily data, '1wk' for weekly data, '1mo' for monthly
% data
origDate = datenum('01-Jan-1970 00:00:00', 'dd-mmm-yyyy HH:MM:SS');
if ischar(start_date)
startDate = (datenum(start_date, 'ddmmyyyy') - origDate) * 24 * 60 * 60;
else
startDate = (floor(start_date) - origDate) * 24 * 60 * 60;
end
if ischar(end_date)
endDate = (datenum(end_date, 'ddmmyyyy') - origDate) * 24 * 60 * 60;
else
endDate = (floor(end_date) - origDate) * 24 * 60 * 60;
end
% Create URL string and download csv file
url_string = ['https://query1.finance.yahoo.com/v7/finance/download/' upper(symbol) '?period1=' startDate '&period2=' end_date '&interval=' period '&events=history&crumb=GAAvT/4ue2V'];
urlwrite(url_string,['data_' upper(symbol) '.csv']);
import=importdata(['data_' upper(symbol) '.csv']);
delete(['data_' upper(symbol) '.csv']);
% Reverse to normal chronological order, so 1st entry is oldest data point
open = flipud(import.data(:,1));
high = flipud(import.data(:,2));
low = flipud(import.data(:,3));
close = flipud(import.data(:,4));
vol = flipud(import.data(:,5));
adjclose = flipud(import.data(:,6));
for a=length(open):-1:1
date(length(open)-a+1)=datenum((import.textdata{a+1}),'yyyy-mm-dd');
end
end

Related

How to reduce the fetching time in view table?

I have tried to fetch the data from view table .I have executed select query in phpmyadmin .It takes 5.2875 seconds. But normal table takes only 0.0300 seconds.
Query :
SELECT line FROM `summary_view` WHERE date='2022-02-25'
CREATE VIEW summary_view AS
select
adh.id AS id,
adh.line AS line,
fam.area AS area,
fam.sub_area AS sub_area,
fam.family AS family,
pro.produced AS produced,
pro.service AS service,
round(((testdb.down * 60) / adh.takt_time),0) AS units_lost,
round(((((adh.worked_time * 60) - adh.break_time) * 60) / adh.takt_time),0) AS oa_capacity,
round(((((adh.plan_time * 60) - adh.break_time) * 60) / adh.takt_time),0) AS ay_capacity,
testdb.machines AS machines,
testdb.manpower AS manpower,
testdb.material AS material,
testdb.methods AS methods,
testdb.misc AS misc,
testdb.down AS down,
round((((pro.produced + pro.service) * adh.takt_time) / 60),0) AS uptime,
adh.break_time AS break_time,
round(((adh.worked_time * 60) - (((((pro.produced + pro.service) * adh.takt_time) / 60) + testdb.down) + adh.break_time)),0) AS minute_error,
round((adh.worked_time * 60),0) AS working_time,
round(((adh.worked_time * 60) - adh.break_time),0) AS worked_time,
round(((adh.plan_time * 60) - adh.break_time),0) AS plan_time,
round(adh.takt_time,0) AS takt_time,
NULL AS headcount,
rep.nff AS nff,
rep.dpu AS dpu,
round(((((pro.produced + pro.service) * adh.takt_time) / 60) / ((adh.worked_time * 60) - adh.break_time)),4) AS oa,
round(((((pro.produced + pro.service) * adh.takt_time) / 60) / ((adh.plan_time * 60) - adh.break_time)),4) AS ay,
adh.shift AS shift,
GET_MONTH(adh.date) AS month,
GET_WEEK(adh.date) AS week,
adh.date AS date,
adh.skey AS skey
from
(
(
(
(
adhoc adh
left join
(
select
sum(if((down.category = 'Machines'),down.duration,NULL)) AS machines,
sum(if((down.category = 'Manpower'),down.duration,NULL)) AS manpower,
sum(if((down.category = 'Material'),down.duration,NULL)) AS material,
sum(if((down.category = 'Methods'),down.duration,NULL)) AS methods,
sum(if((down.category = 'Misc'),down.duration,NULL)) AS misc,
sum(down.duration) AS down,
down.skey AS skey
from (down join family on((down.line = family.line)))
where ((down.reason <> 'Scheduled Shutdown') and (family.area in ('Indoor','Outdoor','Specialty')))
group by down.skey
)
testdb on((adh.skey = testdb.skey))
)
left join
(
select
count(if((prod.repair_flag <> 'S'),1,NULL)) AS produced,
count(if((prod.repair_flag = 'S'),1,NULL)) AS service,
prod.skey AS skey
from (prod join family on((prod.line = family.line)))
group by prod.skey
)
pro on((adh.skey = pro.skey))
)
left join
(
select
count(if((repair.level_one = 'No Fault Found'),1,NULL)) AS nff,
count(if(((repair.level_one <> 'No Fault Found') and (repair.level_two <> 'Reclaim Refrigerant')),1,NULL)) AS dpu,
repair.skey AS skey
from repair
group by repair.skey
)
rep on((adh.skey = rep.skey))
)
join family fam on((adh.line = fam.line))
)
where (adh.area = 'Assembly')
;
How to reduce the query execution time in view table ?
Is there any way to reduce the execution time without adding index ?

Need store procedure to calculate from these two tables

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

SQL Match employee intime (punch time) with employee shift

I have a Stored Procedure that retrieves employee daily summary intime - outtime:
SELECT ads.attendancesumid,
ads.employeeid,
ads.date,
ads.day, -- month day number
ads.intime,
ads.outtime
--employee shift intime and outtime
ss.intime,
ss.outtime
FROM employee_attendance_daily_summary ads
JOIN employee emp
ON emp.employeeid = ads.employeeid
JOIN setup_shift ss
ON ss.shiftcode = emp.shiftcode
AND DATEPART(dw, ads.date) = ss.day
WHERE ads.employeeid = 4 -- just to filter one employee
The result of the query is something like this:
Each day is repeated 3 times because table setup_shift (employee shifts) has:
Monday to Sunday for 3 different shift types: DAY, AFTERNOON and NIGHT.
Here is the same info but with the shift type column:
What I need is to ONLY get 1 row per day but with the closest employee shift depending on the intime and outtime.
So the desire result should looks like this:
Any clue on how to do this? Appreciate it in advance.
I have also these case where intime is 00:00:00 but outtime has a value:
UPDATE:
HERE IS THE SQL FIDDLE
http://sqlfiddle.com/#!6/791cb/7
select ads.attendancesumid,
ads.employeeid,
ads.date,
ads.day,
ads.intime,
ads.outtime,
ss.intime,
ss.outtime
from employee_attendance_daily_summary ads
join employee emp
on emp.employeeid = ads.employeeid
join setup_shift ss
on ss.shiftcode = emp.shiftcode
and datepart(dw, ads.date) = ss.day
where ads.employeeid = 4
and ((abs(datediff(hh,
cast(ads.intime as datetime),
cast(ss.intime as datetime))) between 0 and 2) or
(ads.intime = '00:00:00' and
ss.intime =
(select min(x.intime)
from setup_shift x
where x.shiftcode = ss.shiftcode
and x.intime > (select min(y.intime)
from setup_shift y
where y.shiftcode = x.shiftcode))))
This would be much easier if the times were in seconds after midnight, rather than in a time, datetime, or string format. You can convert them using the formula:
select datepart(hour, intime) * 3600 + datepart(minute, intime) * 60 + datepart(second, intime)
(Part of this is just my own discomfort with all the nested functions needed to handle other data types.)
So, let me assume that you have a series of similar columns measured in seconds. You can then approach this problem by taking the overlap with each shift and choosing the shift with the largest overlap.
with t as (
<your query here>
),
ts as (
select t.*,
(datepart(hour, ads.intime) * 3600 + datepart(minute, ads.intime) * 60 +
datepart(second, ads.intime)
) as e_intimes,
. . .
from t
),
tss as (
select ts.*,
(case when e_intimes >= s_outtimes then 0
when e_outtimes <= s_inttimes then 0
else (case when e_outtimes < s_outtimes then e_outtimes else s_outtimes end) -
(case when e_intimes > s_intimes then e_intimes else s_intimes end)
end) as overlap
from ts
)
select ts.*
from (select ts.*,
row_number() over (partition by employeeid, date
order by overlap desc
) as seqnum
from ts
) ts
where seqnum = 1;
Try this man,I just take the minimum time difference of the each set datediff(mi,intime,shift_intime)
Select * from
(select
row_number() over(partition by employeeid
order by datediff(mi,intime,shift_intime) asc) as id,
attendance,employeeid,date,day,intime,outime,shiftintime,shiftoutime from table
)
where id=1

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)

ORA-00937 not a single-group group function converted from SQL Server

I'm getting an ORA-00937 error when running the following ORACLE statement, I'm very new to Oracle (T-SQL is more my bag). I've converted a select statement previously written in SQL I'm at a loss as to why it's expecting a group by ... as all columns are sum'd. This is the converted select statement (I'm currently running/ testing these using SQLPLUS) :
select
round(COALESCE(
(COALESCE((select SUM((F_BOOK_DATES.BKD_ADATE_ENDS - F_BOOK_DATES.BKD_ADATE_START) * (60 * 24)) FROM F_BOOK_DATES
INNER JOIN F_BOOK_HEADER ON F_BOOK_DATES.BKD_FKEY_BK_SEQ = F_BOOK_HEADER.BK_SEQ
and EXTRACT (YEAR from F_BOOK_DATES.BKD_DATE_START ) = EXTRACT (YEAR from SYSDATE )
and EXTRACT (MONTH from F_BOOK_DATES.BKD_DATE_START ) = EXTRACT (MONTH from SYSDATE )
and EXTRACT (DAY from F_BOOK_DATES.BKD_DATE_START ) = EXTRACT (DAY from SYSDATE )
AND F_BOOK_HEADER.BK_STATUS NOT IN ('CX','TP')
AND F_BOOK_HEADER.Deleted <> 1
AND F_BOOK_DATES.Deleted <> 1),9999999999.99,0)
/sum(case when FAREALO.AllDayBooking = 1 then 1400 else
((FAREALO.LO_TIME_END - FAREALO.LO_TIME_START) * (60 * 24)) end) * 100),0),2) as "PercentUtilised"
from FAREALO
inner join F_LO_TYPE on FAREALO.LO_FKEY_LOT_SEQ = F_LO_TYPE.LOT_SEQ
where FAREALO.LO_BK_LOCATION = 1
and LOT_CBS = 1;
This is the SQL statement (that works) that I tried to convert above:
select
round(isnull(
(isnull(convert (DECIMAL(10,2),(select SUM(datediff(n,BKD_ADATE_START, BKD_ADATE_ENDS)) FROM F_BOOK_DATES with (NOLOCK)
INNER JOIN F_BOOK_HEADER with (NOLOCK) ON BKD_FKEY_BK_SEQ = BK_SEQ
AND DATEPART(YYYY,BKD_DATE_START) = DATEPART(YYYY,GETDATE())
AND DATEPART(MM,BKD_DATE_START) = DATEPART(MM,GETDATE())
AND DATEPART(DD,BKD_DATE_START) = DATEPART(DD,GETDATE())
AND BK_STATUS NOT IN ('CX','TP')
AND F_BOOK_HEADER.Deleted <> 1
AND F_BOOK_DATES.Deleted <> 1)),0)
/
sum(case when AllDayBooking = 1 then 1400 else
datediff(n,LO_TIME_START, LO_TIME_END) end) * 100),0),2) as PercentUtilised
from FAREALO with (NOLOCK)
inner join F_LO_TYPE with (NOLOCK) on LO_FKEY_LOT_SEQ = LOT_SEQ
where LO_BK_LOCATION = 1
and LOT_CBS = 1
Anybody see what I've missed?
Thanks
J
Here's the altered query which runs fine, after adding the MIN function:
select
round(COALESCE(
(COALESCE(MIN((select SUM((BKD_ADATE_ENDS - BKD_ADATE_START) * (60 * 24)) FROM F_BOOK_DATES
INNER JOIN F_BOOK_HEADER ON BKD_FKEY_BK_SEQ = BK_SEQ
and EXTRACT (YEAR from BKD_DATE_START ) = EXTRACT (YEAR from SYSDATE )
and EXTRACT (MONTH from BKD_DATE_START ) = EXTRACT (MONTH from SYSDATE )
and EXTRACT (DAY from BKD_DATE_START ) = EXTRACT (DAY from SYSDATE )
AND BK_STATUS NOT IN ('CX','TP')
AND F_BOOK_HEADER.Deleted <> 1
AND F_BOOK_DATES.Deleted <> 1)),9999999999.99,0)
/sum(case when AllDayBooking = 1 then 1400 else
((LO_TIME_END - LO_TIME_START) * (60 * 24)) end) * 100),0),2) as "PercentUtilised"
from FAREALO
inner join F_LO_TYPE on LO_FKEY_LOT_SEQ = LOT_SEQ
where LO_BK_LOCATION = 1
and LOT_CBS = 1;