Magento2: How to Split order based on product quantity - magento2

How to split order based on product quantity
I need to split and order on products quantity like if any product added to cart has two qty then two orders will be created and each thing like grand total, subtotal,shipping amount, tax , discount should be splited.
foreach ($quote->getAllVisibleItems() as $item) {}

Related

Orientdb SELECT and subqueries with aggregate

I have a generic class invoices with columns of dateTime, invoice, customer, amount. In a day there are 100's of invoices.
I want to write a query to get the invoice,customer whose amount is max for the day. So this would be report for a week with one daily record of the invoice which was the highest in that day.
Have tried something, but it yields no results.
SELECT FROM Invoices
LET $a = (
SELECT dateTime as maxdateTime, max(amount) as maxInvoice
FROM Invoices GROUP BY dateTime
)
where amount = maxAmount and dateTime = maxdateTime

Sum up the count

I have a table like this:
Count Product
100 apple
50 apple
20 orange
How can I select to get the sum of the count per product like this?
Count Product
150 apple
20 orange
Try Query using SUM(),
select SUM(count) as count,Product from #YourTable
group by Product
SUM() function returns the total sum of a numeric column
You should use Aggregate Functions :
select SUM(count) as Count, Product from Product
group by Product

select only when an item exists on a table 3 or more times postgres

I have 2 tables. SalesOrderDetail and SalesOrderHeader.
SalesOrderDetails contains SalesOrderID and ProductID columns.
SalesOrderHeader contains SalesOrderID and CustomerID.
I want to make a query that shows all the Customers who ordered 3 or more products with different ProductID and how many orders he made(with 3 or more different products). I know that a customer made an order of 3 or more products when the table SalesOrderDetail have his SalesOrderID number more than 3 or more times.
So the Customer with ID 29825 has ordered 12 different Products.
And here's my code:
SELECT "SalesOrderHeader"."CustomerID", count("SalesOrderDetail"."SalesOrderID") AS TotalOrders
FROM
public."SalesOrderHeader",
public."SalesOrderDetail"
WHERE
"SalesOrderHeader"."SalesOrderID" = "SalesOrderDetail"."SalesOrderID"
GROUP BY "SalesOrderHeader"."CustomerID"
HAVING count("SalesOrderDetail"."SalesOrderID") >= 3
Problem with this is that is shows the number of products he ordered but I want the total orders with 3 or more different products.
If you want the total orders with 3 or more products per customer, then use two levels of aggregation:
select soh."CustomerID", count(*) as NumOrders
from public."SalesOrderHeader" soh join
(select SalesOrderID, count(distinct ProductID) as numproducts
from public."SalesOrderDetail" sod
group by SalesOrderId
) sod
on sod."SalesOrderID" = soh."SalesOrderID"
where numproducts >= 3
group by soh."CustomerID"

Working on joining several tables to obtain a desired result

Need to join several tables which shows each customer and total_value of orders placed by it. Report the customer no and name for the customer and the total_value for the customer who placed the highest of orders. So far I have:
select customer.customer_num, customer.customer_name, price as total_value
from customer, orders, order_line, part
where customer.customer_num = orders.customer_num
and orders.order_num = order_line.order_num
and order_line.part_num = part.part_num;
This narrows it down, but now I need to answer the initial question, report on which customer had the greatest sum of price found in the part table.
I just cannot figure out where to place this syntax?
customer.customer_num > sum(part.price)
after joining the four tables.
select customer.customer_num, customer.customer_name, price as total_value
from customer
join order on
customer.customer_num = orders.customer_num
join order_line on
orders.order_num = order_line.order_num
join part on
order_line.part_num = part.part_num where customer.customer_num >sum(part.price);

SQL Server SUM GROUP BY Help

I have a table that contains an orderid, an inventoryid, and a quantity -- its a line items table. The database is SQL Server 2008.
What I need to know is how to write a SQL statement that returns the sums of quantities for that itemid at that order, not counting orders that have larger orderids than it. It must return the orderid, itemid, and total.
Any help? Thanks!
Guess:
SELECT
SUM(quantity) AS total, --"sums of quantities for that itemid at that order"
orderid, inventoryid --"It must return the orderid, itemid"
WHERE
orderid < (some larger order id value) --"not counting orders that have larger orderids"
GROUP BY
orderid, inventoryid