2

在结果行数的情况下,我需要更改 EDU_INSTITUTIONS.FULL_NAME_INSTRUMENTAL_CASE 的值。例如,如果查询返回一行 INS 应该只是 INS,如果返回两行,则从第一行返回 INS + '' + 从第二行返回 INS,如果超过 3 行返回 - 来自第一行的 INS 和数字或行 - 1

这是我目前的代码。

SELECT   

EDU_INSTITUTIONS.FULL_NAME_GENITIVE_CASE AS GEN,
EDU_INSTITUTIONS.FULL_NAME_INSTRUMENTAL_CASE AS INS,
EDU_INSTITUTIONS.FULL_NAME_DATIVE_CASE AS DAT,
NOTIFICATIONS.[ADDRESS] AS [ADDRESS],
NOTIFICATIONS.NUMBER AS NUM,
NOTIFICATIONS.RESPONSE_NUMBER AS RNUM,
NOTIFICATIONS.SIGN_DATE AS SDATE,
NOTIFICATIONS.SIGNER_POST AS SPOST,
NOTIFICATIONS.SIGNER_FULL_NAME AS SNAME,
RECIPIENT_FULL_NAME AS FIO,
RTRIM(ISNULL(APPLICATION_TYPES.NAME_GENITIVE_CASE,'') + ' ' + ISNULL(APPLICATION_REASONS.NAME_PREPOSITIONAL_CASE, '')) AS REAS

FROM NOTIFICATIONS
LEFT JOIN APPLICATIONS ON NOTIFICATIONS.APPLICATION_FK=APPLICATIONS.ID
LEFT JOIN APPLICATION_REASONS ON APPLICATION_REASONS.ID=APPLICATIONS.REASON_FK
LEFT JOIN APPLICATION_TYPES ON APPLICATION_TYPES.ID=APPLICATION_REASONS.APPLICATION_TYPE_FK
LEFT JOIN REFUSAL_TYPES ON REFUSAL_TYPES.ID=APPLICATIONS.REFUSAL_TYPE_FK
LEFT JOIN USERS ON USERS.ID=NOTIFICATIONS.EXECUTANT_FK
LEFT JOIN APPLICATION_INSTITUTIONS ON APPLICATION_INSTITUTIONS.APPLICATION_FK=APPLICATIONS.ID 
LEFT JOIN EDU_INSTITUTIONS ON EDU_INSTITUTIONS.ID=APPLICATION_INSTITUTIONS.INSTITUTION_FK

WHERE
NOTIFICATIONS.ID=@ID

PS对不起我的英语。

UDP: 我解决了第一种和第三种情况,但我现在不知道如何解决第二种我的代码

select INSE =
case 
when TotalRows = 1 then INS
--when TotalRows = 2 then INS + INS   
when cast(TotalRows as int) > 2 then INS + ' и '+ cast(TotalRows-1 as nvarchar)+' филиалами'
end
from(
SELECT 

EDU_INSTITUTIONS.FULL_NAME_GENITIVE_CASE AS GEN,
EDU_INSTITUTIONS.FULL_NAME_INSTRUMENTAL_CASE AS INS,
EDU_INSTITUTIONS.FULL_NAME_DATIVE_CASE AS DAT,
NOTIFICATIONS.[ADDRESS] AS [ADDRESS],
NOTIFICATIONS.NUMBER AS NUM,
NOTIFICATIONS.RESPONSE_NUMBER AS RNUM,
NOTIFICATIONS.SIGN_DATE AS SDATE,
NOTIFICATIONS.SIGNER_POST AS SPOST,
NOTIFICATIONS.SIGNER_FULL_NAME AS SNAME,
RECIPIENT_FULL_NAME AS FIO,
RTRIM(ISNULL(APPLICATION_TYPES.NAME_GENITIVE_CASE,'') + ' ' + ISNULL(APPLICATION_REASONS.NAME_PREPOSITIONAL_CASE, '')) AS REAS,
APPLICATION_INSTITUTIONS.IS_APPLICANT as ORD,
COUNT(*) OVER () [TotalRows]

FROM NOTIFICATIONS
left join APPLICATIONS on NOTIFICATIONS.APPLICATION_FK=APPLICATIONS.ID
left join APPLICATION_REASONS on APPLICATION_REASONS.ID=APPLICATIONS.REASON_FK
left join APPLICATION_TYPES on APPLICATION_TYPES.ID=APPLICATION_REASONS.APPLICATION_TYPE_FK
left join REFUSAL_TYPES on REFUSAL_TYPES.ID=APPLICATIONS.REFUSAL_TYPE_FK
left join USERS on USERS.ID=NOTIFICATIONS.EXECUTANT_FK
left join APPLICATION_INSTITUTIONS on APPLICATION_INSTITUTIONS.APPLICATION_FK=APPLICATIONS.ID 
left join EDU_INSTITUTIONS on EDU_INSTITUTIONS.ID=APPLICATION_INSTITUTIONS.INSTITUTION_FK

WHERE
NOTIFICATIONS.ID='20874f82-1887-4214-8812-53c21611202f'


) as tmp
order by  ORD desc

UPD2:这个版本几乎可以工作,不工作是第二种情况

select INS_CASE =
case 
when TotalRows = 1 then INS
when TotalRows = 2 then
    (SELECT TOP 1 FULL_NAME_INSTRUMENTAL_CASE FROM EDU_INSTITUTIONS AS EDU WHERE EDU.ID = tmp.ID ORDER BY EDU.FULL_NAME_INSTRUMENTAL_CASE ASC ) 
    + ' и ' + 
    (SELECT TOP 1 EDU.FULL_NAME_INSTRUMENTAL_CASE FROM EDU_INSTITUTIONS AS EDU WHERE EDU.ID = tmp.ID ORDER BY EDU.FULL_NAME_INSTRUMENTAL_CASE DESC )
when TotalRows > 2 then INS + ' и '+ cast(TotalRows-1 as nvarchar)+' филиалами'
when TotalRows > 2 and ORD=0 then cast(TotalRows as nvarchar)+' филиалами ' + (SELECT top 1 INS from EDU_INSTITUTIONS as EDU where edu.id = id )
end
from(
SELECT 
EDU_INSTITUTIONS.ID as ID,
EDU_INSTITUTIONS.FULL_NAME_GENITIVE_CASE AS GEN,
EDU_INSTITUTIONS.FULL_NAME_INSTRUMENTAL_CASE AS INS,
EDU_INSTITUTIONS.FULL_NAME_DATIVE_CASE AS DAT,
NOTIFICATIONS.[ADDRESS] AS [ADDRESS],
NOTIFICATIONS.NUMBER AS NUM,
NOTIFICATIONS.RESPONSE_NUMBER AS RNUM,
NOTIFICATIONS.SIGN_DATE AS SDATE,
NOTIFICATIONS.SIGNER_POST AS SPOST,
NOTIFICATIONS.SIGNER_FULL_NAME AS SNAME,
RECIPIENT_FULL_NAME AS FIO,
RTRIM(ISNULL(APPLICATION_TYPES.NAME_GENITIVE_CASE,'') + ' ' + ISNULL(APPLICATION_REASONS.NAME_PREPOSITIONAL_CASE, '')) AS REAS,
APPLICATION_INSTITUTIONS.IS_APPLICANT as ORD,
COUNT(*) OVER () [TotalRows]

FROM NOTIFICATIONS
left join APPLICATIONS on NOTIFICATIONS.APPLICATION_FK=APPLICATIONS.ID
left join APPLICATION_REASONS on APPLICATION_REASONS.ID=APPLICATIONS.REASON_FK
left join APPLICATION_TYPES on APPLICATION_TYPES.ID=APPLICATION_REASONS.APPLICATION_TYPE_FK
left join REFUSAL_TYPES on REFUSAL_TYPES.ID=APPLICATIONS.REFUSAL_TYPE_FK
left join USERS on USERS.ID=NOTIFICATIONS.EXECUTANT_FK
left join APPLICATION_INSTITUTIONS on APPLICATION_INSTITUTIONS.APPLICATION_FK=APPLICATIONS.ID 
left join EDU_INSTITUTIONS on EDU_INSTITUTIONS.ID=APPLICATION_INSTITUTIONS.INSTITUTION_FK

WHERE
NOTIFICATIONS.ID='20874f82-1887-4214-8812-53c21611202f'
--and APPLICATION_INSTITUTIONS.IS_APPLICANT =1

--'20874f82-1887-4214-8812-53c21611202f'    --=2
--'859eef0a-be0f-48e8-9bb0-83cbebca6c5d'    -->2 
--'31992df7-a3ee-4d07-9596-21017424e594'    --=1
--'640d283d-57a7-4ddb-a7c3-f43caeb83aa1'    -->2 ORD 0

) as tmp
order by ORD desc

UPD3:这是解决问题的方法

select INS_CASE =
case 
when TotalRows = 1 then INS
when TotalRows = 2 then
    (SELECT TOP 1 INS FROM (
SELECT 
EDU_INSTITUTIONS.ID as ID,
EDU_INSTITUTIONS.FULL_NAME_GENITIVE_CASE AS GEN,
EDU_INSTITUTIONS.FULL_NAME_INSTRUMENTAL_CASE AS INS,
EDU_INSTITUTIONS.FULL_NAME_DATIVE_CASE AS DAT,
NOTIFICATIONS.[ADDRESS] AS [ADDRESS],
NOTIFICATIONS.NUMBER AS NUM,
NOTIFICATIONS.RESPONSE_NUMBER AS RNUM,
NOTIFICATIONS.SIGN_DATE AS SDATE,
NOTIFICATIONS.SIGNER_POST AS SPOST,
NOTIFICATIONS.SIGNER_FULL_NAME AS SNAME,
RECIPIENT_FULL_NAME AS FIO,
RTRIM(ISNULL(APPLICATION_TYPES.NAME_GENITIVE_CASE,'') + ' ' + ISNULL(APPLICATION_REASONS.NAME_PREPOSITIONAL_CASE, '')) AS REAS,
APPLICATION_INSTITUTIONS.IS_APPLICANT as ORD,
COUNT(*) OVER () [TotalRows]

FROM NOTIFICATIONS
left join APPLICATIONS on NOTIFICATIONS.APPLICATION_FK=APPLICATIONS.ID
left join APPLICATION_REASONS on APPLICATION_REASONS.ID=APPLICATIONS.REASON_FK
left join APPLICATION_TYPES on APPLICATION_TYPES.ID=APPLICATION_REASONS.APPLICATION_TYPE_FK
left join REFUSAL_TYPES on REFUSAL_TYPES.ID=APPLICATIONS.REFUSAL_TYPE_FK
left join USERS on USERS.ID=NOTIFICATIONS.EXECUTANT_FK
left join APPLICATION_INSTITUTIONS on APPLICATION_INSTITUTIONS.APPLICATION_FK=APPLICATIONS.ID 
left join EDU_INSTITUTIONS on EDU_INSTITUTIONS.ID=APPLICATION_INSTITUTIONS.INSTITUTION_FK

WHERE
NOTIFICATIONS.ID='20874f82-1887-4214-8812-53c21611202f'

) as tmp ORDER BY INS DESC ) 
    + ' и ' + 
    (SELECT TOP 1INS FROM (
SELECT 
EDU_INSTITUTIONS.ID as ID,
EDU_INSTITUTIONS.FULL_NAME_GENITIVE_CASE AS GEN,
EDU_INSTITUTIONS.FULL_NAME_INSTRUMENTAL_CASE AS INS,
EDU_INSTITUTIONS.FULL_NAME_DATIVE_CASE AS DAT,
NOTIFICATIONS.[ADDRESS] AS [ADDRESS],
NOTIFICATIONS.NUMBER AS NUM,
NOTIFICATIONS.RESPONSE_NUMBER AS RNUM,
NOTIFICATIONS.SIGN_DATE AS SDATE,
NOTIFICATIONS.SIGNER_POST AS SPOST,
NOTIFICATIONS.SIGNER_FULL_NAME AS SNAME,
RECIPIENT_FULL_NAME AS FIO,
RTRIM(ISNULL(APPLICATION_TYPES.NAME_GENITIVE_CASE,'') + ' ' + ISNULL(APPLICATION_REASONS.NAME_PREPOSITIONAL_CASE, '')) AS REAS,
APPLICATION_INSTITUTIONS.IS_APPLICANT as ORD,
COUNT(*) OVER () [TotalRows]

FROM NOTIFICATIONS
left join APPLICATIONS on NOTIFICATIONS.APPLICATION_FK=APPLICATIONS.ID
left join APPLICATION_REASONS on APPLICATION_REASONS.ID=APPLICATIONS.REASON_FK
left join APPLICATION_TYPES on APPLICATION_TYPES.ID=APPLICATION_REASONS.APPLICATION_TYPE_FK
left join REFUSAL_TYPES on REFUSAL_TYPES.ID=APPLICATIONS.REFUSAL_TYPE_FK
left join USERS on USERS.ID=NOTIFICATIONS.EXECUTANT_FK
left join APPLICATION_INSTITUTIONS on APPLICATION_INSTITUTIONS.APPLICATION_FK=APPLICATIONS.ID 
left join EDU_INSTITUTIONS on EDU_INSTITUTIONS.ID=APPLICATION_INSTITUTIONS.INSTITUTION_FK

WHERE
NOTIFICATIONS.ID='20874f82-1887-4214-8812-53c21611202f'

) as tmp  ORDER BY INS ASC )
when TotalRows > 2 then INS + ' и '+ cast(TotalRows-1 as nvarchar)+' филиалами'
when TotalRows > 2 and ORD=0 then cast(TotalRows as nvarchar)+' филиалами ' + (SELECT top 1 INS from EDU_INSTITUTIONS as EDU where edu.id = id )
end
from(
SELECT 
EDU_INSTITUTIONS.ID as ID,
EDU_INSTITUTIONS.FULL_NAME_GENITIVE_CASE AS GEN,
EDU_INSTITUTIONS.FULL_NAME_INSTRUMENTAL_CASE AS INS,
EDU_INSTITUTIONS.FULL_NAME_DATIVE_CASE AS DAT,
NOTIFICATIONS.[ADDRESS] AS [ADDRESS],
NOTIFICATIONS.NUMBER AS NUM,
NOTIFICATIONS.RESPONSE_NUMBER AS RNUM,
NOTIFICATIONS.SIGN_DATE AS SDATE,
NOTIFICATIONS.SIGNER_POST AS SPOST,
NOTIFICATIONS.SIGNER_FULL_NAME AS SNAME,
RECIPIENT_FULL_NAME AS FIO,
RTRIM(ISNULL(APPLICATION_TYPES.NAME_GENITIVE_CASE,'') + ' ' + ISNULL(APPLICATION_REASONS.NAME_PREPOSITIONAL_CASE, '')) AS REAS,
APPLICATION_INSTITUTIONS.IS_APPLICANT as ORD,
COUNT(*) OVER () [TotalRows]

FROM NOTIFICATIONS
left join APPLICATIONS on NOTIFICATIONS.APPLICATION_FK=APPLICATIONS.ID
left join APPLICATION_REASONS on APPLICATION_REASONS.ID=APPLICATIONS.REASON_FK
left join APPLICATION_TYPES on APPLICATION_TYPES.ID=APPLICATION_REASONS.APPLICATION_TYPE_FK
left join REFUSAL_TYPES on REFUSAL_TYPES.ID=APPLICATIONS.REFUSAL_TYPE_FK
left join USERS on USERS.ID=NOTIFICATIONS.EXECUTANT_FK
left join APPLICATION_INSTITUTIONS on APPLICATION_INSTITUTIONS.APPLICATION_FK=APPLICATIONS.ID 
left join EDU_INSTITUTIONS on EDU_INSTITUTIONS.ID=APPLICATION_INSTITUTIONS.INSTITUTION_FK

WHERE
NOTIFICATIONS.ID='20874f82-1887-4214-8812-53c21611202f'


) as tmp
order by ORD desc
4

1 回答 1

1

也许你可以使用下一个组合:

....

SELECT INSE =
CASE
WHEN TotalRows = 1 THEN INS
WHEN TotalRows = 2 THEN 
    (SELECT TOP 1 EDU_INSTITUTIONS.FULL_NAME_INSTRUMENTAL_CASE 
      FROM tmp AS EDU 
      ORDER BY EDU_INSTITUTIONS.FULL_NAME_INSTRUMENTAL_CASE ASC) + ' ' + 
    (SELECT TOP 1 EDU_INSTITUTIONS.FULL_NAME_INSTRUMENTAL_CASE 
      FROM tmp AS EDU 
      ORDER BY EDU_INSTITUTIONS.FULL_NAME_INSTRUMENTAL_CASE DESC)
WHEN CAST(TotalRows AS int) > 2 THEN INS + ' и '+ CAST(TotalRows-1 as nvarchar)+' филиалами'
END

....
于 2013-03-20T10:15:06.727 回答