Magento grabbing the top 10 buyers - magento

I've been searching through the Magento tables, what fields would I use to grab the top 10 buyers name and the total amount each they spent.
i've been looking in the flat_order table ...

If just having the total sales and the customer_id is enough:
SELECT customer_id,SUM(base_grand_total) AS total_sales
FROM sales_flat_order
GROUP BY customer_id
ORDER BY total_sales DESC
LIMIT 10;
If you need the customer information in the same result, you'll have to join in some of the customer_entity tables, but if you're just looking for a quick report, the above should give you the top ten.
P.S. If you do want to tie in customer data, start with the customer_entity table, and things like their name appear to be in the customer_entity_varchar table.

Related

Hi there, I'm using Oracle. I want to create a view where in this view, I display the rows of the same primary key and the total number of coin sales

There's a couple of tables that I need to use columns from in the select statement. the questions is: Create a View to display the employee id, first name and surname. In your query include the coin price and a 10% commission for the sales made by the employees.
the difficult part for me is that employees of the same employee number, can make multiple coin sales, so in the view, i need to be able to add all the coin sales together of each respective primary key (employee_id)
As you can see in this image, emp101 has sold two different coins, with the coin_id's of "7116" and "7112". In the view i want to be able to somehow tally each coin value that each employee_id has sold if that makes sense ?
There's multiple other tables, but there's too many to send, so i am just trying to get a general idea of how to do this. I understand the logistics of the question, i just dont know how to implement the answer with the correct syntax and methods etc...
Since this appears to be a homework question, here is a discussion of how to solve it:
You want to CREATE a VIEW and give the view a name (something like employee_commisions) and include 5 columns (employee_id, first_name, surname, coin_price and commission - or maybe only 4 columns if they want the combined price plus commission).
To get the values for that view, you want to SELECT ... FROM existing tables; however, your image does not include first_name, surname or a value for a coin so I am assuming that you will have an employees table and a coins table that you will need to INNER JOIN to the invoice table on their respective primary keys.
To get the total value for the coins, you want to aggregate the values and this would be done with the SUM aggregation function and, so that you get the value for each employee, you would need to GROUP BY the employee_id primary key. You will either need to include the other columns you are not aggregating by in the GROUP BY clause or apply an aggregation function to those columns such as MAX(surname).
The syntax for CREATE VIEW is here.
The syntax for SELECT is here.

Tables involved in storing order information for a certain product in magento

What are the Tables involved in storing the information of an order.
For example: Customer - product purchased - etc.
I found the product purchases status etc.(invoice and sales tables) but having hard time in linking it to the customer. Could not find who bought what ? Any suggestions
Thanks
The table sales_flat_order has a field customer_id referring to a row in the customer_entity table. But remember that some orders are guest orders, and they are not associated to a customer account. In those cases customer_id is null.
The products (line items) for an order are stored in sales_flat_order_item. There's an order_id linking it to the order and a product_id linking it to the product.

SQL Server - How to auto-delete "old" database records?

I have a database table which storing shop list for users. I wish to store only 12 shop list per user, means if currently user1 has 12 records in the table, once user1 create a new shop list, the 1st shop list (oldest) will be deleted and the new shop list will be stored.
The ShopList table consist of ShopListID (PK), UserID (FK) and a LastUpdatedDate will is updated by a trigger once user insert/delete any shoplist item belong to the shoplist.
I got no idea how to do this at all.. is it using trigger? or stored procedure? really need help here...
Appreciate any feedback.. Thanks...
You can do this via a trigger or a procedure. You can also in your service layer/ business ligic layer query for the count there upon a save and remove the old records as well. Im for the business logic approach as it's more testable and keeps business logic out of triggers or procedures , so my recommendation is a code based approach.
I'd personally change the select query to only select the top 12 so that will control what the user can see.
I'd then use a database job that runs on a schedule that deletes the ones that you don't want.
I have come across this problem recently and it really depends on your "archiving" strategy.
What I have done is that I created a stored procedure that selects the records to be archived element onwards for every user account (my requirement is very similar to yours in the sense that i have to select the 31st element onwards in a user account). I can also give you some code here if you think it will come in handy.
I have created an extra table called XXXX_archive which is a clone of the schema on your shopping_list table(s). This is to insert old, archived records there in case a user asks to retrieve his list in the future (this is obviously optional but would come in handy).
The stored procedure finds this records and inserts them in the XXXX_archive table and then deletes them from the XXXX. This runs on a nightly basis (or whenever you feel its necessary) through the SQL Server Agent.
The effect is that the 13th element is not deleted the moment that the user creates another shopping list but i think thats fine cause you are in charge of your archiving strategy and can describe it in your TOS.
Just thought I should write my experience here cause i sorted out this problem just days ago.
EDIT: My stored proc is as follows:
INSERT into shopping_lists_archive
SELECT *
FROM shopping_lists
WHERE id in (
select id
from (
SELECT ROW_NUMBER() OVER (
PARTITION BY user_ID
ORDER BY user_ID desc) AS RowNumber,
id, user_ID
FROM shopping_lists c
where c.user_ID in (select USER_ID from shopping_lists group by user_id having COUNT(1) > 12)
) t
where rownumber > 12
)
DELETE FROM shopping_lists
WHERE id in (
select id
from (
SELECT ROW_NUMBER() OVER (
PARTITION BY user_ID
ORDER BY user_ID desc) AS RowNumber,
id, user_ID
FROM shopping_lists c
where c.user_ID in (select USER_ID from shopping_lists group by user_id having COUNT(1) > 12)
) t
where rownumber > 12
)
There you go - it may be slightly different than what you need cause i m archiving based on a join between two tables and had to amend my original query to your requirement.

Magento Products Ordered Report

I'm struggling with the Magento report for "Products Ordered".
Is the Ordered Quantity affected by the date filter? Why do some of the products show as 0? Where does Magento keep the ordered_qty data? I am using Magento 1.4.0.1.
Magento keeps record of all items sold in sales_order_entity, which you can retrieve like this:
select * from sales_order_entity where
entity_type_id = (select entity_type_id from eav_entity_type where
entity_type_code = 'sales/order_item'
);
By grouping by entity_id and counting the result, you can get an accurate total of the quantity_sold for a date range. You can see how Magento does this in Mage_Reports_Model_Mysql4_Order_Collection. From the code, it appears that Magento does in fact respect the date parameter.
Just to be clear, this means that there is no single place where you can retrieve an "ordered_qty" number without more complex queries. The most obvious reason some products show as 0 is that they haven't sold anything.
In v1.4 and up, you should be able to query the sales_flat_order_item table and then group and sum on qty_ordered. e.g.
select product_id, sku, sum(`qty_ordered`) from sales_flat_order_item group by product_id
Cheers,
JD

Is an Index Organized Table appropriate here?

I recently was reading about Oracle Index Organized Tables (IOTs) but am not sure I quite understand WHEN to use them. So I have a small table:
create table categories
(
id VARCHAR2(36),
group VARCHAR2(100),
category VARCHAR2(100
)
create unique index (group, category, id) COMPRESS 2;
The id column is a foreign key from another table entries and my common query is:
select e.id, e.time, e.title from entries e, categories c where e.id=c.id AND e.group=? AND c.category=? ORDER by e.time
The entries table is indexed properly.
Both of these tables have millions (16M currently) of rows and currently this query really stinks (note: I have it wrapped in a pagination query also so I only get back the first 20, but for simplicity I omitted that).
Since I am basically indexing the entire table, does it make sense to create this table as an IOT?
EDIT by popular demand:
create table entries
(
id VARCHAR2(36),
time TIMESTAMP,
group VARCHAR2(100),
title VARCHAR2(500),
....
)
create index (group, time) compress 1;
My real question I dont think depends on this though. Basically if you have a table with few columns (3 in this example) and you are planning on putting a composite index on all three rows is there any reason not to use an IOT?
IOTs are great for a number of purposes, including this case where you're gonna have an index on all (or most) of the columns anyway - but the benefit only materialises if you don't have the extra index - the idea is that the table itself is an index, so put the columns in the order that you want the index to be in. In your case, you're accessing category by id, so it makes sense for that to be the first column. So effectively you've got an index on (id, group, category). I don't know why you'd want an additional index on (group, category, id).
Your query:
SELECT e.id, e.time, e.title
FROM entries e, categories c
WHERE e.id=c.id AND e.group=? AND c.category=?
ORDER by e.time
You're joining the tables by ID, but you have no index on entries.id - so the query is probably doing a hash or sort merge join. I wouldn't mind seeing a plan for what your system is doing now to confirm.
If you're doing a pagination query (i.e. only interested in a small number of rows) you want to get the first rows back as quick as possible; for this to happen you'll probably want a nested loop on entries, e.g.:
NESTED LOOPS
ACCESS TABLE BY ROWID - ENTRIES
INDEX RANGE SCAN - (index on ENTRIES.group,time)
ACCESS TABLE BY ROWID - CATEGORIES
INDEX RANGE SCAN - (index on CATEGORIES.ID)
Since the join to CATEGORIES is on ID, you'll want an index on ID; if you make it an IOT, and make ID the leading column, that might be sufficient.
The performance of the plan I've shown above will be dependent on how many rows match the given "group" - i.e. how selective an average "group" is.
Have you looked at dba-oracle.com, asktom.com, IOUG, another asktom.com?
There are penalties to pay for IOTs - e.g., poorer insert performance
Can you prototype it and compare performance?
Also, perhaps you might want to consider a hash cluster.
IOT's are a trade off. You are getting access performance for decreased insert/update performance. We typically use them for reference data that is batch loaded daily and not updated during the day. This is not to say it's the only way to use them, just how we use them.
Few things here:
You mention pagination - have you considered the first_rows hint?
Is that the order your index is in, with group as the first field? If so I'd consider moving ID to be the first column since that index will not be used.
foreign keys should have an index on the column. Consider addind an index on the foreign key (id column).
Are you sure it's not the ORDER BY causing slowness?
What version of Oracle are you using?
I ASSUME there is a primary key on table entries for field id, correct?
Why the WHERE condition does not include "c.group = e.group" ?
Try to:
Remove the order by condition
Change the index definition from "create unique index (group,
category, id)" to "create unique index (id, group, category)"
Reorganise table categories as an IOT on (group, category, id)
Reorganise table categories as an IOT on (id, group, category)
In each of the above case use EXPLAIN PLAN to review the cost

Resources