Sybase Adaptive Server Enterprise - SQL行比较

时间:2015-01-13 04:42:57

标签: sql sybase sybase-ase self-join

我正在查询Sybase Adaptive Server Enterprise(ASE)数据库,需要比较行,例如第1行,第2行,第2行,第3行等。出于性能原因,我不想使用游标。我认为自我联接是答案,但ID不是顺序的,即1,2,3,4。我尝试使用Row_Number函数在临时表中创建一个列,但似乎不支持该函数。以下是获取必需数据的查询(没有自连接)。有没有人知道在这种情况下不使用游标来比较行?

SELECT h.HeaderIdS, w.WrapUpIdS, h.CallStarttimeDtTm, h.CallEndtimeDtTm, h.InvestorIdS, h.AdviserIdS, h.BadgeIdS, h.EmployerNoIdS, h.ContactTypeCdS, w.CallTypeCdS, w.SubTypeCdS, w.ProductCdS, h.TeamCodeCdS, h.ContactedByCdS,
h.AgentIdS, h.SystemTypeCdS
    INTO #tempHeader
    FROM WrapUpHeader h
--Criteria
INNER JOIN WrapUp w ON h.HeaderIdS = w.HeaderIdS And w.StatusCdS = 'Active'
WHERE  h.StatusCdS = 'Active'
AND h.CallStartTimeDtTm >= @FromDate
AND h.CallStartTimeDtTm < DateAdd(dd,1,@ToDate)
AND h.TeamCodeCdS = @BusinessUnit
AND ((h.AdviserIdS IS NULL) OR (h.AdviserIdS IS NOT NULL AND h.AdviserIdS <> @AdviserNo))
AND ((h.BadgeIdS IS NULL) OR (h.BadgeIdS IS NOT NULL AND h.BadgeIdS <> @DealerGroup))
AND ((h.EmployerNoIdS IS NULL) OR (h.EmployerNoIdS IS NOT NULL AND h.EmployerNoIdS <> @EmployerNo))
AND ((w.ProductCdS IS NULL) OR (w.ProductCdS IS NOT NULL AND w.ProductCdS <> @Product))   
AND ((w.CallTypeCdS IS NULL) OR (w.CallTypeCdS IS NOT NULL AND w.CallTypeCdS <> @CallType))   
AND ((w.SubTypeCdS IS NULL) OR (w.SubTypeCdS IS NOT NULL AND w.SubTypeCdS <> @SubType))   


SELECT  
        Distinct RTRIM(th.headerids) as 'CALLIDA',  
        th.callstarttimedttm as 'TimeA',
        RTRIM(th.investorids) as 'AccountIDA',
        RTRIM(e.entitynametxm) as 'StaffMemberA',
        Count(th.investorids) as 'RepeatCallA'

INTO #TABLEA
from #tempHeader tH 
inner join wrapupdetail d on d.wrapupids = th.wrapupids       
inner join entity e on e.entityids = th.agentids
left outer join notes note on note.contactids = th.headerids
left outer join ownershiptasks ot on note.OwnershipTasksIds =ot.OwnershipTasksIds
order by  th.investorids, th.callstarttimedttm

0 个答案:

没有答案