select top 50 1 as sousid,a.ID,a.keyword,
sumcount=(select count(*) from Fact_Access b 
          where b.cid=23 and b.sousid=1 and b.keyid=a.ID 
          and b.intime>='2006-05-01' and b.intime<='2006-05-31'),
sumvalue=(select isnull(sum(c.accessvalue),0) from Fact_Access c 
          where c.cid=23 and c.sousid=1 and c.keyid=a.ID 
          and c.intime>='2006-05-01' and c.intime<='2006-05-31')
from stat_keyword a
order by sumvalue desc
union
select top 50 2 as sousid,j.ID,j.keyword,
sumcount=(select count(*) from Fact_Access h 
          where h.cid=23 and h.sousid=2 and h.keyid=j.ID 
          and h.intime>='2006-05-01' and h.intime<='2006-05-31'),
sumvalue=(select isnull(sum(i.accessvalue),0) from Fact_Access i 
          where i.cid=23 and i.sousid=2 and i.keyid=j.ID 
          and i.intime>='2006-05-01' and i.intime<='2006-05-31')
from stat_keyword j
order by sumvalue desc

解决方案 »

  1.   

    select top 50 1 as sousid,a.ID as ID,a.keyword as keyword,
    sumcount=(select count(*) from Fact_Access b 
              where b.cid=23 and b.sousid=1 and b.keyid=a.ID 
              and b.intime>='2006-05-01' and b.intime<='2006-05-31'),
    sumvalue=(select isnull(sum(c.accessvalue),0) from Fact_Access c 
              where c.cid=23 and c.sousid=1 and c.keyid=a.ID 
              and c.intime>='2006-05-01' and c.intime<='2006-05-31')
    from stat_keyword a
    order by sumvalue desc
    union
    select top 50 2 as sousid,j.ID as ID ,j.keyword as keyword,
    sumcount=(select count(*) from Fact_Access h 
              where h.cid=23 and h.sousid=2 and h.keyid=j.ID 
              and h.intime>='2006-05-01' and h.intime<='2006-05-31'),
    sumvalue=(select isnull(sum(i.accessvalue),0) from Fact_Access i 
              where i.cid=23 and i.sousid=2 and i.keyid=j.ID 
              and i.intime>='2006-05-01' and i.intime<='2006-05-31')
    from stat_keyword j
    order by sumvalue desc
      

  2.   

    order by sumvalue desc
    这样写只能一个,你把union前面那个order by排序去掉就可以了,不过可能不是你要的结果
      

  3.   

    --tryselect top 50 1 as sousid,a.ID,a.keyword,
    sumcount=(select count(*) from Fact_Access b 
              where b.cid=23 and b.sousid=1 and b.keyid=a.ID 
              and b.intime>='2006-05-01' and b.intime<='2006-05-31'),
    sumvalue=(select isnull(sum(c.accessvalue),0) from Fact_Access c 
              where c.cid=23 and c.sousid=1 and c.keyid=a.ID 
              and c.intime>='2006-05-01' and c.intime<='2006-05-31')
    from stat_keyword a
    --order by sumvalue desc
    union
    select top 50 2 as sousid,j.ID,j.keyword,
    sumcount=(select count(*) from Fact_Access h 
              where h.cid=23 and h.sousid=2 and h.keyid=j.ID 
              and h.intime>='2006-05-01' and h.intime<='2006-05-31'),
    sumvalue=(select isnull(sum(i.accessvalue),0) from Fact_Access i 
              where i.cid=23 and i.sousid=2 and i.keyid=j.ID 
              and i.intime>='2006-05-01' and i.intime<='2006-05-31')
    from stat_keyword j
    order by sumvalue desc
      

  4.   

    SELECT * 
    FROM 
    (SELECT TOP 50 1 AS sousid,a.ID,a.keyword,
    sumcount=(SELECT COUNT(*) FROM Fact_Access b 
              WHERE b.cid=23 AND b.sousid=1 AND b.keyid=a.ID 
              AND b.intime>='2006-05-01' AND b.intime<='2006-05-31'),
    sumvalue=(SELECT isnull(SUM(c.accessvalue),0) FROM Fact_Access c 
              WHERE c.cid=23 AND c.sousid=1 AND c.keyid=a.ID 
              AND c.intime>='2006-05-01' AND c.intime<='2006-05-31')
    FROM stat_keyword a
    UNION
    SELECT TOP 50 2 AS sousid,j.ID,j.keyword,
    sumcount=(SELECT COUNT(*) FROM Fact_Access h 
              WHERE h.cid=23 AND h.sousid=2 AND h.keyid=j.ID 
              AND h.intime>='2006-05-01' AND h.intime<='2006-05-31'),
    sumvalue=(SELECT isnull(SUM(i.accessvalue),0) FROM Fact_Access i 
              WHERE i.cid=23 AND i.sousid=2 AND i.keyid=j.ID 
              AND i.intime>='2006-05-01' AND i.intime<='2006-05-31')
    FROM stat_keyword j) a
    ORDER BY sumvalue DESC