选择多个且不同的SQL

时间:2015-10-20 11:46:57

标签: sql sql-server stored-procedures distinct

请查看下面的SQL查询。 MessageId对于许多记录可能是相同的。 我的目标是区分MessageId。 我尝试用tmp后添加DISTINCT(选择 但在这种情况下,DISTINCT-s的每一列都被','分开。 。 有没有办法像这样做一些事情:选择这些和DISTINCT这个?

    USE [ABAIB-QUEUE]
    GO
    /****** Object:  StoredProcedure [dbo].[proc_GetTraceMessages]    Script Date: 10/20/2015 15:34:08 ******/
    SET ANSI_NULLS ON
    GO
    SET QUOTED_IDENTIFIER ON
    GO
    --select * from IB_TRACE_MESSAGE order by Traced desc
    ALTER procedure [dbo].[proc_GetTraceMessages]
         @Interface nvarchar(100)
        ,@Page int
    as begin
    with tmp as (
        select 
             NETREQ.MessageId
            ,NETREQ.TargetInterface as NETInterface
            ,ABAREQ.TargetInterface as ABAInterface
            ,NETREQ.Traced as RequestIncoming
            ,NETRES.Traced as RequestOutgoing
            ,NETREQ.[Message] as NETRequestMessage
            ,NETRES.[Message] as NETResponseMessage
            ,ABAREQ.[Message] as ABARequestMessage
            ,ABARES.[Message] as ABAResponseMessage
            ,T24REQ1.[Message] as T24CreateFTRequestMessage
            ,T24RES1.[Message] as T24CreateFTResponseMessage
            ,T24REQ2.[Message] as T24AuthorizeFTRequestMessage
            ,T24RES2.[Message] as T24AuthorizeFTResponseMessage
            ,T24REQ3.[Message] as T24DeleteFTRequestMessage
            ,T24RES3.[Message] as T24DeleteFTResponseMessage
        from
            IB_TRACE_MESSAGE NETREQ

        left join IB_TRACE_MESSAGE NETRES  on NETREQ.MessageId = NETRES.MessageId  and NETRES.TargetService  = 'NET' and NETRES.IsRequest  = 0
        left join IB_TRACE_MESSAGE T24REQ1 on NETREQ.MessageId = T24REQ1.MessageId and T24REQ1.TargetService = 'T24' and T24REQ1.IsRequest = 1 and T24REQ1.TargetInterface = 'FUNDS.TRANSFER,LMB.IB.CARD.PAYMENT'
        left join IB_TRACE_MESSAGE T24RES1 on NETREQ.MessageId = T24RES1.MessageId and T24RES1.TargetService = 'T24' and T24RES1.IsRequest = 0 and T24REQ1.TargetInterface = 'FUNDS.TRANSFER,LMB.IB.CARD.PAYMENT'
        left join IB_TRACE_MESSAGE ABAREQ  on NETREQ.MessageId = ABAREQ.MessageId  and ABAREQ.TargetService  = 'ABA' and ABAREQ.IsRequest  = 1 and ABAREQ.TargetInterface = 'IBCharge'
        left join IB_TRACE_MESSAGE ABARES  on NETREQ.MessageId = ABARES.MessageId  and ABARES.TargetService  = 'ABA' and ABARES.IsRequest  = 0 and ABAREQ.TargetInterface = 'IBCharge'
        left join IB_TRACE_MESSAGE T24REQ2 on NETREQ.MessageId = T24REQ2.MessageId and T24REQ2.TargetService = 'T24' and T24REQ2.IsRequest = 1 and T24REQ1.TargetInterface = 'FUNDS.TRANSFER,LMB.IB.CARD.PAYMENT.APPROVE'
        left join IB_TRACE_MESSAGE T24RES2 on NETREQ.MessageId = T24RES2.MessageId and T24RES2.TargetService = 'T24' and T24RES2.IsRequest = 0 and T24REQ1.TargetInterface = 'FUNDS.TRANSFER,LMB.IB.CARD.PAYMENT.APPROVE'
        left join IB_TRACE_MESSAGE T24REQ3 on NETREQ.MessageId = T24REQ3.MessageId and T24REQ3.TargetService = 'T24' and T24REQ3.IsRequest = 1 and T24REQ1.TargetInterface = 'FUNDS.TRANSFER,LMB.IB.CARD.PAYMENT' and cast(T24REQ1.Message as nvarchar(max)) like '%FUNDS.TRANSFER,LMB.IB.CARD.PAYMENT/D%'
        left join IB_TRACE_MESSAGE T24RES3 on NETREQ.MessageId = T24RES3.MessageId and T24RES3.TargetService = 'T24' and T24RES3.IsRequest = 0 and T24REQ1.TargetInterface = 'FUNDS.TRANSFER,LMB.IB.CARD.PAYMENT.APPROVE'
        where
            NETREQ.TargetService = 'NET' and NETREQ.IsRequest = 1 and NETREQ.TargetInterface = 'PutCardPayment'
    )

    select top 50 
        *
    from (
        select
            *
            ,row_number() over (order by RequestIncoming desc) - 1 as [Index] 
        from
            tmp) x
    where
        [Index] >= 50 * @Page 
    end

0 个答案:

没有答案