0

--这是代码

create table #Test (Systemtraceno nvarchar(50),Bin nvarchar(50),SwitchCode nvarchar(50),
SwitchDesc nvarchar(50),[Description] nvarchar(50))

insert into #Test 
select SystemTraceno , Bin,SwitchCode,SwitchDesc,[Description]
from(
select A.SystemTraceNo, A.BIN,'' SwitchCode, '' SwitchDesc,''[Description]
from ATM035 A 
where a.TranDate = '20130924' and  MsgType = '0210' and TerminalID = '08880001'
and A.ProcessCode in ('011000','012000','013000') and A.ResponseCode = '0000' and A.BIN <> '502265' 
--group by A.SystemTraceNo, A.BIN
)x
group by SystemTraceNo,BIN,SwitchCode,SwitchDesc,[Description]
having COUNT(SystemTraceNo)=2

update #Test set SwitchCode = (select top 1 SwitchCode from ATM027 where Bin = #Test.Bin )
update #test set SwitchDesc = (select switchname from ATM016 where SwitchCode = #test.switchcode)
update #test set [Description] = (Select top 1 Description from ATM027 where BIN = #Test.Bin )

Select * from #test order by SwitchDesc asc
drop table #test    

--,'301000','302000','303000'

我只是想选择 SystemTrace number count = 2 的行。我之前遇到了聚合问题,现在这个问题。希望你能帮助我。提前致谢

4

4 回答 4

1

我不会进入你的代码,但它应该是这样的:

select customers.customerId 
from customers join orders on custumers.Id = orders.customerId
group by customers.customerId
having count(orders.id)=2

请参阅此处的第 2 节:(第 1 节适用于说这是可能的人)。

http://i.stack.imgur.com/aE2M3.png

于 2013-09-25T08:46:09.347 回答
1

@Royi:实际上,我认为应该是

select customers.customerId , count(orders.id) as num
from customers join orders on custumers.Id = orders.customerId
group by customers.customerId
having num=2
于 2013-09-25T08:51:04.630 回答
0
where systemtraceno in (select systemtraceno from #Test group by systemtraceno having count(systemtraceno) = 2)
于 2013-09-25T08:46:41.247 回答
0

我认为问题出在

group by SystemTraceNo

当你想使用

COUNT(SystemTraceNo)=2

简而言之,您的插入代码应该是这样的:

insert into #Test 
select A.SystemTraceno , A,Bin,'' SwitchCode,'' SwitchDesc,'' [Description]
from ATM035 A 
where a.TranDate = '20130924' and  MsgType = '0210' and TerminalID = '08880001'
and A.ProcessCode in ('011000','012000','013000') and A.ResponseCode = '0000' and A.BIN <> '502265'
group by A.BIN
having COUNT(SystemTraceNo)=2
于 2013-09-25T08:48:52.523 回答