Hi @Lylyy
add discount to the final result set
You need to add discount
in the group by
list too.
And need total for each order id.
You could use Grouping Sets.
Check this query:
select order_id,t.product,d.discount
,SUM(price * d.discount) as total
from @temp t join @discount d on t.product=d.product
group by grouping sets((order_id,t.product,d.discount),(order_id))
order by order_id
Best regards,
Cosmog Hong
If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".
Note: Please follow the steps in our Documentation to enable e-mail notifications if you want to receive the related email notification for this thread.