有很多产品的不同价格放在一个表里 且每个产品 的每个价格都有一个日期
例如
 id  产品名称  产品价格   价格日期
 1     白糖        3      2010-1-1
 2     白糖        4      2010-1-2
 3     白糖        5      2010-1-3 4     砂糖        3      2010-1-1
 5     砂糖        5      2010-1-2
 6     砂糖        6      2010-1-3
 7     砂糖        7      2010-1-4 8     红糖        2      2010-1-1
 9     红糖        3      2010-1-2
 10    红糖        4      2010-1-3
 11    红糖        5      2010-1-4
 12    红糖        6      2010-1-5以上表中可以看出 不是每一天所有产品都有价格的
现在客户想同时列出三个产品的价格 如果有的产品在某一天没有价格就都不列出了,也就是三种产品的价格在某天都存在才列出来,如何实现呢?
列出的表格样式要如下样子的
 价格日期  白糖  红糖  砂糖 
2010-1-1     3    3     2
2010-1-2     4    3     2
2010-1-3     5    3     2能列出来吗???    

解决方案 »

  1.   

    select a.*
    from tab a,(
    select 价格日期 from tab
    where 产品名称 in ('白糖','红糖','砂糖')
    group by 价格日期
    having count(1)=3
    ) as t
    where a.价格日期 = t.价格日期这是选择记录的,行列转换论坛很多,自己写写吧
      

  2.   

    --> 测试数据: #tb
    if object_id('tempdb.dbo.#tb') is not null drop table #tb
    go 
    create table #tb (id int,产品名称 varchar(4),产品价格 int,价格日期 datetime)
    insert into #tb
    select 1,'白糖',3,'2010-1-1' union all
    select 2,'白糖',4,'2010-1-2' union all
    select 3,'白糖',5,'2010-1-3' union all
    select 4,'砂糖',3,'2010-1-1' union all
    select 5,'砂糖',5,'2010-1-2' union all
    select 6,'砂糖',6,'2010-1-3' union all
    select 7,'砂糖',7,'2010-1-4' union all
    select 8,'红糖',2,'2010-1-1' union all
    select 9,'红糖',3,'2010-1-2' union all
    select 10,'红糖',4,'2010-1-3' union all
    select 11,'红糖',5,'2010-1-4' union all
    select 12,'红糖',6,'2010-1-5'select * from 
    (
    select 价格日期,
    白糖=max(case when 产品名称='白糖' then 产品价格 end),
    红糖=max(case when 产品名称='红糖' then 产品价格 end), 
    砂糖=max(case when 产品名称='砂糖' then 产品价格 end)  
    from #tb
    group by 价格日期
    )t
    where 白糖+红糖+砂糖 is not null
    价格日期                    白糖          红糖          砂糖
    ----------------------- ----------- ----------- -----------
    2010-01-01 00:00:00.000 3           2           3
    2010-01-02 00:00:00.000 4           3           5
    2010-01-03 00:00:00.000 5           4           6 (3 row(s) affected)
      

  3.   

    应该加多个条件select a.*
    from tab a,(
    select 价格日期 from tab
    where 产品名称 in ('白糖','红糖','砂糖')
    group by 价格日期
    having count(1)=3
    ) as t
    where a.价格日期 = t.价格日期
    and a.产品名称 in ('白糖','红糖','砂糖')
      

  4.   


    declare @sql varchar(max)
    set @sql = 'select convert(varchar(7),价格日期,120)价格日期'
    select @sql = @sql + ',max(case 产品名称 when ''' + 产品名称 + ''' then 产品价格 else 0 end)[' + 产品名称 + ']'
    from (select distinct 产品名称 from tb)t
    select @sql = @sql + ' from tb group by convert(varchar(7),价格日期,120)'
    exec(@sql)
      

  5.   


    create table tb (id int,产品名称 varchar(4),产品价格 int,价格日期 datetime)
    insert into tb
    select 1,'白糖',3,'2010-1-1' union all
    select 2,'白糖',4,'2010-1-2' union all
    select 3,'白糖',5,'2010-1-3' union all
    select 4,'砂糖',3,'2010-1-1' union all
    select 5,'砂糖',5,'2010-1-2' union all
    select 6,'砂糖',6,'2010-1-3' union all
    select 7,'砂糖',7,'2010-1-4' union all
    select 8,'红糖',2,'2010-1-1' union all
    select 9,'红糖',3,'2010-1-2' union all
    select 10,'红糖',4,'2010-1-3' union all
    select 11,'红糖',5,'2010-1-4' union all
    select 12,'红糖',6,'2010-1-5'
    godeclare @sql varchar(max)
    set @sql = 'select convert(varchar(10),价格日期,120)价格日期'
    select @sql = @sql + ',max(case 产品名称 when ''' + 产品名称 + ''' then 产品价格 else 0 end)[' + 产品名称 + ']'
    from (select distinct 产品名称 from tb)t
    select @sql = @sql + ' from tb group by convert(varchar(10),价格日期,120)'
    exec(@sql)drop table tb/*价格日期       白糖          红糖          砂糖
    ---------- ----------- ----------- -----------
    2010-01-01 3           2           3
    2010-01-02 4           3           5
    2010-01-03 5           4           6
    2010-01-04 0           5           7
    2010-01-05 0           6           0(5 行受影响)
      

  6.   

    CREATE TABLE #temp
    (
    id INT,
    产品名称 NVARCHAR(100),
    产品价格 INT,
    价格日期 DATETIME
    )
    INSERT #temp
    select '1', N'白糖', '3', '2010-1-1' union all
    select '2', N'白糖', '4', '2010-1-2' union all
    select '3', N'白糖', '5', '2010-1-3' union all
    select '4', N'砂糖', '3', '2010-1-1' union all
    select '5', N'砂糖', '5', '2010-1-2' union all
    select '6', N'砂糖', '6', '2010-1-3' union all
    select '7', N'砂糖', '7', '2010-1-4' union all
    select '8', N'红糖', '2', '2010-1-1' union all
    select '9', N'红糖', '3', '2010-1-2' union all
    select '10', N'红糖', '4', '2010-1-3' union all
    select '11', N'红糖', '5', '2010-1-4' union all
    select '12', N'红糖', '6', '2010-1-5'SELECT * FROM #temp
    --SQL:
    SELECT * FROM
    (
    SELECT b.价格日期, b.产品名称, 产品价格=MAX(产品价格) FROM 
    (SELECT 价格日期 FROM #temp GROUP BY 价格日期 HAVING COUNT(DISTINCT 产品名称) = 3) a
    CROSS APPLY
    (SELECT * FROM #temp WHERE 价格日期 = a.价格日期) b
    GROUP BY b.价格日期, b.产品名称
    ) m
    PIVOT
    (MAX(产品价格) FOR 产品名称 IN ([白糖], [红糖], [砂糖])) n
    /*
    价格日期 白糖 红糖 砂糖
    2010-01-01 00:00:00.000 3 2 3
    2010-01-02 00:00:00.000 4 3 5
    2010-01-03 00:00:00.000 5 4 6
    */
      

  7.   


    --建表
    create table #Price(ID int, Pname varchar(20), price int, PDate varchar(20))
    --数据
    insert into #Price
    select '1','白糖','3','2010-1-1'
    union all select '2','白糖','4','2010-1-2'
    union all select '3','白糖','5','2010-1-3'
    union all select '4','砂糖','3','2010-1-1'
    union all select '5','砂糖','5','2010-1-2'
    union all select '6','砂糖','6','2010-1-3'
    union all select '7','砂糖','7','2010-1-4'
    union all select '8','红糖','2','2010-1-1'
    union all select '9','红糖','3','2010-1-2'
    union all select '10','红糖','4','2010-1-3'
    union all select '11','红糖','5','2010-1-4'
    union all select '12','红糖','6','2010-1-5'--显示
    select Pdate,max(case pname when '白糖' then Price end) as [白糖],
    max(case pname when '红糖' then Price end) as [红糖],max(case pname when '砂糖' then Price end) as [砂糖]
    from #Price where Pdate in
    (
    select Pdate from #Price
    group by Pdate having count(0)>2
    ) group by Pdate
    --结果
    Pdate                白糖          红糖          砂糖
    -------------------- ----------- ----------- -----------
    2010-1-1             3           2           3
    2010-1-2             4           3           5
    2010-1-3             5           4           6