我如何能够查询结果量

时间:2013-03-20 08:40:43

标签: sql-server tsql

我需要在结果行数的情况下更改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

P.S。抱歉我的英文。

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

1 个答案:

答案 0 :(得分: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

....