Operation with dates in Grocery CRUD and Codeigniter - codeigniter-2

In table Employees I have columns: start_date(dd/mm/yyyy), end_date(dd/mm/yyyy) and period(dd/mm/yyyy).
I want to put calculation in column period = end_date - start_date.
I don't now haw to take data from columns start_date and end_date and write operation for column period. Please help me to solve my problem.

This is an SQL question IMO. But you tagged it as codeigniter so I guess you need CI code to go with this.
Anyway you can use DATEDIFF on your query. Like so.
$sql = "SELECT DATEDIFF(start_date,end_date) AS DiffDate";
Then fire it up on CI using query().
$qry = $this->db->query($sql);
return $qry->result();

Related

How can I write the below pure SQL statement in laravel without using DB:raw

I'm trying to show the total price according to month. I know how to retrieve the data with pure SQL statements but I don't know a way to apply it inside Laravel. And also I don't want to use DB::raw(). I need help!! Below is the pure SQL statement.
SELECT month(dt.created_at) as Month,SUM(dp.price) as Total_Price
FROM datapack_transactions dt
INNER JOIN datapack_packages dp ON dt.package_id=dp.id
GROUP BY month(dt.created_at);
Below is the result of the above pure SQL statement.
I want to use the Laravel Eloquent instead of using DB::raw().
Try this query:
$orders = datapack_transactions::select(
DB::raw('month(datapack_transactions.created_at) as Month'),
DB::raw("SUM(datapack_packages.price) as Total_Price")
)
->join('datapack_packages','datapack_packages.id','=','datapack_transactions.package_id')
->groupBy('datapack_transactions.created_at')
->get();
//Change model and columns as per yours

Get order between two status

I have a problem in getting the sum of time taken between an order
I can able the get the created time by the following code:
$collection = Mage::getResourceModel('sales/order_status_history_collection')
->addAttributeToSelect('created_at')
->addAttributeToFilter('status', array('eq'=>'complete'))
->load()
is there a way to get the sum of time taken between two status like sum(OrderInvoiced+OrderShipped) ?
Any help would be appreciated.
You can use the direct SQL queries method to get the time difference between invoice and shipment created.
Magento Direct SQL Queries
Invoice is saved under sales_flat_invoice and shipment are saved under sales_flat_shipment. There are 2 dates for each created_at and updated_at in both tables.
Here is the SQL query to get the difference between invoice and shipment based on created_at column
SELECT TIMEDIFF(ss.created_at,si.created_at) As TimeTaken
FROM `sales_flat_invoice` si
JOIN `sales_flat_shipment` ss
ON si.order_id=ss.order_id
WHERE si.order_id=1
To get the time difference for all the order just remove WHERE si.order_id=1 from the above query.

Running "exists" queries in Laravel query builder

I'm using MySQL and have a table of 9 million rows and would like to quickly check if a record (id) exists or not.
Based on some research it seems the fastest way is the following sql:
SELECT EXISTS(SELECT 1 FROM table1 WHERE id = 100)
Source: Best way to test if a row exists in a MySQL table
How can I write this using Laravel's query builder?
Use selectOne method of the Connection class:
$resultObj = DB::selectOne('select exists(select 1 from your_table where id=some_id) as `exists`');
$resultObj->exists; // 0 / 1;
see here http://laravel.com/docs/4.2/queries
Scroll down to Exists Statements, you will get what you need
DB::table('users')
->whereExists(function($query)
{
$query->select(DB::raw(1))
->from('table1')
->whereRaw("id = '100'");
})
->get();
This is an old question that was already answered, but I'll post my opinion - maybe it'll help someone down the road.
As mysql documentation suggests, EXISTS will still execute provided subquery. Using EXISTS is helpful when you need to have it as a part of a bigger query. But if you just want to check from your Laravel app if record exists, Eloquent provides simpler way to do this:
DB::table('table_name')->where('field_name', 'value')->exists();
this will execute query like
select count(*) as aggregate from `table_name` where `field_name` = 'value' limit 1
// this is kinda the same as your subquery for EXISTS
and will evaluate the result and return a true/false depending if record exists.
For me this way is also cleaner then the accepted answer, because it's not using raw queries.
Update
In laravel 5 the same statement will now execute
select exists(select * from `table_name` where `field_name` = 'value')
Which is exactly, what was asked for.

hive query using regular expression

hi i was looking for a way to query a hive table ( user_acc_detl )
where a column (ACC_DETAILS) data looks like below,
COUNTRY[0]_united staes~DATE[0]_6/10/2014~AMOUNT[0]_200~ID[0]_20140509065052159324~COUNTRY[1]_united kingdom~DATE[1]_6/17/2014~AMOUNT[1]_125~ID[1]_20140516075156389761~COUNTRY[2]_Canada~DATE[2]_6/26/2014~AMOUNT[2]_200~ID[2]_20140515094013444121~COUNTRY[3]_Mexico~DATE[3]_7/3/2014~AMOUNT[3]_1200~ID[3]_20140601000937914898
i can query the hive table by
select ACC_DETAILS["COUNTRY[0]"] as COUNTRY, ACC_DETAILS["DATE[0]"] as DATE, ACC_DETAILS["AMOUNT[0]"] as BILLAMOUNT, ACC_DETAILS["ID[0]"] as PAYMENTID
from user_acc_detl
the above query gives the data for country[0], date[0], amount[0], id[0] which is fine.
Question - all i need to query it using just country, date, amount....without specifying as country[0]...
Question - is there a regular expression way to modify the query accordingly. please help me.
A simple way to achieve this is to wrap your query in a view:
CREATE VIEW user_acc_detl_simple AS
SELECT ACC_DETAILS["COUNTRY[0]"] as COUNTRY
, ACC_DETAILS["DATE[0]"] as DATE
, ACC_DETAILS["AMOUNT[0]"] as BILLAMOUNT
, ACC_DETAILS["ID[0]"] as PAYMENTID
FROM user_acc_detl;
SELECT country, date, billamount, paymentid FROM user_acc_detl_simple;

select statement

my table looks like this:
If the field name contains cost or quantity for the same lineItemIds, I have to display the result as:
cost is changed from 8*1=8
(fromVal*fromVal) to 9*6=54
(toVal*toVal) for itemID 123.
any help will be appreciated.
SELECT tc.LINE_ITEM_ID ITEM_ID,
tc.FROMVAL COST_FROMVAL,
tq.FROMVAL QTY_FROMVAL,
(tc.FROMVAL*tq.FROMVAL) PROD_FROMVAL,
tc.TOVAL COST_TOVAL,
tq.TOVAL QTY_TOVAL,
(tc.TOVAL*tq.TOVAL) PROD_TOVAL,
FROM
(SELECT LINE_ITEM_ID,
FROMVAL,
TOVAL,
FROM table
WHERE FIELDNAME = 'cost') tc
JOIN (SELECT LINE_ITEM_ID,
FROMVAL,
TOVAL,
FROM table
WHERE FIELDNAME = 'quantity') tq
ON tc.LINE_ITEM_ID = tq.LINE_ITEM_ID
I would look into using product aggregate functions. You'll have to compile them yourself though, Oracle doesn't include them as system functions. http://radino.eu/2010/11/17/product-aggregate-function/
If it's just for this one case where cost or quantity are used, then you could also just use subqueries, or temporary transaction based tables.
I'd provide you with a query example, but unfortunately don't have an Oracle instance accessible presently.

Resources