Answer the question
In order to leave comments, you need to log in
How to write functions?
CREATE FUNCTION select_orders_by_item_name (@string nvarchar(max))
returns Table
AS
RETURN
(
select distinct Orders.row_id,Customers.name from orders,orderitems,Customers
where [email protected]string and OrderItems.order_id=Orders.row_id
and Customers.row_id=Orders.customer_id
);
CREATE FUNCTION calculate_total_price_for_orders_group(@f int)
RETURNS FLOAT
AS
BEGIN
DECLARE @s int
SET @s=(SELECT Sum(OrderItems.price)
FROM orderitems INNER JOIN orders
ON orders.row_id = OrderItems.order_id
GROUP BY Orders.row_id
HAVING [email protected])
RETURN (@s)
END
Answer the question
In order to leave comments, you need to log in
We use CTEs:
with cte_orders(row_id, parent_id, group_name) as (
select row_id, parent_id, group_name from Orders where row_id = @f
union all
select O.row_id, O.parent_id, O.group_name
from Orders O
join cte_orders on cte_orders.row_id = O.parent_id
)
select sum(price) total_pice from cte_orders
left join OrderItems on cte_orders.row_id = OrderItems.order_id ;
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question