I have 3 sql statements that I need to print to a html table.
1: selects all employee initials that has made an order between two dates.
2: sums up the count of each employee's orders between two dates.
3: sums up each employee's total amount of money spent between two dates.
All data is in a table: Orders(orderNr, itemNr, orderAmount, employee, date, price)
Now, I have to print all three into a table, and I am struggling to figure out how to loop through them correctly while having all the right resultsets.
The table should show: Employee - Amount of orders - Total price
Any help is greatly appreciated.
Try this
SELECT employee, COUNT(DISTINCT(orderNr)) AS orders_count, SUM(price) AS total_price FROM Orders WHERE
date BETWEEN '$startdate' AND '$enddate' GROUP BY employee
Related
I'm working on a panel to show all orders from our e-commerce site and the way I have the orders set up is to have a row for each order referring to the customer id.
I'm working on showing all open orders on our back-end however the rows are showing multiple rows for the same order if (so if the orderID is 18, and the order has 2 items ordered there a 2 rows all the with the orderID of 18).
I'll include some screenshots below so you have an idea of what's happening.
This is my sql statement to show all open orders:
function GetAllOpenOrders (){
global $conn;
$sql = "SELECT * FROM customer_orders LEFT JOIN ordered_products ON ordered_products.custOrderID=customer_orders.custOrderID WHERE customer_orders.orderOpen=1";
return $result = $conn->query($sql);
}
So again, I want to combine the orders with multiple products and display the total price in the open orders screen.
You need an aggregate query to display the total. Something like:
SELECT sum(prod.totalPrice) as TotalPrice
FROM customer_orders
LEFT JOIN ordered_products ON ordered_products.custOrderID=customer_orders.custOrderID
WHERE customer_orders.orderOpen=1
group by customer_orders.custOrderID
I don't have your database to test that query but that should point you in the right direction.
You need a sub-query where you SUM the order totals and then aggregate. You also can't add the "product ID" to your query otherwise it will by definition break it down into rows for each product ordered.
For example
select
customer_order_id,
sum(product_total_price) as order_total
from
(select
customer_order_id,
product_id,
product_total_price
from table
group by 1,2,3)
group by 1
If you're looking to show the names of the products ordered in your 1 row, I suggest using a CONCAT function for the product names (so it's all in one field) and then you'll have the total in 1 row.
We have a db that was converted by a third party from cube cart to prestashop. Mostly things went fine but recently found a couple thousand products that lack a discount reduction value.
I have found bits and pieces on how to do what we need but I just need help. This is my first foray into sql/php so bear with me and dont laugh too much.
Get a product id # from the product_id column in Table1
Get the base price amount from price column in Table1
Find every instance of the product id listed in Table2 (can be up to 3 tiers of discount for each product)and do the next steps
Get the discount price from the discount column in Table2 for each instance of that product.
Subtract price (table1) from discount (table2) to find the reduction amount.
Insert reduction amount into the reduction column in Table2.
Repeat this for every row in Table2
After research I learned enough to do it within the same table but this playing with values from two different tables my brain goes "Im outta here."
SELECT *, (price - discount) AS Sum FROM Table1
I found some examples but nothing crosses over close enough to my needs to work or my syntax is messing things up.
Even a nudge in the right direction would mean a lot.
select b.*, (b.price - a.discount) as sum
from table2 b
left join table1 a on b.product_id = a.product_id
So if I understand this correctly table1 has product Id, base price, and table2 has discount?
It looks like you want to take table 2 and just add a column that has price after discount on every row?
Let me know and I can try and guide you further....
I have 2 tables, student and family.
Half of the students come Sundays, the other half Saturdays
Some families have more than one child.
Families pay fees for the school. Some families because of some critearia have a discount.
"discount" is a field in 'family' table
In a report I need to calculate the sum of all the discounts of all families that take advantage of a discount (by day).
When I do so, if a family has 2 children, the result returns double of the discount, if 3 children 3 x discount....
My Query:
$select_total_discounts =
"SELECT SUM(Discount) AS total_discounts
FROM family, student
WHERE family.Family_ID=student.Family_ID
AND student.Day like '%$Day%' ;" ;
$query= mysql_query($select_total_discounts,$conn) ;
$row = mysql_fetch_assoc($query);
$total_discounts= $row['total_discounts'];
Thank you in advance
If I understood your question correctly, you want to calculate the discounts by family disregarding the amount of students it has. So it'd be like this (change the day value by your variable):
SELECT SUM(Discount) AS total_discounts
FROM family
WHERE family.Family_ID in (select student.Family_ID from student where student.Day like 'Monday')
So it sums the discounts of the families that have at least one student from the specified day. If it has one, two, three or more students, it will not affect the result.
Working SQLFiddle: http://sqlfiddle.com/#!2/9b9793/1
It is not really full answer, but I posted it here, so it would be readable.
I think you are looking for something like UNION:
SELECT sum(value) FROM ((SELECT value FROM table) UNION ALL (SELECT value FROM table2)) sq
UNION merges your tables vertically.
I have two tables: invoice and charges, with a one-to-many relationship. (simplified) invoice has: id, description, date as fields and charges has: id, id_invoice, price, qty, date as fields
For invoice generation I need the info from the invoice table where e.g. id=1 and also all the info from the charges table where id_invoice=1 (so one row from invoice and multiple rows from charges)
At the moment I've got two separate queries but I was wondering if it was possible to do this in a single query?
Thanks
You could just do a simple JOIN between the two tables joining on invoice.id=charges.id_invoice
The invoice fields would be returned identically for every charge on the same invoice but it'd allow you to fetch the data in a single query.
The query would look something like:
SELECT * FROM invoice i, charges c WHERE i.id=c.id_invoice ORDER BY i.id;
For your needs, a LEFT JOIN seems better. Ex:
SELECT * FROM invoice i LEFT JOIN charges c ON i.id=c.id_invoice ORDER BY i.id;
A nice illustration of SQL joins can be found here
For each item in the first table, there is a 'numberOf' field. The value of this field must have an identical number of rows in a related table. These are like reservation rows so multiple users can book the item at the same time. This syncronisation sometimes goes out and there are more rows than the 'numberOf' field variable, and vice versa.
So I want to display a table that outputs the 'numberOf' from the first table, and the amount of rows that correspond to it from the other table. They are linked by the Item ID. Hope this isn't too confusing. The query is output with a do while loop. Here is the query I have so far anyway:
$querySync = sprintf("SELECT
COUNT(reserve_id), item_id, details, numberOf
FROM
reservations
JOIN
items ON item_id = itemID_reserved
WHERE
itemID_reserved = 1 ");
So at the moment it counts the number of rows in the reservations table. It then joins the items table so I can display the description and numberOf etc. Of course at the moment it only outputs the item with ID 1. But I can't seem to get it to go though each item, check its numberOf, and compare it to the number of rows in reservations table.
The idea is to have it all on one column and at the end of the row print if it is out of sync etc. I then need to rebuild the rows in the reservations table to match the numberOf.
Sorry thats a long one!
SELECT COUNT(reserve_id), item_id, details, numberOf,
COUNT(reserve_id) > numberOf AS overbook
FROM items
LEFT JOIN
reservations
ON itemID_reserved = item_id
GROUP BY
item_id
It might be easier to just directly calculate which items are "out of sync":
select i.item_id
from reservations r JOIN items i on (i.item_id = r.itemID_reserved)
group by i.item_id
having count(r.itemID_reserved) > i.numberOf
I'm making some assumptions there about which tables have which fields, but it should be sufficiently illustrative.