用于对所选内容进行排序和选择的SQL查询

时间:2015-11-06 20:32:07

标签: sql sql-server sql-server-2008

我会帮助解决这个问题,我有新闻表 我想选择2000个术语并对它们进行排序,然后检查2000中的术语是否存在,否则为0。 有点像这样。

 SELECT TOP 1000 [terms]
      ,[frequency]
      ,[occurance]
      ,[idf]
      ,[tfidf]
    FROM [Central].[news]
    ORDER BY tfidf DESC;

    IF @@ROWCOUNT= 0 

    select 0 as FinalResult;

    ELSE 
    if @@ROWCOUNT< 2000 
       select * from [CentralFinance].[dbo].[TFIDF_1]  where terms = 'project'

2 个答案:

答案 0 :(得分:0)

您可以使用Temp表存储初始查询,并查询新数据集的临时表。或者,您可以向where子句添加子查询。你想从查询中返回什么?

答案 1 :(得分:0)

也许这很有帮助。这是一个总猜:

select top 2000 -- or 1000?
    terms, frequency, occurance, idf, tfidf
from Central.news
order by tfidf desc;

if @@rowcount > 0 begin
    select * from CentralFinance.dbo.TFIDF_1
    where terms in (
        select top 2000 terms
        from Central.news
        order by tfidf desc
    );
    select 1 as FinalResult;
end
else begin
    select 0 as FinalResult;
end

另一个想法是:

if exists (select 1 from Central.news) begin
    select * from CentralFinance.dbo.TFIDF_1
    where terms in (
        select top 2000 terms
        from Central.news
        order by tfidf desc
    );
    select 1 as FinalResult;
end
else begin
    select 0 as FinalResult;
end

最后是第三个猜测:

select sgn(count(*)) as FinalResult
from (select 1) dummy
where 'project' in
(
    select top 2000 terms
    from Central.news
    order by tfidf desc
)