一个SQL查询统计问题,急求答案!
表1[Sell]
字段1:ProdID(产品ID)
字段2:SpecifId(规格ID),
字段3:Scalar(数量)
字段4:Date(日期yyyy-mm-dd格式)表2[Product]
字段1:ID(唯一序号)
字段2:ProductName(产品名称)
字段3:Specif(规格)如何做一个数据视图,内有
字段1:ID(序列号)
字段2:产品名
字段3:规格
字段4:1月
字段5:2月
字段6:3月
字段7:4月
字段8:5月
字段9:6月
字段10:7月
字段11:8月
字段12:9月
字段13:10月
字段14:11月
字段15:12月能对应不同产品的不同规格统计出每个月份的数字出来。
做出来的表应该反映为:产品名    规格    1月    2月    3月    4月    5月    6月    7月    8月    9月    10月    11月    12月
-----------------------------------------------------------------------------------------------------
A产品    a规格     20     59    70     120    163    189    200    233    258    300    310     350
B产品    b规格     18     34    67     114    159    234    267    282    329    368    420     456
C产品    c规格     ... ...
......
-----------------------------------------------------------------------------------------------------
记住:各产品不同规格的数字均为当月份多个记录的合计数,其中部分当月记录是没有的。
请哪位高手给予指教,谢谢

解决方案 »

  1.   

    select 产品名,规格,
            1月 = case when datepart(mm, date) = 1 then....
            2月 = case ..from ta
    group by   产品名,规格
      

  2.   

    select
    a.ProductName,
    a.Specif,
    year(b.Date) as Year,
    sum(case month(b.Date) when 1 then b.Scalar) as [1],
    sum(case month(b.Date) when 2 then b.Scalar) as [2],
    sum(case month(b.Date) when 3 then b.Scalar) as [3],
    sum(case month(b.Date) when 4 then b.Scalar) as [4],
    sum(case month(b.Date) when 5 then b.Scalar) as [5],
    sum(case month(b.Date) when 6 then b.Scalar) as [6],
    sum(case month(b.Date) when 7 then b.Scalar) as [7],
    sum(case month(b.Date) when 8 then b.Scalar) as [8],
    sum(case month(b.Date) when 9 then b.Scalar) as [9],
    sum(case month(b.Date) when 10 then b.Scalar) as [10],
    sum(case month(b.Date) when 11 then b.Scalar) as [11],
    sum(case month(b.Date) when 12 then b.Scalar) as [12]
    from
    Product a
    inner join
    Sell b
    on a.ID=b.ProdID and a.Specif=b.SpecifId
    group by
    a.ProductName, a.Specif, year(b.Date)
      

  3.   

    4楼,我看您的答复应该能解决我的问题,可是将您的答复复制到SQL查询分析器后,提示:“第 5 行: ')' 附近有语法错误。”,是错在哪里,请指明,谢谢!