--------------------------------------------
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

解决方案 »

  1.   

    这样行吗?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
      

  2.   

    我现在没sql,回去试试看已经写好了测试数据,直接把结果贴出来吧  谢谢
      

  3.   

    楼主,我个人觉得:
    首先,#tb_color 是多余的,颜色约束你可以在#tb_car表里约束
    另外,#tb_car表和#tb_car_sale可以合并成一个表即变成以下几个表:
    --------------------------------------------
    create table #tb_car_typ( --车型表
    ID int primary key,
    Nam varchar(20))insert into #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_car( --车辆基本信息表
    CarID varchar(20) primary key,
    CarTypID int,
    Color varchar(10),
    SellDate DateTime)insert into #tb_car select '1001',1,'深红','2007-2-1'
    union all select '1002',5,'铂金','2007-3-27'
    union all select '1003',3,'皓白','2007-1-14'
    union all select '1004',1,'墨绿','2007-4-26'
    union all select '1005',4,'碳黑','2007-2-3'
    union all select '1006',3,'浅红','2007-2-16'
    union all select '1007',1,'深绿','2007-3-1'
      

  4.   

    然后根据#tb_car_typ的ID列与#tb_car表的CarTypID列关联
    用datepart(wk,SellDate)来判断SellDate是第几周
    代码略
      

  5.   

    你现在这个数据直接在EXCEL里用数据透视表,马上就出来了,版式还漂亮,呵呵