Crossreferencing two SQL results, finding overlap? - php

I have a table of customer orders: inside are zero cost orders (things I have given away to customers) as well as paid orders. I want to find out how many customers who have received a free order have also purchased an order.
SELECT customer_id
FROM orders
WHERE total = 0
GROUP BY customer_id
SELECT customer_id
FROM orders
WHERE total != 0
GROUP BY customer_id
These two queries give me a list of customers who received a free order and a list of customers who paid for an order. I want to count the overlapping instances, for example:
//free orders
1,5,2,9,3,11,7
//paid orders
1,5,4,8,7,9,3,12,10,13
The intersect between these two sets is {1,5,9,3,7}, which is five element. I need a way to do this strictly using SQL preferred.

Just use a having clause. This is probably the simplest way, assuming total has no negative values:
SELECT customer_id
FROM orders
GROUP BY customer_id
HAVING MIN(total) = 0 AND MAX(total) > 0;

Try this (not tested, sorry)
SELECT count(distinct customer_id)
FROM orders a JOIN orders b
ON a.customer_id = b.customer_id
WHERE a.total = 0 and b.total <>0

Related

Mysql query with count on another table

I have two tables, one that has transaction information such as id, status, price and date. I have another table that stores the number of items in that order as individual rows. So, say transaction 2 has 10 items, there will be 10 rows in the second table of different items. What im trying to do is run a query that lists transactions and the number of items sold in that transaction. I imagine this would require a count on the second table, but im not entirely sure of how to do it. This is the basic layout of the database
transaction id, date, price, discount, status
items: id, transaction_id, item_name, email, date_ordered, hash
Thanks in advance for all the help.
Group by the columns in the transaction table you want to select. Then add a count of the items
select t.id, count(i.id) as item_count
from transaction t
left join items i on i.transaction_id = t.id
group by t.id
You can do a left join on both tables like below
select t.*, tab.total_order
from transaction t
left join
(
select transaction_id, count(*) as total_order
from items
group by transaction_id
) tab on t.id = tab.transaction_id

fast and slow moving products in mysql

Can anyone help me out about fast and slow moving products on MySQL?
I want to know how to SELECT CLAUSE all the fast moving products and the slow moving products seperately. Here are my tables.
**product**
productID
productname
price
**sales**
transactionID
productID
quantity
subtotal
**transaction**
transactionID
datetransact
I cut some of the columns to make it look simple.
FAST MOVING PRODUCTS is a product that have been sold often in a specific period of time.
SLOW MOVING PRODUCTS is a product that sell not so often and sit on the shelves on a long period of time.
You will want to group by product and select the min(datetransact) and max(datetransact). The difference of these two will give you the number of products sold and the timespan between the first and last sale date. Then you can divide these to get an average.
Updated to calculate on quantity sold.
select sum(sales.quantity) as productssold,
min(transaction.datetransact) as firstsale,
max(transaction.datetransact) as lastsale,
max(transaction.datetransact) - min(transaction.datetransact) as timespan,
sum(sales.quantity) / max(transaction.datetransact) - min(transaction.datetransact) as averagesold
from product
join sales on product.productid = sales.productid
join transaction on sales.transactionid = transaction.transactionid
group by product.productid
having averagesold >= 'desired value'
Scott's answer is good as far as it goes. First, you seem to be concerned about the quantity of the products sold not just the number of transactions containing the product. And, the question (which has perhaps been revised) is about a particular date range.
To get the answer for a particular range of dates, simply use a where clause or conditional aggregation. The following uses filtering and includes products with no sales:
select p.*, sum(s.quantity) as productssold,
sum(s.quantity) / datediff(#datelast, #datefirst)) as AvgPerDay
from product p left join
sales s
on p.productid = s.productid left join
transaction t
on s.transactionid = t.transactionid
where t.datetransact between #datefirst and #datelast
group by p.productid
order by AvgPerDay;
If you don't want products that never sold, simple change the left join back to inner joins.
The problem with the filtering approach is that some products may have had their first sale after beginning of your period. To handle this, you want to measure the average since the first sales date (or perhaps since some release date in the product table). This basically moves the date condition from the where clause to the having clause:
select p.*, sum(case when t.datetransact between #datefirst and #datelast then s.quantity else 0 end
) as productssold,
(sum(case when t.datetransact between #datefirst and #datelast then s.quantity else 0 end) /
datediff(#datelast, least(#datefirst, max(t.datetransact)))
) as AvgPerDay
from product p left join
sales s
on p.productid = s.productid left join
transaction t
on s.transactionid = t.transactionid
group by p.productid
order by AvgPerDay;

MySQL sum up numbers in a relational database

I'm making a web application to make customers order items for anything. For that I've made a MySQL database which has the following tables:
customers
orders
order-items
products
In the customers table is all the information about the person such as:
The customer ID, for the primary key and auto increment (id)
The first name (first_name)
The last name (last_name)
The email address (email_adress)
Information about the customer (customer_info)
Example:
In the orders table is all the specific information about it such as:
The order ID, for the primary key and auto increment (id)
Which customer ordered it, linked with id field from the customers table (customer_id)
Order information (order_info)
The location where the order needs to go to (location)
When the order was created (created)
Example:
In the order-items table are all the items which every customer ordered, this is being linked by the order-id from the previous table.
The ID, for primary key and auto increment, not used for any relation (id)
The order ID, used for which product is for which order. This is linked with the id field from the orders table (order_id)
The product ID, this is used for what product they ordered, this is linked with the id field from the products table. (product_id)
The amount of this product they ordered (quantity)
Example:
In the products table is all the information about the products:
The ID, for primary key and auto incrementing, This is linked with the product_id field from the order_items table (id)
The name of the product (name)
The description of the product (description)
The price of the product (price)
Example:
The problem
Bob ordered product_id 2, times 3. Which is the sandwich with beef with the price of 2.50, which we have to multiply by 3 because it has been ordered 3 times. Which is 7.50
Bob also ordered product_id 3, times 5. Which is the sandwich with chicken with the price of 3.00, which we have to multiply by 5 because it has been ordered 5 times. Which comes out on 15.00
Now I need to sum these up. Which is 15.00 + 7.50 = 22.50
The question
How do I get the product_id linked with the actual price of the product_id? which I can then multiply by the quantity.
And then sum up all those values with the same order_id
For the first order we get product 2 (Quantity 3) and product 3 (Quantity 5), which should add 2.503 + 3.005 = 22.50
You asked, "
How do i get the product_id linked with the actual price of the product_id? Which i can then multiply by the quantity... And then sum up all those values with the same order_id."
Like this:
SELECT OI.Order_ID, Sum(OI.Quantity * P.Price) Total_Price
FROM `order-items` OI
INNER JOIN Products P
on OI.Products_Id = P.ID
GROUP BY OI.Order_ID
Expected output for sample data in question:
ORDER_ID Total_price
1 22.50
This is why I asked about sample Output. I'm not sure what columns you were trying to return so I returned just the sum total for each order.
Now what this says
Return a row for each order showing the order ID and the total_price which is the sum of (quantity ordered * price all lines for that order.
This is accomplished by looking at the order-items table (in back tic's because I'm not sure if mySQL like's dashes (-) in names.). Joining these tables based on the product_Id between the ordered items table and the price table. and then group the results by the ordered item allowing the sum to aggregate all the rows for an order times the price of the item on that line of the order together.
You can build an intermediate table that has the totals for each order, then JOIN that with the orders table to get the customerID of that order, which you join with customers to get the customer name
SELECT C.FirstName, C.LastName, Totals.Order_ID, Totals.NetCost
FROM (SELECT OI.order_id, SUM(OI.quantity * P.price) as NetCost
FROM orderitems as OI INNER JOIN products as P ON OI.products_id = P.id
GROUP BY OI.order_id
) as Totals
INNER JOIN orders as O on O.ID = Totals.order_id
INNER JOIN customers as C on C.ID = O.customer_id
And the problem #xQbert is talking about is that if your customer places an order on Monday, you raise your price on Tuesday, and run this report on Wed, the report will show a total different from what the customer saw when he approved the order. A better design would be to store orderprice in orderitem, or at least the ordertotal in orders so subsequent price changes don't affect historic orders. The former is better in case a customer wants a refund on one item.
EDIT: I mean the problem #xQbert mentions in his comment, his answer came in ahead of mine but I missed it on the refresh
EDIT 2: More explanation, as requested by #Bas
It's easiest to think of this query from the inside and work outward, so let me start with getting the prices of items.
I want to link an item in orderitem with it's price, so JOIN orderitem (I give it an alias of OI) to products (I give an alias of P) on the product ID. Product ID is called products_id in OrderItem, but just ID in products.
Once I have that, I can multiply the price from Products by the quantity in OrderItems to get a cost for just that item
Because I want the total of everything for each order, I use a GROUP BY clause on this sub query, with the thing I'm grouping by being order_id. With a GROUP BY, you usually take the field or fields you are grouping by, and some "aggregate" fields where you take the sum, or max, or average, or whatever of them, and it's the sum or whatever for all rows that are in the same group.
So at this point we have defined the query inside the parenthesis. I named that Totals, and it acts just like a real table, but it goes away when your query is over.
The next step is to combine this "totals" table with your orders table so we can look up the customer ID, and then combine it with Customers so we can look up the customer name. I used aliases for Customer and Orders (C and O respectively), but they are not necessary. In fact, this could be rewritten most if not all of the aliases. I think it makes it more readable, but if they confuse you, you could use the following instead, it's the same. I think Totals is necessary, but maybe not even that.
SELECT FirstName, LastName, Totals.Order_ID, Totals.NetCost
FROM (SELECT order_id, SUM(quantity * price) as NetCost
FROM orderitems INNER JOIN products ON products_id = products.id
GROUP BY order_id
) as Totals
INNER JOIN orders on orders.ID = Totals.order_id
INNER JOIN customers on customers.ID = orders.customer_id
Hopefully this is clear, but if not, leave more comments and I'll further explain tonight or tomorrow.

Find New Buyers, and Returning Buyers from the Orders tables

I have an existing E-commerce database with the rather standard sales tables. The relevant tables are:
Orders table.
The fields are like:
OrderID, CustomerID, OrderDate, ...
Customers table.
CustoerID, CustomerFirstName, CustomerLastName, ...
I need to find two values, namely:
Total new buyers (within a certain time period *)
Basically, those are fist time buyers within a certain time period
Total returning buyers (within a certain time period *)
Basically, these are buyers who have bought before, prior to the time period
time period, we will provide as the inputs, such as within 1 week
My database is in MySQL.
Question:
What is the easiest and most efficient way to get the two totals?
1. Total new buyers
2. Total returning buyers
Do I need to write a program in PHP? Or I can simply use SQL statements to achieve this?
Thanks for any help.
This can be done purely in SQL:
Number of First time buyers:
SELECT
COUNT(DISTINCT CustomerID)
FROM Orders
WHERE OrderDate BETWEEN <startdate> AND <enddate>
/* Buyers with only one order record */
AND CustomerID IN (SELECT CustomerID FROM Orders GROUP BY CustomerID HAVING COUNT(*)=1)
Number of repeat buyers:
SELECT
COUNT(DISTINCT CustomerID)
FROM Orders
WHERE OrderDate BETWEEN <startdate> AND <enddate>
/* Buyers with more than one order record */
AND CustomerID IN (SELECT CustomerID FROM Orders GROUP BY CustomerID HAVING COUNT(*)>1)
Both! You have to write SQL statement that return data from the Database and call the statement from inside a PHP script to deal with it. The SQL statement make you able to retrive data and the PHP code make you able to menage and eventually presetn data on your web page (cenverting it into HTML language).
This is the common scenario, but if you need a more detailed procedure with code, use google and have a nice coding!
Total new buyers
SELECT c.*
FROM Customers c, Orders o
WHERE c.CustomerID = o.CustomerID
AND c.CustomerID not in(SELECT o1.CustomerID from Orders o1)
Total returning buyers
Total new buyers
SELECT c.*
FROM Customers c, Orders o
WHERE c.CustomerID = o.CustomerID
AND c.CustomerID in(SELECT o1.CustomerID from Orders o1)
you can add time frame to both queries

Complex SQL Query Help

Given a pretty standard set of related tables like Customers, Invoices and Line Items. I need to make a query like "Select all customers that have invoices that have 5 line items or more" or "Select all customers that have more than 2 invoices" or lastly "Select all customers that have line items totaling more than $100"
I'm doing this the hard way now (walking through all the records manually) and I know it's the most inefficient way but I don't know enough SQL to construct these queries. I'm using PHP5, MySQL and CakePHP 1.25.
Start by joining them all together:
select c.id
from customers c
left join invoices i on i.customer_id = c.id
left join lineitems li on li.invoice_id = i.id
group by c.id
To filter customers with more than 5 line items or more, add:
having count(li.id) >= 5
Filtering customers with two or more invoices is trickier, since we're joining the lineitems table. There may be multiple rows per invoice. So to count only unique invoices, we have to add distinct to the count, like:
having count(distinct i.id) >= 2
To filter customers with more than $100 in items, add:
having sum(li.cost) > 100
You can use math inside the sum, in case you're storing separate line item counts and prices:
having sum(li.itemcount * li.itemcost) > 100
For the first two you can use GROUP BY and HAVING COUNT(*)>4 and for the last one you can use SUM(field).
If you want SQL then please show your attempt.
Say you had:
customers.id
line_items.user_id
line_items.numItems
Query would look like:
SELECT * FROM customers
JOIN line_items
ON line_items.user_id WHERE line_items.numItems > 5
Your WHERE clause will change depending on what you wanted to return.
Haven't worked with SQL Joins in a while, but I think it's something like that.
Here is some help with the second one that should get you going:
SELECT customer_id, COUNT(*) AS num_invoices
FROM invoices
GROUP BY customer_id
HAVING COUNT(*) > 2

Categories