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.
Related
i need some help with my query, basically im trying to get all my order_items on the table, but i need to get all items by user_id, but there is a detail, i want to include also records that includes on a column more than 3 times the the same email on different users_id.
Ex:
table:
- id;
- user_id;
- email;
1- Get all records from mine (user_id); 2 - Get all records where on 3 differentes user_id have the same email value;
Here is my query:
$orders = OrderItem::select('email','user_id')->where('user_id',Auth::user()->id)->distinct('email')->get();
This should work:
$orders = OrderItem::select(DB::raw('count(*) as email_count,email,user_id'))
->groupBy('email')
->where('email_count','>',2)
->orWhere('user_id',Auth::user()->id)
->get();
I have the following prices-table:
shop_id (int)
product_id (int)
price (float)
created (DateTime)
Every hour a cronjob checks the shops and inserts new entries (current prices) into these price-table.
Now I want to display the newest price for a product. I have to GROUP BY the shop_id because I only want one price per shop but I only want the newest entry (created).
Can I solve this with Eloquent Query-Builder or do I have to use raw SQL? Is it possible to pass the result of a raw SQL-query into a model if the columns are the same?
You can try it as:
Price::select('*', DB::raw('MAX(created_at) as max_created_at'))
->groupBy('shop_id')
->get()
Assuming model name is Price
Eloquent (purist) approach:
Price::orderBy('created', 'desc')->groupBy('shop_id')
->get('shop_id', 'price');
References:
https://laravel.com/api/5.3/Illuminate/Database/Query/Builder.html#method_orderBy
https://laravel.com/api/5.3/Illuminate/Database/Query/Builder.html#method_groupBy
https://laravel.com/api/5.3/Illuminate/Database/Query/Builder.html#method_get
*untested though
Q: Is it possible to pass the result of a raw SQL-query into a model if the columns are the same?
A: you could pass it to Model's contructor - but it might need model's field to be fillable - or hydrate a model. Alternatively, just access it like an keyed-array, ie. $something[0]['price'] <-- assuming an array of prices with price column.
I solved the problem without QueryBuilder. Instead I use a raw SQL-statement and generating the models with the hydrateRaw()-function of the Model-class.
$prices = Price::hydrateRaw( 'SELECT p.*
FROM prices p
INNER JOIN (
SELECT shop_id, max(created_at) AS max_ca
FROM prices p1
GROUP BY shop_id
) m ON p.shop_id = m.shop_id AND p.created_at = m.max_ca');
I have a Contact model which has many Notes. On one page of my app, I show several attributes of a table of contacts (name, email, latest note created_at).
For the note column, I'm trying to write a joins statement that grabs all contacts along with just their latest note (or even just the created_at of it
What I've come up with is incorrect as it limits and orders the contacts, not their notes:
current_user.contacts.joins(:notes).limit(1).order('created_at DESC')
If you just want the created_at value for the most recent note for each contact, you can first create a query to find the max value and then join with that query:
max_times = Note.group(:contact_id).select("contact_id, MAX(created_at) AS note_created_at").to_sql
current_user.contacts.select("contacts.*, note_created_at").joins("LEFT JOIN (#{max_times}) max_times ON contacts.id = max_times.contact_id")
If you want to work with the Note object for the most recent notes, one option would be to select the notes and group them by the contact_id. Then you can read them out of the hash as you work with each Contact.
max_times = Note.group(:contact_id).select("contact_id, MAX(created_at) AS note_created_at").to_sql
max_notes = Note.select("DISTINCT ON (notes.contact_id) notes.*").joins("INNER JOIN (#{max_times}) max_times ON notes.contact_id = max_times.contact_id AND notes.created_at = note_created_at").where(contact_id: current_user.contact_ids)
max_notes.group_by(&:contact_id)
This uses DISTINCT ON to drop dups in case two notes have exactly the same contact_id and created_at values. If you aren't using PostgreSQL you'll need another way to deal with dups.
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();
i'm in a situation my mind is blocked en hope someone can help me.
I have two tables. One table with customers of a subsription service and one invoice table.
these tables are not linked with keys in the database for keeping history of invoices if customers are deleted. This way I have to query the customers table joining the invoiceheader table by another unique contraint (not know by the database). This constraint is using name and address together.
An invoice is send one time a year. In the invoice-header table the date is stored when the invoice is created. In a couple of years constomers can have multiple invoices.
i'm trying to create a linq query but i'm looking the wrong way for a solution I'm afraid.
who can point me the right way?
for now i have a query :
var temp = from c in context.customer
from i in context.invoiceheader
where c.name + c.address == i.name + i.address
&& i.invoicedate < DateTime.Now.Year
select c;
With this query I get all customers who have receive an invoice last year and stil have subscribed. The trouble is with new customers who never received an invoice.
What to do for customers where in this case they haven't any invoice records.?
summurized: I want to query the last know invoice. If this invoice is older than a year (previous year)or no invoice is sent at al, i wanna retreive a list of customers the should be sent a new invoice.
I guess that what you want is a left outer join - this way you should be able to get all the customers you need:
var customers = from c in context.customer
join i in context.invoiceheader
on c.Name + c.Address equals i.Name + i.Address
into g
from row in g.DefaultIfEmpty()
where row == null ||row.invoicedate < DateTime.Now.Year
select c;