How to insert multidimensional array in database using laravel - laravel

I have a multidimensional array which contains employee salaries according to salary year with its respective months. I want to insert salaries of different year at different row with their respective months values. I also have one year column and 12 months column in database table. Please guide me how should I insert salaries of employees at different row in table.
My multidimensional array structure is like this:-
Array
(
[2016] => Array
(
[jan] => 15000
[feb] => 15000
[mar] => 15000
[apr] => 15000
[may] => 15000
[jun] => 15000
[jul] => 15000
[aug] => 15000
[sep] => 15000
[oct] => 15000
[nov] => 15000
[dec] => 15000
)
[2017] => Array
(
[jan] => 20000
[feb] => 20000
[mar] => 20000
[apr] => 20000
[may] => 20000
[jun] => 20000
[jul] => 20000
[aug] => 20000
[sep] => 20000
[oct] => 20000
[nov] => 20000
[dec] => 20000
)
)

You must flatten your array, you need an array like :
$data = [
['year'=>'2016', 'month'=>'1', 'salary' => 15000],
['year'=>'2016', 'month'=>'2', 'salary' => 15000],
// ... and so on
Then you can just insert using your model like :
YourSalaryModel::insert($data);

Q. Why aren't you saving them (or saved them) at that point in time i.e Jan 2017? (but that's an aside q)
I would have a salaries' table with a date column (2016-01-01), user_id, and a salary (whether int, or float/double depending on if they are always integer or can be float).
In your example, it is a case of doing two loops:
foreach ($salaries as $year => $months) {
foreach ($months as $month => $salary) {
// carbon parse to create a date
//insert into the table
}
}

Related

How to SUM a column in an oracle apex collection

So I am trying to output the total of a column from a collection.
This is the first query I tried
select sum(C007),sum(C007) A FROM APEX_COLLECTIONS WHERE COLLECTION_NAME='PURCHASE'
This is the second query I tried
select sum(C005*C007),sum(C005*C007) A FROM APEX_COLLECTIONS WHERE COLLECTION_NAME='PURCHASE'
Both produce the same result which list out all the values in the column insted of suming them
Expected Results:
10
Actual Results:
2
2
2
2
2
Please help
Looks like you did something wrong.
I created sample page; it contains a button (which will just submit the page) and an item which will display total (sum of collection's values). Item gets populated by a process which contains everything (for simplicity):
if not apex_collection.collection_exists('PURCHASE') then
apex_collection.create_collection('PURCHASE');
end if;
apex_collection.add_member(
p_collection_name => 'PURCHASE',
p_c001 => 'Little',
p_c007 => 100); --> 100 ...
apex_collection.add_member(
p_collection_name => 'PURCHASE',
p_c001 => 'Foot',
p_c007 => 200); --> ... + 200 = 300
select sum(c007)
into :P7_TOTAL
from apex_collections
where collection_name = 'PURCHASE';
When ran (and after button was pressed), item's value is - as expected - 300.

Sum up total data on the same date and group them by date

I have a transaction table below
id | amount | created_at
1 | 300 | 2021-01-01
2 | 400 | 2021-01-01
3 | 150 | 2021-01-02
4 | 600 | 2021-01-03
How do I total up all the amounts with the same date and display them group by created_at?
I've tried this so far but it seems wrong
$query->selectRaw('sum(amount) as amount, created_at')
->groupBy('created_at')
->get()
->map(function($item){
return [
'date' => date('d M', strtotime($item->created_at)),
'total' => $item->amount
];
});
If the created_at field is date format, your code won't have the problem you described in the comment (duplicate results).
I'm pretty sure that the column is not really date format but datetime.
You can use DATE(created_at) to actually convert it to date format :
$query->selectRaw('sum(amount) as amount, DATE(created_at) as date')
->groupBy('date')
->get()
->map(function ($item) {
return [
'date' => date('d M', strtotime($item->date)),
'total' => $item->amount,
];
});

how to use dbms_scheduler to run the job every 30 minutes

I am using oracle DB , now as i am monitoring the performance of oracle DB which is connected to my java application , so rite now i have to monitor the count of active connections in DB at regular intervals lets say after every 30 minutes below is the query which return me the count of active users along with there name and count
select osuser, count(osuser) as active_conn_count
from v$session
group by osuser
order by active_conn_count desc
now please advise how can i make an schedule a job in scheduler in oracle DB itself that will get triggered at every 30 minutes .
I would suggest to keep your statistics in a table (say my_log_table), in that case schedule would look something like this:
begin
DBMS_SCHEDULER.CREATE_JOB (
job_name => 'keep_stats',
job_type => 'PLSQL_BLOCK',
job_action => 'begin insert into my_log_table (mUser,mCnt) (select osuser, count(osuser) as active_conn_count from v$session group by osuser order by active_conn_count desc);commit;end;',
start_date => timestamp '2016-11-07 19:00:00',
repeat_interval => 'FREQ=MINUTELY;INTERVAL=30;',
enabled => TRUE);
end;
There are pretty much examples about dbms_scheduler. It's easy to submit a recurring job. But actually you do not need to do that! Oracle already stores tons of data about database performance, it will be easier and much more accurate to use these statistics.
Take a look at the active_session_history over here.
Hope this is what you are looking for.
repeat_interval => 'FREQ=HOURLY; BYMINUTE=0,30; BYSECOND=0 ', -
First, you create a schedule.
dbms_scheduler.create_schedule( schedule_name => 'EVERY_1ST_SATURDAY'
, repeat_interval => 'FREQ=MONTHLY;INTERVAL=1;BYDAY=1 SAT;BYHOUR=2;BYMINUTE=0'
, start_date => SYSTIMESTAMP
, comments => '1st Saturday of the month 2am.'
);
Optionally, you create a program:
dbms_scheduler.create_program( program_name => 'GATHER_SCHEMA_STATS'
, program_type => 'STORED_PROCEDURE'
, program_action => 'dbms_stats.GATHER_SCHEMA_STATS'
, number_of_arguments => 1
, enabled => FALSE
, comments => 'Gather schema''s stats (e.g. FRAMEWORK''s)'
);
... grant access to it:
GRANT execute ON sys.GATHER_SCHEMA_STATS TO framework;
... and parameters for it:
DBMS_SCHEDULER.define_program_argument ( 'GATHER_SCHEMA_STATS', 1 , 'ownname', 'VARCHAR2', 'FRAMEWORK' );
Secondly, you create a job:
DBMS_SCHEDULER.CREATE_JOB ( job_name => 'FRAMEWORK.GATHER_FRAMEWORK_STATS_JOB1'
, program_name => 'SYS.GATHER_SCHEMA_STATS'
, schedule_name => 'SYS.EVERY_1ST_SATURDAY'
, enabled => TRUE
, auto_drop => FALSE
, comments => 'Gather FRAMEWORK''s stats on 1st Saturday of the month.'
);

Oracle job scheduler not working

i create a job in oracle which will run in every minute but the problem is job is create successfully but not run on time
execute PACKAGE_BATCH.USP_TERMIANTE_SUSPENSION;
BEGIN
DBMS_SCHEDULER.CREATE_JOB (
job_name => '"LTR"."TERMINATE_SUSPENSION_JOB"',
job_type => 'STORED_PROCEDURE',
job_action => 'LTR.PACKAGE_BATCH.USP_TERMINATE_SUSPENSION',
number_of_arguments => 0,
start_date => sysdate,
repeat_interval => 'FREQ=MINUTELY;BYMINUTE=1',
end_date => NULL,
enabled => TRUE,
auto_drop => TRUE,
comments => 'Terminate Suspension When End Date is equal to Current Date');
DBMS_SCHEDULER.set_attribute
( name => '"LTR"."TERMINATE_SUSPENSION_JOB"',
attribute => 'job_action',
value => 'LTR.PACKAGE_BATCH.USP_TERMINATE_SUSPENSION');
DBMS_SCHEDULER.SET_ATTRIBUTE(
name => '"LTR"."TERMINATE_SUSPENSION_JOB"',
attribute => 'logging_level', value => DBMS_SCHEDULER.LOGGING_RUNS);
DBMS_SCHEDULER.enable(
name => '"LTR"."TERMINATE_SUSPENSION_JOB"');
END;
FREQ=MINUTELY;BYMINUTE=1
This will make the job run at every "1"st minute. So 12:01, 1:01, 2:01 etc.
If you need to make it run every minute - FREQ=MINUTELY is sufficient or you can add BYSECOND=0 if you want to ensure it runs at exactly 12:01:00, 12:02:00, 12:03:00 etc.
To keep it simple, see these examples:
Every minute
Repeat interval using calendaring syntax.
'freq=minutely;'
Repeat interval using dates and timestamps.
'sysdate + 1/24/60'
'systimestamp + 1/24/60'
'sysdate + interval ''1'' minute'
'systimestamp + interval ''1'' minute'
Every minute, on the minute
Repeat interval using calendaring syntax.
'freq=minutely; bysecond=0;'
Repeat interval using dates and timestamps.
'trunc(sysdate, ''MI'') + 1/24/60'
'trunc(systimestamp, ''MI'') + 1/24/60'
'trunc(sysdate, ''MI'') + interval ''1'' minute'
'trunc(systimestamp, ''MI'') + interval ''1'' minute'
More example here http://oracle-base.com/articles/10g/scheduler-10g.php#every_minute

HBase getting all timestamped values for a cell

i have the following scenario in my hbase instance
hbase(main):002:0> create 'test', 'cf'
0 row(s) in 1.4690 seconds
hbase(main):003:0> put 'test', 'row1', 'cf:a', 'value1'
0 row(s) in 0.1480 seconds
hbase(main):004:0> put 'test', 'row2', 'cf:b', 'value2'
0 row(s) in 0.0070 seconds
hbase(main):005:0> put 'test', 'row3', 'cf:c', 'value3'
0 row(s) in 0.0120 seconds
hbase(main):006:0> put 'test', 'row3', 'cf:c', 'value4'
0 row(s) in 0.0070 seconds
Now if you will see, the last two inserts are for the same column family, same column and same key. But if i understand hbase properly cf:c+row3 represent a cell which will have all timestamped versions of inserted value.
But a simple scan return only recent value
hbase(main):010:0> scan 'test'
ROW COLUMN+CELL
row1 column=cf:a, timestamp=1317945279379, value=value1
row2 column=cf:b, timestamp=1317945285731, value=value2
row3 column=cf:c, timestamp=1317945301466, value=value4
3 row(s) in 0.0250 seconds
How do i get all timestamped values for a cell, or how to perform time range based query?
In order to see versions of a column you need to give the version count.
scan 'test', {VERSIONS => 3}
will give you 2 versions of columns if they are available. you can use it in get aswell :
get 'test', 'row3', {COLUMN => 'cf:c', VERSIONS => 3}
for getting the value of a spesific time you can use TIMESTAMP aswell.
get 'test', 'row3', {COLUMN => 'cf:c', TIMESTAMP => 1317945301466}
if you need to get values "between" 2 timestamps you should use TimestampsFilter.
To change the number of versions allowed in a column family use the following command:
alter 'test', NAME=>'cf', VERSIONS=>2
then add another entry:
put 'test', 'row1', 'cf:a2', 'value1e'
then see the different versions:
get 'test', 'row1', {COLUMN => 'cf:a2', VERSIONS => 2}
would return something like:
COLUMN CELL
cf:a2 timestamp=1457947804214, value=value1e
cf:a2 timestamp=1457947217039, value=value1d
2 row(s) in 0.0090 seconds
Here is a link for more details:
https://learnhbase.wordpress.com/2013/03/02/hbase-shell-commands/.
The row key 'row3' of cf:c for value4 should be unique otherwise it gets overwritten:
hbase(main):052:0> scan 'mytable' , {COLUMN => 'cf1:1', VERSION => 3}
ROW COLUMN+CELL
1234 column=cf1:1, timestamp=1405796300388, value=hello
1 row(s) in 0.0160 seconds
hbase(main):053:0> put 'mytable', 1234, 'cf1:1', 'wow!'
0 row(s) in 0.1020 seconds
Column 1 of cf1 having a value of 'hello' is overwritten by second put with same row key 1234 and a value of 'wow!'
hbase(main):054:0> scan 'mytable', {COLUMN => 'cf1:1', VERSION => 3}
ROW COLUMN+CELL
1234 column=cf1:1, timestamp=1405831703617, value=wow!
2 row(s) in 0.0310 seconds
Now the second insert contained a new value 'hey' for column 1 of cf1 and the scan query for last 3 versions now shows 'wow!' and 'hey', please not the versions are displayed on descending order.
hbase(main):055:0> put 'mytable', 123, 'cf1:1', 'hey'
hbase(main):004:0> scan 'mytable', {COLUMN => 'cf1:1', VERSION => 3}
ROW COLUMN+CELL
123 column=cf1:1, timestamp=1405831295769, value=hey
1234 column=cf1:1, timestamp=1405831703617, value=wow!

Resources