I am developing an eCommerce website using Laravel 8. I write the following script for find out total price & total quantity under a single order number. From following script getting the ERROR where is the problem please help me.
*At first I write row mysql then i will convert laravel query Builder.
SELECT COUNT (total_price) as totaPrice, COUNT (productqty) as proQnty
FROM (SELECT DISTINCT order_id FROM orderDetails)
LEFT JOIN ordertbl
ON ordertbl.id = orderDetails.order_id;
I guess you want to sum the prices and quantities, so use SUM() aggregate function.
Also you should do a LEFT join of ordertbl to orderDetails and not the other way around:
SELECT ot.id,
SUM(od.total_price) AS totaPrice,
SUM(od.productqty) AS proQnty
FROM ordertbl ot LEFT JOIN orderDetails od
ON ot.id = od.order_id
WHERE ot.id = ?
GROUP BY ot.id;
Or, without a join:
SELECT SUM(total_price) AS totaPrice,
SUM(productqty) AS proQnty
FROM orderDetails
WHERE order_id = ?;
Replace ? with the id of the order that you want.
In Your raw in missing the tablename alis for the subquery ..
Your raw query should be
SELECT COUNT(total_price) as totaPrice, COUNT(productqty) as proQnty
FROM (
SELECT DISTINCT order_id FROM orderDetails
) T
LEFT JOIN ordertbl ON ordertbl.id = T.order_id;
Related
This problem has left me clueless. I am trying to use a Join in mysql and a subquery and I keep getting a syntax error.
The statement in question is
SELECT Customer.customer_id, Customer.name, Order.address FROM Customer
WHERE customer_id = (SELECT customer_id FROM Order WHERE customer_id = "625060836f7496e9fce3bbc6")
INNER JOIN Order ON Customer.customer_id=Order.customer_id;
I have tried to just use the query without the Subquery and it works fine.
SELECT Customer.customer_id, Customer.name, Order.address FROM Customer
INNER JOIN Order ON Customer.customer_id=Order.customer_id;
Removing the join but keeping the subquery also works.
SELECT Customer.customer_id, Customer.name, Order.address FROM Customer
WHERE customer_id = (SELECT customer_id FROM Order WHERE customer_id = "625060836f7496e9fce3bbc6")
Only using both the subquery and the join results in a syntax error
I cannot seem to find the error.
What have I done wrong here.
Thanks in advance
The secret is to get the syntax right!
When querying more than one table it's good practice to use aliases to reference them and where multiple tables share the same column names, also string literals should be delimited with single 'quotes'.
In this specific example however the subquery is superfluous, just use the string literal directly in the where clause.
SELECT c.customer_id, c.name, o.address
FROM Customer c
JOIN Order o ON c.customer_id = o.customer_id
WHERE c.customer_id = (
SELECT customer_id
FROM Order
WHERE customer_id = '625060836f7496e9fce3bbc6'
);
Good day everyone,
I have been attempting to convert the following SQL statement into a join clause, however I have been unable to figure out how to do it and was wondering if I could get some help.
Here is the statement:
SELECT OrderID FROM Orders
WHERE OrderDate = (SELECT orderDate FROM Orders WHERE OrderID = 10280);
I am essentially trying to get all orderIDs that were placed on the same date as the Order with OrderID 10280.
Thanks,
Try this:
SELECT o.OrderId
FROM Orders o
JOIN Orders ord
ON o.OrderDate = ord.OrderDate
WHERE ord.OrderID = 10280;
You can have a better understanding from here: https://www.sqlservertutorial.net/sql-server-basics/sql-server-self-join/#:~:text=A%20self%20join%20allows%20you,join%20or%20left%20join%20clause.
There are three tables
Inventory - Which contains the stock information
Order - Which contains the store information of an order
OrderParts - Which contains the Parts associated with the order
Inventory Table
Order Table
Order Parts Table
Below Expected Report needs to be generated
Query Tried to achieve the above result
SELECT
inventory.partNumber, inventory.storeId, SUM(parts.quantity) as orderedQuantity,
inventory.availableQuantity
FROM
(SELECT *, SUM(availableQuantity) AS availQty
FROM
inventory AS inventory
GROUP BY storeId , partNumber) AS inventory
LEFT JOIN
(SELECT
*
FROM
order
GROUP BY storeId) AS order ON order.storeId = inventory.storeId
LEFT JOIN
(SELECT
*
FROM
orderParts) AS parts ON inventory.partNumber = parts.partNumbe
where inventory.partNumber in ("A1234", "B1234");
GROUP BY order.storeId , parts.partNumber
Getting null for Ordered Quantity
Kindly let me know the way to get the cumulative Ordered quantity as expected. Thanks in Advance.
A relatively simple method is union all and aggregation:
select storeid, partnumber,
sum(availableqty) as availableqty,
sum(orderedqty) as orderedqty
from (select storeid, partnumber, availableqty, 0 as orderedqty
from inventory
union all
select o.storeid, op.partnumber, 0, op.quantity
from orderparts op join
orders o
on op.ordernumber = o.ordernumber
) sp
group by storeid, partnumber;
Your question doesn't explicitly describe the filters, but you can add filters in either the subqueries or the outer query.
I'm trying to create a view of some columns from 3 different tables. One of the columns 'OrderNumber' is in 2 of the tables so I'm trying to do a UNION for them, but because I've made a subquery it returns an 1242 error and won't return more than 1 row. I just want to know how I can rewrite this query so that there are no subqueries, or is there someway to bypass it. Or perhaps I need to write multiple queries? Though I'd prefer to keep it to the one query, thanks.
CREATE VIEW CustOrderItems AS
SELECT CustFirstName,
CustLastName,
(SELECT OrderNumber
FROM Orders
UNION
SELECT OrderNumber
FROM Order_Details)
OrderDate,
ShipDate,
QuantityOrdered * QuotedPrice as ItemTotal
FROM Customers JOIN Orders JOIN Order_Details;
Substitute whatever your customer id
drop view if exists custorders;
create view custorders as
SELECT c.CustFirstName,
c.CustLastName,
o.OrderNumber order_ordernumber,
od.OrderNumber orderdetails_ordernumber,
o.OrderDate,
o.ShipDate,
od.QuantityOrdered * od.QuotedPrice as ItemTotal
FROM Customers c
JOIN Orders o on c.id = o.cust_id
JOIN Order_Details od on o.ordernumber = od.ordernumber
where c.id = ?
It's not clear what are your join criteria because the statement syntax is bad. But I assume you want to join on OrderNumber like SELECT ... FROM Customers INNER JOIN Orders ON Customers.OrderNumber = Orders.OrderNumber. In this case, if you want to use order numbers from two tables just repeat the query and make union of the two like:
SELECT ,,, FROM Customers INNER JOIN Order_Details ON Customers.OrderNumber = Order_Details.OrderNumber
UNION
SELECT FROM Customers INNER JOIN Orders ON Customers.OrderNumber = Orders.OrderNumber
in this cust_id is a foreign key and ords returns the number of orders for every customers
SELECT cust_name, (
SELECT COUNT(*)
FROM Orders
WHERE Orders.cust_id = Customers.cust_id
) AS ords
FROM Customers
The output is correct but i want to filter it to retrieve only the customers with less than a given amount of orders, i don't know how to filter the subquery ords, i tried WHERE ords < 2 at the end of the code but it doesn't work and i've tried adding AND COUNT(*)<2 after the cust_id comparison but it doesn't work. I am using MySQL
Use the HAVING clause (and use a join instead of a subquery).....
SELECT Customers.cust_id, Customers.cust_name, COUNT(*) ords
FROM Orders, Customers
WHERE Orders.cust_id = Customers.cust_id
GROUP BY 1,2
HAVING COUNT(*)<2
If you want to include people with zero orders you change the join to an outer join.
There is no need for a correlated subquery here, because it calculates the value for each row which doesn't give a "good" performance. A better approach would be to use a regular query with joins, group by and having clause to apply your condition to groups.
Since your condition is to return only customers that have less than 2 orders, left join instead of inner join would be appropriate. It would return customers that have no orders as well (with 0 count).
select
cust_name, count(*)
from
customers c
left join orders o on c.cust_id = o.cust_id
group by cust_name
having count(*) < 2