从查询结果创建月份桶

时间:2015-10-09 21:59:57

标签: sql sorting group-by

我有以下查询结果:

enter image description here

但我想按编号对结果进行分组,并按月对数量进行分类,因此产生的结果将为:

enter image description here

如何创建月份存储桶以获取日期并将其设为列并相应地放置值。这是我到目前为止的尝试:

select converT(varchar,year(NewShipDate)) + 'M' + MonthBucket,
        *
from(

select ItemNumber, des.[Product Family], ItemDescription, des.Country, month(RequestedShipDate) as 'Month', ItemOrderedQuantity
from FS_COLine co
join FS_Item it on co.ItemKey = it.ItemKey
left outer join FIN_PLANCAP_2..ProductDescriptions des on it.ItemNumber collate database_default = des.RICNum
where RequestedShipDate >= convert(datetime, '01/01/2016', 120) and
      RequestedShipDate <= convert(datetime, '12/31/2016', 120) and
    isNumeric(it.ItemNumber) = 1 and 
    isNumeric(des.RICNum) = 1 
   --and it.ItemNumber = 35191305
group by ItemNumber, des.[Product Family], ItemDescription, des.Country, month(RequestedShipDate), ItemOrderedQuantity
order by ItemNumber
)A

0 个答案:

没有答案