I have two tables named order and orderdetail with a relation on key order_id
I want to query the database to fetch a grand total per order.
The order table has fields order_id, customer_name etc.
The order_detail table has fields id (primary key), order_number (foreign key), product, qty, price
I would like a result with fields like this:
order_id, grand_total.
I created a view for order_detail like so:
CREATE OR REPLACE VIEW orderTotal AS
SELECT
order_number,
Round(sum(qty * price),2) as grandTotal
FROM order_detail t group by order_number;
It works pretty well, but there is an issue. Some orders has no reference data in order_detail, and then that record is not listed by this view. How can I solve this? Please give me a sample query.
It may need multiple SQL queries, but can we build it as a view?
As an example, I need data like this:
order_id grandTotal
1 200
2 0 // when data is missing
3 150.35
If you have parent records and no children, it could help you:
SELECT
order.order_id,
coalesce(Round(sum(order_detail.qty * order_detail.price),2),0) as grandTotal
FROM order
LEFT join order_detail on order_detail.order_number = order.order_id
GROUP BY order.order_id
Related
Write an SQL statement to show the warehouse information for warehouses which do not have all SKU items in inventory
I tried this query but results: NULL
SELECT SKU
FROM SKU_DATA
WHERE NOT EXISTS
(SELECT 1 FROM INVENTORY
JOIN WAREHOUSE ON INVENTORY.WAREHOUSEID = WAREHOUSE.WAREHOUSEID
WHERE SKU_DATA.SKU =INVENTORY.SKU);
Here the different tables available
Inventory (WarehouseID,SKU,SKU_Description,QuantityOnHand,QuantityOnOrder)
Order_Item(OrderNumber,SKU,Quantity,Price,ExtendedPrice)
Retail_order(OrderNumber,StoreNumber,StoreZip,OrderMonth,OrderYear,OrderTotal)
SKU_Data(SKU,SKU_Description,Department,Buyer)
Warehouse(WarehouseID,WarehouseCity,WarehouseState,Manager,SquareFeet)
enter image description here
Probably the easiest way will be just to count the number of unique SKUs in the SKU_Data, then select all the WarehouseID from the Inventory that have less than that number of unique SKUs.
Critically, this assumes a warehouse doesn't have a SKU that is missing from the SKU table.
Also assuming Inventory(WarehouseID,SKU) & SKU_Data(SKU) are unique
select WarehouseID from Inventory group by SKU
having count(*) < (select count(*) from SKU_Data)
I've not tested this, but I think it's about right :)
If this doesn't work, you can always select the count(*) into a local variable, something like
select #skunum := count(*) from SKU_Data
select WarehouseID from Inventory group by SKU having count(*) > #skunum
If you want to also check the warehouse has the item in stock, just add where QuantityOnHand > 0, after from Inventory
I'm trying to join 3 tables in BigQuery that contain sales for products with the same identifier (SKU) and attending to the order date.
My goal would be to have in one table the order date, Sales Table 1, Sales Table 2, Sales Table 3 and Product identifier (SKU). For that I've been trying the next query:
SELECT
Order_Date, Sales1, Sales2, Sales3, SKU
FROM
Table 1
LEFT JOIN
Table 2 ON Order_Date_Table1 = Order_Date_Table2 AND SKU_Table1 = SKU_Table2
LEFT JOIN
Table 3 ON Order_Date_Table1 = Order_Date_Table3 AND SKU_Table1 = SKU_Table3
However when 2 of the tables contain sales for the same SKU and the same day, I get duplicated rows. Something similar happens with all other join types.
How can I avoid this?
It seems to me that aggregation can be a solution for your problem:
SELECT Order_Date, SKU, SUM(Sales1), SUM(Sales2), SUM(Sales3)
...
GROUP BY Order_Date, SKU
I have two tables:
Orders
Orders product
I currently have the query that counts the amounts of orders:
SELECT COUNT(*) as total FROM orders WHERE DATE(`created_on`) = CURDATE()
This gives me the amount of orders from today
Now I want to change this query, so insted of that I count the orders I count the amount of products that they ordered.
The order_products table is like this:
-id
-order_id
-product_name
-product_weight
etc etc
I am only interested in the order_id because that links this table to the orders table. For every product that is ordered, 1 row is added in this table linked to the order with the order_id.
Is it possible to have one single query to select the count of that, or is that not possible? I think I have to use a LEFT JOIN for this operation, but I cannot seem to find it.
You just need to create inner join between those table and applying the relation throw foreign key then you need to add group by condition on what you are looking for
SELECT COUNT(*) as total FROM orders o inner join order_product op on(op.order_id=o.order_id) WHERE DATE(`created_on`) = CURDATE() group by op.id
I am quit new to MySQL.
I have 2 tables:
Table 1: client_debts (id, client_name, total_debts)
Table 2: client_details (id, client_id, payments)
Where client_id is the foreign key of table 2 and is connected to table 1 id column.
What I need to do is, to create an PHP-HTML (No problem with this part) table that gives me the total debts of a client, with the total of his payments to compare how much he still have. IF his payments are equal, I want to remove his name from the HTML table.
What I tried ?
I tried this SQL query:
SELECT (SELECT sum(total_debts) FROM client_debts) AS 'TOTAL',
(SELECT sum(payments) FROM client_details) AS 'Total Payed'
FROM client_debts INNER JOIN client_details
ON client_debts.id = client_details.client_id
And I have got this:
And the results are only for the first id and it is duplicated to all rows.
So how can I do this for every client?
Try This Query
SELECT client_debts.id,sum(total_debts) AS 'TOTAL',
sum(payments) AS 'Total Payed'
FROM client_debts
INNER JOIN client_details
ON client_debts.id = client_details.client_id
GROUP BY client_debts.id;
I hope this will work and in this query get all client id and its SUM.
Let's say I have these two tables: Product and Storage
Product has a ProductId, and a QuantityAvailable for each record
Storage has a LocationId, ProductId and QuantityAvailable for each record (ProductId is a FK to the product table). A 'ProductId' can be stored in multiple 'LocationId's, therefore ProductId and LocationId make up a compound primary key.
What I want is an SQL query that is able to determine if a 'Product' is in storage, and if so, if the 'Quantity' in cumulative storage is less than the 'QuantityAvailable'.
To put things simply, I want a listing of all the 'ProductId's in the Product table where the sum of the 'Quantity' of all the associated entries in the Storage table is less than the 'QuantityAvailable' of the Product Table.
How can I achieve this (without changing the table structure)?
thanks
The following query will give you the sum of the quantities for each product:
select ProductID, sum(Quantity) as ProductQuantity
from
Storage
group by
ProductID
Now you simply need to use that query as a derived table, join to it, and compare your 2 quantities:
select
*
from
Product p
inner join (
select ProductID, sum(Quantity) as ProductQuantity
from
Storage
group by
ProductID) q
on p.ProductID = q.ProductID
where
q.ProductQuantity < p.QuantityAvailable