我正在尝试回答问题15 http://sqlzoo.net/wiki/AdventureWorks_hard_questions。
这个问题,“确定三个最重要的城市。显示针对城市的顶级产品类别的细分。”措辞有点差,但我把它看作是一份报告,显示总订单价值最高的3个城市,分为母产品类别和总数。
我最终得到了以下内容:
select * from
(
select * from
(
select city, sum(accessories) as accessories, sum(bikes) as bikes, sum(clothing) as clothing, sum(components) as components, sum(total) as total from
(
select address.city, decode(ppcat.name, 'Accessories', sod.unitprice*sod.orderqty) as accessories, decode(ppcat.name, 'Bikes', sod.unitprice*sod.orderqty) as bikes, decode(ppcat.name, 'Clothing', sod.unitprice*sod.orderqty) as clothing, decode(ppcat.name, 'Components', sod.unitprice*sod.orderqty) as components, sod.unitprice*sod.orderqty as total from salesorderheader soh
inner join address on (soh.shiptoaddressid = address.addressid or soh.shiptoaddressid = address.addressid)
inner join salesorderdetail sod on soh.salesorderid = sod.salesorderid
inner join productaw paw on sod.productid = paw.productid
inner join productcategory pcat on paw.productcategoryid = pcat.productcategoryid
inner join productcategory ppcat on pcat.parentproductcategoryid = ppcat.productcategoryid
)
group by city
)
order by total desc
) where rownum <= 3
问题是,在假想的场景中添加了额外的父产品类别,我的报告需要手动更新...我无法弄清楚是否可以使用不同的方法自动填充列标题母产品类别。
我不希望行具有重复的城市,或者使用在SQLzoo上下文中无效的sql(即,必须使用Oracle Developer或其他人)。