添加Top 1和Order By以选择具有多个连接的语句

时间:2016-03-14 15:20:43

标签: sql sql-server

我正在sql server中编写一个VIEW,并加入了几个带有内连接的表。

问题。

  1. 当我添加"按nameRela.WNAME_INDIVIDUAL_LAST"在内部联接语句中,下一个内部联接以红色突出显示,表示“内部'
  2. 附近的语法不正确”
  3. 当我添加"前1"在select语句中,它还给出了红色错误。
  4. 使用带有多重内连接的select语句,添加' Top 1'的正确语法是什么?或者选择第一条记录并按

    添加订单

    谢谢

    select  top 500    
        rtrim(pendk_policy) as Id
        ,case (pendk_company) when '17' then 'BNR' when 'WP' then 'PENN' end as Company
        ,case rtrim(pendk_policy) when '' then '--' else rtrim(pendk_policy) end  as PolicyNumber 
        ,'InForce' as [Type]
        , (
            case
                when WPOLC_CONTRACT_CODE = 'A' then 'Active'
                when WPOLC_CONTRACT_CODE = 'T' then 'Terminated'     
                when WPOLC_CONTRACT_CODE = 'P' then 'Pending'  
                when WPOLC_CONTRACT_CODE = 'S' then 'Suspended'  
                when WPOLC_CONTRACT_CODE = 'H' then 'On Hold'
            end
        ) as [Status]
        ,  top 1  rtrim(nameRela.wname_individual_first) + ' '+ rtrim(nameRela.wname_individual_middle) + ' ' + rtrim(nameRela.wname_individual_last) as Customer
    from  
        WPOLC_TABL wpolc
        inner join WRELA_TABL wrela on (wrela.WRELA_COMPANY_CODE = wpolc.WPOLC_COMPANY_CODE and wrela.WRELA_POLICY_NUMBER = wpolc.WPOLC_POLICY_NUMBER and wrela.WRELA_RELATE_CODE = 'sa')
    
        inner join WRELA_TABL custRela on (custRela.WRELA_COMPANY_CODE = wpolc.WPOLC_COMPANY_CODE and custRela.WRELA_POLICY_NUMBER = wpolc.WPOLC_POLICY_NUMBER 
            and custRela.WRELA_RELATE_CODE in ('in', 'ji' , 'je')) order by  custRela.WRELA_RELATE_CODE
    
        inner join wname_tabl nameRela on (custRela.WRELA_COMPANY_CODE = nameRela.WNAME_COMPANY_CODE and custRela.WRELA_NAME_ID = nameRela.WNAME_NAME_ID )
        order by nameRela.WNAME_INDIVIDUAL_LAST
    
        inner join PENDKEY_TABL pk on (wrela.WRELA_COMPANY_CODE = pk.PendK_company and wrela.WRELA_POLICY_NUMBER = pk.PENDK_POLICY)
    

1 个答案:

答案 0 :(得分:0)

订单应该在查询中排在最后。基本格式为:

Select 
From
  inner join
where
order by