068 Assignment 11 Answers & Tips for writing complicated queries

SQL

select 
    C.Name as 'Category Name',
    SUM(ISNULL(OL.Quantity,0)) as 'Ordered Quantity',
    case
        when SUM(ISNULL(OL.Quantity,0)) between 1 and 9 then 'Low-demand'
        when SUM(ISNULL(OL.Quantity,0)) between 10 and 30 then 'Mid-demand'
        when SUM(ISNULL(OL.Quantity,0)) > 30 then 'High-demand'
        else 'No-demand'
    end as 'Demand Type'
from Categories C
    left join Products P on C.CategoryId = P.CategoryId
    left join OrderLines OL on P.ProductId = OL.ProductId
group by C.Name
order by C.Name

猜你喜欢

转载自blog.csdn.net/KevinHuang2088/article/details/143372879
068