select 商品名称,max(价格) 价格 from 价格表 group by 商品名称

解决方案 »

  1.   

    或:select 商品名称,min(价格) 价格 from 价格表 group by 商品名称
      

  2.   

    select 商品名称,(select top 1 价格 from 价格表 where 商品名称=a.商品名称) 价格 from 价格表 a group by 商品名称或随机:select 商品名称,(select top 1 价格 from 价格表 where 商品名称=a.商品名称 order by newid()) 价格 from 价格表 a group by 商品名称
      

  3.   

    select 商品名称,
    (select top 1 价格 from 表 where 商品名称 = a.商品名称 order by newid()) as 价格
    from 表 a
      

  4.   

    第一条:select 商品名称,(select top 1 价格 from 价格表 where 商品名称=a.商品名称)    价格 from 价格表 a group by 商品名称最大值:select 商品名称,max(价格) 价格 from 价格表 group by 商品名称最小值:select 商品名称,min(价格) 价格 from 价格表 group by 商品名称平均值: select 商品名称,avg(价格) 价格 from 价格表 group by 商品名称
      

  5.   

    那如果我是两张表
    表a
    id  订单号   商品名称
    1    0001    f1
    2    0002    f2
    3    0003    f2表b
    id  商品名称  价格
    1    f1        1
    2    f1        2
    3    f1        2.5
    4    f2        2
    5    f2        3
    6    f3        1
    我要得到结果
     订单号  商品名称    价格
     0001     f1      (取1,2,2.5中的任意一个价格)
     0002     f2      (表中f2的任意一个价格2,或者3)
     0003     f2       (f3的任意一个价格)
    我该如何写在同一条SQL语句上!
      

  6.   

    SELECT a.商品名称,b.订单号,
    (SELECT TOP 1 价格 FROM 表 WHERE 商品名称 = a.商品名称 ORDER BY newid()) as 价格
    FROM 表a INNER JOIN  表b on a.id = b.id
      

  7.   

    select b.订单号,aa.* from (select 商品名称,max(价格) 价格 from 价格表 group by 商品名称) aa left join aa.商品名称=b.商品名称
      

  8.   

    --1.表b的第一个价格select 订单号,商品名称
    ,价格=(select top 1 价格 from 表b where 商品名称=a.商品名称)
    from 表a a
    --2.表b的最大价格
    select 订单号,商品名称
    ,价格=(select max(价格) from 表b where 商品名称=a.商品名称)
    from 表a a
    --3.表b的最小价格
    select 订单号,商品名称
    ,价格=(select min(价格) from 表b where 商品名称=a.商品名称)
    from 表a a
    --4.表b的随机一个价格
    select 订单号,商品名称
    ,价格=(select top 1 价格 from 表b where 商品名称=a.商品名称 order by newid())
    from 表a a
      

  9.   

    declare @表a table(id int,订单号 varchar(4),商品名称 varchar(2))
    insert into @表a
    select 1,'0001','f1'
    union all select 2,'0002','f2'
    union all select 3,'0003','f2'declare @表b table(id int,商品名称 varchar(2),价格 float)
    insert into @表b
    select 1,'f1',1
    union all select 2,'f1',2
    union all select 3,'f1',2.5
    union all select 4,'f2',2
    union all select 5,'f2',3
    union all select 6,'f3',1
    --1.表b的第一个价格
    select 订单号,商品名称
    ,价格=(select top 1 价格 from @表b where 商品名称=a.商品名称)
    from @表a a
    --2.表b的最大价格
    select 订单号,商品名称
    ,价格=(select max(价格) from @表b where 商品名称=a.商品名称)
    from @表a a
    --3.表b的最小价格
    select 订单号,商品名称
    ,价格=(select min(价格) from @表b where 商品名称=a.商品名称)
    from @表a a
    --4.表b的随机一个价格
    select 订单号,商品名称
    ,价格=(select top 1 价格 from @表b where 商品名称=a.商品名称 order by newid())
    from @表a a/*--测试结果--1.表b的第一个价格订单号  商品名称 价格   
    ---- ---- -------------
    0001 f1   1.0
    0002 f2   2.0
    0003 f2   2.0(所影响的行数为 3 行)
    --2.表b的最大价格订单号  商品名称 价格    
    ---- ---- --------------
    0001 f1   2.5
    0002 f2   3.0
    0003 f2   3.0(所影响的行数为 3 行)--3.表b的最小价格订单号  商品名称 价格     
    ---- ---- ---------------
    0001 f1   1.0
    0002 f2   2.0
    0003 f2   2.0(所影响的行数为 3 行)--4.表b的随机一个价格订单号  商品名称 价格    
    ---- ---- --------------
    0001 f1   2.0
    0002 f2   3.0
    0003 f2   3.0(所影响的行数为 3 行)
    --*/
      

  10.   

    select *,(select top 1 价格 from 表b where 商品名称=表a.商品名称) 商品名称 from 表a就可以了