仅当列值不止一次时,SQL选择记录

时间:2016-01-20 13:49:02

标签: sql-server stored-procedures

我在SQL Server中有一个存储过程,我试图只选择列的值不止一次的记录,这可能看起来有点奇怪但我似乎无法弄明白,我尝试过使用HAVING条款,但没有运气..

我希望能够只选择那里有多次ACCOUNT的记录,例如:

ACCOUNT | PAYDATE
-------------------
B066    | 15
B066    | OUTSTAND
B027    | OUTSTAND  <--- **SHOULD NOT BE IN THE SELECT**
B039    | 09
B039    | OUTSTAND
B052    | 09
B052    | 15
B052    | OUTSTAND

BO27不应该在我的选择中显示,其余的ACCOUNTS应该。

这是我的存储过程的开始和结束:

Select * from (

*** SELECTS ARE HERE ***

                ) X where O_STAND <> 0.0000 
        group by X.ACCOUNT, X.ACCT_NAME , X.DAYS_CR, X.PAYDATE, X.O_STAND 
        order by X.ACCOUNT

我一直在努力解决这个问题,任何帮助或建议都会受到赞赏。提前谢谢。

2 个答案:

答案 0 :(得分:1)

你可以用

替换第一个字符串
$scope.playVideo = function(index, video) {
    //Obviously some other stuff happens here
    //The jist is - do your logic in here to get your source
    //Trigger a variable to show the iframe
    $scope.showVideo = true; //for showing the iFrame
    $scope.videoUrl = $filter('youtubeEmbedUrl')($scope.videoUrl); //Dont forget to inject $filter in your controller
}

然后再将您的查询包装为子查询

<iframe ng-if="showVideo" ng-src="{{videoUrl}}"></iframe>

答案 1 :(得分:0)

是的,having子句用于解决这类任务。基本上,它就像where,但不仅可以按列值进行过滤,还可以按集合函数的结果进行过滤:

declare @t table (
    Id int identity(1,1) primary key,
    AccountId varchar(20)
);

insert into @t (AccountId)
values
    ('B001'),
    ('B002'),
    ('B015'),
    ('B015'),
    ('B002');

-- Get all rows for which AccountId value is encountered more than once in the table
select *
from @t t
where exists (
    select 0
    from @t h
    where h.AccountId = t.AccountId
    group by h.AccountId
    having count(h.AccountId) > 1
);