SQL获取apear一次的值,而不是明显的

时间:2015-08-21 11:26:31

标签: sql sql-server

只获得一次出现的值。我目前有一些sql代码可以获得所有0%的条目。问题是两行可以包含具有不同百分比的同一个人。如果其中一个高于0,那么我不希望它出现在查询

abridged table:
Name - Percent 
steve    0
dan    0
mike    100
harold    50
steve    80
carl    0
carl    0

Result:
dan - 0
Carl - 0

这是我有多远,但没有设法使Count()或拥有或Group工作的任何变化。

select person, Value2, Value3, Value4, percent
from 
Table1
INNER JOIN Table1 ON Table2.valueNum = Table1.valueNum 
INNER JOINTable1 ON Table3.valueNum = Table1.valueNum 
INNER JOIN Table1 ON Table4.valueNum = Table1.valueNum 
WHERE 
(@date BETWEEN table1.FROMDATE AND table1.todate) 
AND table1.percent =  0
AND table1.varchar IN ('T',  'X')

6 个答案:

答案 0 :(得分:2)

这是一种方法

select name,0 as percent from abridged 
group by name
having min(percent)=0 and max(percent)=0

答案 1 :(得分:1)

您的示例SQL和删节表不匹配。但是,这看起来像你想要的基本想法:

select
  *
from
  dbo.table a
where
  a.percent = 0 and
  not exists (
    select
      'x'
    from
      dbo.table b
    where
      a.Name = b.Name and
      b.percent > 0
  );

答案 2 :(得分:0)

我只想使用窗口函数:

with t as (
      <your query here>
     )
select t.*
from (select t.*, count(*) over (partition by name) as cnt
      from t
     ) t
where cnt = 1;

答案 3 :(得分:0)

要解决您的问题,您可以使用此查询:

select [name], 0 as [percent] from [abridged]
group by [name]
having sum([percent])=0

答案 4 :(得分:0)

这可以解决你的问题吗?

select name,sum([percent]) from abridged 
group by name
having SUM([percent]) = 0

答案 5 :(得分:0)

<强>查询

SELECT distinct name,  min(percentage) from a
group by name
having min(percentage) = 0
and count(*) > 1;