Grabbing all rows from a database that have specific column values in laravel - laravel

I am dealing with a table that has a bunch of short names, ex.
some_id
long_name
short_name
94
March
mar
19
April
apr
0
June
jun
2
September
Sep
I want to grab only a few supported short names, ex. mar, sep, jun.
How would I go about doing it in laravel?
Currently I have something like this:
$this->result = DB::table('table_above')->get();
But this just grabs every table. I was thinking of adding the where command, but not sure how to check for multiple values.
$this->result = DB::table('table_above')->where('short_name', [somehow say either mar, sep, jun])->get();
At the end I'd like an array that holds a dictionary of rows (i.e $result) with the supported short names (i.e here mar, june, sept).

Try this query
DB :: table ('table_above')->whereIn('short_name', ['mar', 'sep'])->get()

Related

Pivot Dynamic Data in Oracle

I have rollup output of month-wise average sales of products in a quarter as shown below:
Rollup output:
Product Month Sales
------------------------------
Product1 MAY 101.27
Product2 MAY 5.47
Product1 JUN 1481.19
Product2 JUN 84.95
ALL QTR 836.44
I need final output in the following format:
Product1 Product2 AverageSales
May 101.27 1481.19 (null)
Jun 5.47 84.95 (null)
Jul 0 0 (null)
ALL (null) (null) 836.44
I tried to apply pivot on month but since month name is a dynamic value based on the quarter selected, I tried to pivot on the product:
select * from (rollup output)
PIVOT MIN(Sales) FOR Product IN ('Product1' AS Product1_sales, 'Product2' AS Product2_sales, 'ALL' AS Average Sales');
I received the following output:
MONTH Product1_sales Product2_sales Average Sales
MAY 5.47 (null) (null)
MAY (null) 101.27 (null)
JUN 84.95 (null) (null)
JUN (null) 1481.19 (null)
Qtr (null) (null) 836.44
However, I need data in the final format mentioned above. How can I achieve this?
Also, if data for a given month of the quarter is not available, I want to display its value as 0 (also shown in the final format above). How can I achieve that, considering that month name is dynamic?
Edit: I can pivot using product as well since I do not want xml output that comes out of dynamic pivot.
I'm not sure why it needs to be dynamic since the number of months is unlikely to change. Why not just:
select product
, nvl(jan,0) as jan
, nvl(feb,0) as feb
, nvl(mar,0) as mar
, nvl(apr,0) as apr
, nvl(may,0) as may
, nvl(jun,0) as jun
, nvl(jul,0) as jul
, nvl(aug,0) as aug
, nvl(sep,0) as sep
, nvl(oct,0) as oct
, nvl(nov,0) as nov
, nvl(dec,0) as dec
from rollup_output
pivot (min(sales) for (month) in
( 'JAN' as jan, 'FEB' as feb, 'MAR' as mar, 'APR' as apr, 'MAY' as may, 'JUN' as jun
, 'JUL' as jul, 'AUG' as aug, 'SEP' as sep, 'OCT' as oct, 'NOV' as nov, 'DEC' as dec)
);
SQL Fiddle

oracle query to convert number format to date format or vice versa

In DB, date is stored in number format (Mon Jul 07 14:41:40 IST 2014
is stored as 1404724300383), I need a query to convert number to date to compare with the sysdate, or from sysdate to number format. How can we convert the date to number or vice versa?
Assume the number is a sequencial one based on specific rule. You need to know the rule first.
Get the rule
a) 07 July 2014 = 1404724300383; 07 July 2013 = ? (assume 1404700000000)
b) Assume value 0 mapping to one day, Day1
c) two formulas 1404724300383 = (07 July 2014 - Day1) * RULE; 1404700000000 = (07 July 2013 - Day1)*Rule;
Then, Rule = 24300383/365 and Day 1 = 1 Jan 1900 + 41285/(1404724300383/1404700000000*41460/((1404724300383/1404700000000)-1))
where 41825 is the days between 07 July 2014 and 1 Jan 1900)
41460 is the days between 07 July 2013 and 1 Jan 1900)
Use the Rule and Day1 to get what you need
select Day1 + [query column]/Rule from Your_Table;

Conversion of 01 to jan , 02 to feb including year in the field

I have a field containing 201402, 201404, here I want to convert 02 to Feb and 04 to April. Is it possible to do that? This field contains entries of all months. Please help.
There is a lot that you can do in terms of datetime format models within Oracle. A simple example below (replace the string literal within the TO_DATE(...) with your field if it is a string or replace the entire TO_DATE with your field if it is already of data type DATE):
SELECT TO_CHAR(TO_DATE('201402','YYYYMM'),'YYYY-MONTH') FROM DUAL;
SELECT TO_CHAR(TO_DATE('201404','YYYYMM'),'YYYY-MONTH') FROM DUAL;

Event Study (Extracting Dates in SAS)

I need to analyse abnormal returns for an event study on mergers and acquisitions.
** I would like to analyse abnormal returns to acquirers by using event windows. Basically I would like to extract the prices for the acquirers using -1 (the day before the announcement date), announcement date, and +1 (the day after the announcement date).**
I have two different datasets to extract information from.
The first is a dataset with all the merger and acquisition information that has the information in the following format:
DealNO AcquirerNO TargetNO AnnouncementDate
123 abcd Cfgg 22/12/2010
222 qwert cddfgf 26/12/1998
In addition, I have a 2nd dataset which has all the prices.
ISINnumber Date Price
abcd 21/12/2010 10
abcd 22/12/2010 11
abcd 23/12/2010 11
abcd 24/12/2010 12
qwert 20/12/1998 20
qwert 21/12/1998 20
qwert 22/12/1998 21
qwert 23/12/1998 21
qwert 24/12/1998 21
qwert 25/12/1998 22
qwert 26/12/1998 21
qwert 27/12/1998 23
ISIN number is the same as acquirer no, and that is the matching code.
In the end I would like to have a database something like this:
DealNO AcquirerNO TargetNO AnnouncementDate Acquirerprice(-1day) Acquireeprice(0day) Acquirerprice(+1day)
123 abcd Cfgg 22/12/2010 10 11 12
222 qwert cddfgf 26/12/1998 22 21 23
Do you know how I can get this?
I'd prefer to use sas to run the code, but if you are familiar with any other programs that can get the data like this, please let me know.
Thank you in advance ^_^.
This can be done quite easily with PROC SQL and joining the PRICE dataset three times. Try this (assuming data set names of ANNOUCE and PRICE):
Warning: untested code
%let day='21DEC2010'd;
proc sql;
create table RESULT as
select a.dealno,
a.acquirerno,
a.targetno,
a.annoucementdate,
p.price as acquirerprice_prev,
c.price as acquirerprice_cur,
n.price as acquirerprice_next
from ANNOUCE a
left join (select * from PRICE where date = &day-1) p on a.acquirerno = p.isinumber
left join (select * from PRICE where date = &day) c on a.acquirerno = c.isinumber
left join (select * from PRICE where date = &day+1) n on a.acquirerno = n.isinumber
;
quit;

Oracle - Date substraction in where clause

I'm trying to figure out how to compare the result of a date substraction in a where clause.
Clients subscribed to a service and therefore are linked to a subscription that has an end date. I want to display the list of subscriptions that will come to an end within the next 2 weeks.
I did not designed the databse but noticed that the End_Date column type is a varchar and not a date.. I can't change that.
My problem is the following:
If I try to select the result of the substraction for example with this request:
SELECT(TO_DATE(s.end_date,'YYYY-MM-DD') - TRUNC(SYSDATE)) , s.name
from SUBSCRIPTION s WHERE s.id_acces = 15
This will work and give me the number of days between the end of the subscription and the current date.
BUT now, if I try to include the exact same request in a clause where for comparison:
SELECT s.name
from SUBSCRIPTION S
WHERE (TO_DATE(s.end_date,'YYYY-MM-DD') - TRUNC(SYSDATE)) between 0 and 16
I will get an error: "ORA-01839 : date not valid for month specified".
Any help would be appreciated..
Somewhere in the table you have your date formatted in a different way from YYYY-MM-DD. In your first query you check a certain row (or a set of rows, s.id_acces = 15), which is probably ok, but in the second you scan through all the table.
Try finding these rows with something like,
select end_date from subscription
where not regexp_like(end_date, '[0-9]{4}-[0-9]{2}-[0-9]{2}')
Check your DD value (ie: day of the month). This value must be between 1 and the number of days in the month.
January - 1 to 31
February - 1 to 28 (1 to 29, if a leap year)
March - 1 to 31
April - 1 to 30
May - 1 to 31
June - 1 to 30
July - 1 to 31
August - 1 to 31
September - 1 to 30
October - 1 to 31
November - 1 to 30
December - 1 to 31
" the End_Date column type is a varchar and not a date.. I can't
change that."
If you can't change the date you'll have to chang3 the data. You can identify the rogue values with this function:
create or replace check_date_format (p_str in varchar2) return varchar2
is
d date;
begin
d := to_date(p_str,'YYYY-MM-DD');
return 'VALID';
exception
when others then
return 'INVALID';
end;
You can use this function in a query:
select sid, end_date
from SUBSCRIPTION
where check_date_format(end_date) != 'VALID';
Your choices are:
fix the data so all the dates are in the same format
fix the data and apply a check constraint to enforce future validity
write a bespoke MY_TO_DATE() function which takes a string and applies lots of different date format masks to it in the hope of getting a successful conversion.

Resources