2013-04-17 2 views
3

이 두 문을 하나로 결합하려고 시도하지만 모든 시도가 실패했습니다! 병합 할 수 있습니까?TSQL - 하나의 TOP 및 COUNT SELECT

-- Is there a open answer? 
SELECT CASE COUNT(tbl_Communication.pk_Communication) WHEN 0 
     THEN 0 ELSE 1 END AS hasAnsweredCom 
FROM tbl_Communication 
JOIN tbl_CommunicationElements ON tbl_CommunicationElements.pk_Communication = tbl_Communication.pk_Communication 
WHERE tbl_Communication.pk_Ticket = @pk_Ticket 
    AND tbl_Communication.isClosed = 0 
    AND tbl_Communication.pk_CommunicationType = (SELECT pk_CommunicationType 
               FROM tbl_CommunicationType 
               WHERE name = 'query') 

-- Get the answer text 
SELECT TOP 1 tbl_Communication.subject AS hasAnsweredComStepName 
FROM tbl_Communication 
JOIN tbl_CommunicationElements ON tbl_CommunicationElements.pk_Communication = tbl_Communication.pk_Communication 
WHERE tbl_Communication.pk_Ticket = @pk_Ticket 
    AND tbl_Communication.isClosed = 0 
    AND tbl_Communication.pk_CommunicationType = (SELECT pk_CommunicationType 
               FROM tbl_CommunicationType 
               WHERE name = 'query') 
ORDER BY tbl_Communication.pk_Communication 
+1

'TOP 1 '로 무엇을 주문 했습니까? 또한'CASE WHEN EXISTS'를보기보다는 모든 것을 세고 0이 아닌 플래그를 설정하십시오. –

+0

왜 주문 하시겠습니까? 두 명령문 모두 1 행/필드가됩니다. – MacTee

+0

@Eichenwald : 네,하지만 두 번째 행에는 여러 행이 있음을 나타내는 'TOP 1'이 필요하므로 순서를 지정해야합니다. 그렇지 않으면 결과를 예측할 수 없습니다. 당신 (지금)이 중요하지 않더라도 '주문'을 생략하는 것은 나쁜 습관입니다. –

답변

1

오른쪽 가입 트릭.

SELECT TOP 1 
    CASE WHEN tbl_CommunicationElements.pk_Communication IS NULL THEN 0 ELSE 1 END hasAnsweredCom 
    , tbl_Communication.subject AS hasAnsweredComStepName 
FROM tbl_Communication 
JOIN tbl_CommunicationElements ON tbl_CommunicationElements.pk_Communication = tbl_Communication.pk_Communication 
RIGHT JOIN (VALUES(1)) AS Ext(x) ON (
    tbl_Communication.pk_Ticket = @pk_Ticket 
    AND tbl_Communication.isClosed = 0 
    AND tbl_Communication.pk_CommunicationType = (SELECT pk_CommunicationType 
               FROM tbl_CommunicationType 
               WHERE name = 'query') 
) 
+0

thx이지만,'tbl_CommunicationElements.pk_Communication'은 NULL이 될 수없고'tbl_Communication'이어야하기 때문에 이것은 작동하지 않을 것입니다. – MacTee

+0

이것은 외부 조인이므로 hasAnsweredCom = 0 일 때 tbl_CommunicationElements.pk_Communication은 null이됩니다. – Serge

+0

와우, 여기서 무슨 일이 일어 났는지는 이해할 수 없지만 작동합니다;) thx alot! – MacTee

1

하나 개의 라인, 다음 작품에있는 두 개의 결과를 넣어하고자하는 경우에는 답이없는 경우 두 번째 값은 NULL이됩니다

select (CASE count(*) WHEN 0 THEN 0 ELSE 1 END) AS hasAnsweredCom, 
     MAX(case when seqnum = 1 then subject end) as hasAnsweredComStepName 
from (SELECT tbl_Communication.pk_Communication, tbl_Communication.subject, 
      ROW_NUMBER() over (order by pk_communication) as seqnum        
     FROM tbl_Communication 
     JOIN tbl_CommunicationElements ON tbl_CommunicationElements.pk_Communication = tbl_Communication.pk_Communication 
     WHERE tbl_Communication.pk_Ticket = @pk_Ticket 
     AND tbl_Communication.isClosed = 0 
     AND tbl_Communication.pk_CommunicationType = (SELECT pk_CommunicationType 
                 FROM tbl_CommunicationType 
                 WHERE name = 'query') 
    ) t 

.

두 행을 반환하는 경우. 내 생각 엔 subject은 문자열이고 hasAnsweredCom은 정수입니다. 유형이 충돌하므로 어떤 종류의 union이나 결과를 함께 가져 오면 두 번째 행에 유형 충돌이 발생할 수 있습니다.

+0

thx too too !!! 이것도 작동합니다. – MacTee