2

結果の行数の場合は、EDU_INSTITUTIONS.FULL_NAME_INSTRUMENTAL_CASE の値を変更する必要があります。たとえば、クエリが 1 行の INS を返す場合は INS のみ、2 行を返す場合は最初の行の INS + '' + 2 番目の行の 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: 1 番目と 3 番目のケースは解決しましたが、2 番目のケースを解決する方法がわかりません。

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:このバージョンはほぼ動作していますが、動作していないのは 2 番目のケースです

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 に答える