SELECT t.state, t.stor_name, SUM(s.qty) AS TotalSales
FROM sales s JOIN stores t ON (s.stor_id=t.stor_id)
GROUP BY t.state, t.stor_name
ORDER BY TotalSales DESC, t.state, t.stor_name结果如下:
state   stor_name                               TotalSales
WA Doc-U-Mat: Quality Laundry and Books 130
CA Barnum's                         125
CA News & Brews                         90
OR Bookbeat                         80
CA Fricative Bookshop                 60
WA Eric the Read Books                 8但我想得到每个state中TotalSales最多的stor_name(将以上state列去重),该如何修改上面的sql?

解决方案 »

  1.   

    --按某一字段分组取最大(小)值所在行的数据
    --2007-10-23于杭州
    /*
    数据如下:
    name val memo
    a    2   a2(a的第二个值)
    a    1   a1--a的第一个值
    a    3   a3:a的第三个值
    b    1   b1--b的第一个值
    b    3   b3:b的第三个值
    b    2   b2b2b2b2
    b    4   b4b4
    b    5   b5b5b5b5b5
    */
    --创建表并插入数据:
    create table tb(name varchar(10),val int,memo varchar(20))
    insert into tb values('a',    2,   'a2(a的第二个值)')
    insert into tb values('a',    1,   'a1--a的第一个值')
    insert into tb values('a',    3,   'a3:a的第三个值')
    insert into tb values('b',    1,   'b1--b的第一个值')
    insert into tb values('b',    3,   'b3:b的第三个值')
    insert into tb values('b',    2,   'b2b2b2b2')
    insert into tb values('b',    4,   'b4b4')
    insert into tb values('b',    5,   'b5b5b5b5b5')
    go--一、按name分组取val最大的值所在行的数据。
    --方法1:
    select a.* from tb a where val = (select max(val) from tb where name = a.name) order by a.name
    --方法2:
    select a.* from tb a where not exists(select 1 from tb where name = a.name and val > a.val)
    --方法3:
    select a.* from tb a,(select name,max(val) val from tb group by name) b where a.name = b.name and a.val = b.val order by a.name
    --方法4:
    select a.* from tb a inner join (select name , max(val) val from tb group by name) b on a.name = b.name and a.val = b.val order by a.name
    --方法5
    select a.* from tb a where 1 > (select count(*) from tb where name = a.name and val > a.val ) order by a.name
    /*
    name       val         memo                 
    ---------- ----------- -------------------- 
    a          3           a3:a的第三个值
    b          5           b5b5b5b5b5
    */--二、按name分组取val最小的值所在行的数据。
    --方法1:
    select a.* from tb a where val = (select min(val) from tb where name = a.name) order by a.name
    --方法2:
    select a.* from tb a where not exists(select 1 from tb where name = a.name and val < a.val)
    --方法3:
    select a.* from tb a,(select name,min(val) val from tb group by name) b where a.name = b.name and a.val = b.val order by a.name
    --方法4:
    select a.* from tb a inner join (select name , min(val) val from tb group by name) b on a.name = b.name and a.val = b.val order by a.name
    --方法5
    select a.* from tb a where 1 > (select count(*) from tb where name = a.name and val < a.val) order by a.name
    /*
    name       val         memo                 
    ---------- ----------- -------------------- 
    a          1           a1--a的第一个值
    b          1           b1--b的第一个值
    */--三、按name分组取第一次出现的行所在的数据。
    select a.* from tb a where val = (select top 1 val from tb where name = a.name) order by a.name
    /*
    name       val         memo                 
    ---------- ----------- -------------------- 
    a          2           a2(a的第二个值)
    b          1           b1--b的第一个值
    */--四、按name分组随机取一条数据。
    select a.* from tb a where val = (select top 1 val from tb where name = a.name order by newid()) order by a.name
    /*
    name       val         memo                 
    ---------- ----------- -------------------- 
    a          1           a1--a的第一个值
    b          5           b5b5b5b5b5
    */--五、按name分组取最小的两个(N个)val
    select a.* from tb a where 2 > (select count(*) from tb where name = a.name and val < a.val ) order by a.name,a.val
    select a.* from tb a where val in (select top 2 val from tb where name=a.name order by val) order by a.name,a.val
    select a.* from tb a where exists (select count(*) from tb where name = a.name and val < a.val having Count(*) < 2) order by a.name
    /*
    name       val         memo                 
    ---------- ----------- -------------------- 
    a          1           a1--a的第一个值
    a          2           a2(a的第二个值)
    b          1           b1--b的第一个值
    b          2           b2b2b2b2
    */--六、按name分组取最大的两个(N个)val
    select a.* from tb a where 2 > (select count(*) from tb where name = a.name and val > a.val ) order by a.name,a.val
    select a.* from tb a where val in (select top 2 val from tb where name=a.name order by val desc) order by a.name,a.val
    select a.* from tb a where exists (select count(*) from tb where name = a.name and val > a.val having Count(*) < 2) order by a.name
    /*
    name       val         memo                 
    ---------- ----------- -------------------- 
    a          2           a2(a的第二个值)
    a          3           a3:a的第三个值
    b          4           b4b4
    b          5           b5b5b5b5b5
    */
      

  2.   

    谢谢楼上的大侠!可是我的SQL含聚合函数sum,而且有join,用举例的方法似乎不可行。请指教
      

  3.   

    with cte as
    (
    SELECT t.state, t.stor_name, SUM(s.qty) AS TotalSales, row_number() over(partition by state order by SUM(s.qty) desc) as rowNum
    FROM sales s JOIN stores t ON (s.stor_id=t.stor_id)
    GROUP BY t.state, t.stor_name
    )select state, stor_name, TotalSales
    from cte
    where rowNum = 1
    order by TotalSales desc
      

  4.   

    有语法错误:服务器: 消息 156,级别 15,状态 1,行 1
    在关键字 'with' 附近有语法错误。
    服务器: 消息 195,级别 15,状态 1,行 3
    'row_number' 不是可以识别的 函数名。
      

  5.   

    刚找到语句:SELECT s.state, st.stor_name,s.totalsales,rank=COUNT(*)
    FROM (SELECT t.state, t.stor_id, SUM(s.qty) AS TotalSales
    FROM sales s JOIN stores t ON (s.stor_id=t.stor_id)
    GROUP BY t.state, t.stor_id) s JOIN
    (SELECT t.state, t.stor_id, SUM(s.qty) AS TotalSales
    FROM sales s JOIN stores t ON (s.stor_id=t.stor_id)
    GROUP BY t.state, t.stor_id) t ON (s.state=t.state)
    JOIN stores st ON (s.stor_id=st.stor_id)
    WHERE s.totalsales <= t.totalsales
    GROUP BY s.state,st.stor_name,s.totalsales
    HAVING COUNT(*) <=1
    ORDER BY s.state, rank看懂的请帮忙解释一下
      

  6.   

    SELECT   s.state,   st.stor_name,s.totalsales,rank=COUNT(*) 
    FROM   (SELECT   t.state,   t.stor_id,   SUM(s.qty)   AS   TotalSales 
    FROM   sales   s   JOIN   stores   t   ON   (s.stor_id=t.stor_id) 
    GROUP   BY   t.state,   t.stor_id)   s   JOIN (SELECT   t.state,   t.stor_id,   SUM(s.qty)   AS   TotalSales 
    FROM   sales   s   JOIN   stores   t   ON   (s.stor_id=t.stor_id) 
    GROUP   BY   t.state,   t.stor_id)   t   ON   (s.state=t.state)
     
    JOIN   stores   st   ON   (s.stor_id=st.stor_id) WHERE   s.totalsales   <=   t.totalsales 
    GROUP   BY   s.state,st.stor_name,s.totalsales 
    HAVING   COUNT(*)   <=1 
    ORDER   BY   s.state,   rank 这样分开好看多了:)