日期:2014-05-19  浏览次数:20794 次

要求统计某一年(输入参数)中每个星期每款车型每种颜色的销售数量
--------------------------------------------
create   table   #tb_car_typ(   --车型表
ID   int   primary   key,
Nam   varchar(20))

insert   #tb_car_typ   select   1, '1.6LX '
union   all   select   2, '1.8GL(新) '
union   all   select   3, '1.6GL '
union   all   select   4, '1.7HRVW '
union   all   select   5, '1.6IEST '
--------------------------------------------
create   table   #tb_color(
ID   int   primary   key,
Nam   varchar(20))

insert   #tb_color   select   1, '黑 '
union   all   select   2, '红 '
union   all   select   3, '白 '
union   all   select   4, '绿 '
union   all   select   5, '金 '
--------------------------------------------
create   table   #tb_car(   --车辆基本信息表
CarID   varchar(20)   primary   key,
CarTypID   int,
Color   varchar(10))

insert   #tb_car   select   '1001 ',1, '深红 '
union   all   select   '1002 ',5, '铂金 '
union   all   select   '1003 ',3, '皓白 '
union   all   select   '1004 ',1, '墨绿 '
union   all   select   '1005 ',4, '碳黑 '
union   all   select   '1006 ',3, '浅红 '
union   all   select   '1007 ',1, '深绿 '
--------------------------------------------
create   table   #tb_car_sale(   --销车表
CarID   varchar(20)   ,
SellDate   DateTime)

insert   #tb_car_sale   select   '1001 ', '2007-2-1 '
union   all   select   '1002 ', '2007-3-27 '
union   all   select   '1003 ', '2007-1-14 '
union   all   select   '1004 ', '2007-4-26 '
union   all   select   '1005 ', '2007-2-3 '
union   all   select   '1006 ', '2007-2-16 '
union   all   select   '1007 ', '2007-3-1 '
--------------------------------------------

要求统计某一年(输入参数)中每个星期每款车型每种颜色的销售数量
说明:1.#tb_color里就是要统计的颜色;#tb_car中的颜色用like规则归类于#tb_color中的颜色
2.期望结果如下:(第一列是根据输入的年生成每个星期的时间段)
  时间段                                 1.6LX黑   1.6LX红     1.6LX白   1.6LX绿   1.6LX金     1.8GL(新)黑   1.8GL(新)红......  
2007-1-1~2007-1-7                
2007-1-8~2007-1-14              
2007-1-15~2007-1-21

------解决方案--------------------
这样行吗?

select 'color '=case j.Color
when '深红 ' then '红 '
when '铂金 ' then '金 '
when '皓白 ' then '白 '
when '墨绿 ' then '绿 '
when '碳黑 ' then '黑 '
when '浅红 ' then '红 '
when '深绿 ' then '绿 '
end ,datepart(ww,s.selldate) as d_week
from #tb_car_typ x ,#tb_car j,#tb_car_sale s
where j.CarTypID=x.id and s.carid=j.carid
order by datepart(ww,s.selldate),color
SqlServer生成Guid怎么格式化,在线求解~