2013-04-17 5 views
3

J'essaie de combiner ces deux instructions en une, mais toutes mes tentatives ont échoué! Est-il possible de les fusionner?TSQL - TOP et COUNT en un 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' commandé par quoi? Regardez aussi 'CASE WHEN EXISTS' plutôt que de les compter tous et de mettre un drapeau si non zéro. –

+0

pourquoi commander? les deux instructions entraîneront 1 seule ligne/champ. – MacTee

+0

@Eichenwald: Oui, mais la seconde a besoin d'un «TOP 1» qui suggère qu'il y a plusieurs lignes, donc vous devriez les commander, sinon la sortie est imprévisible. Même si cela n'a pas d'importance pour vous (maintenant) c'est une mauvaise habitude d'omettre le «ORDER BY». –

Répondre

1

Astuce de jointure à droite.

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, mais cela ne fonctionnera pas car 'tbl_CommunicationElements.pk_Communication' ne peut pas être NULL et devrait être' tbl_Communication'. – MacTee

+0

il s'agit d'une jointure externe, donc tbl_CommunicationElements.pk_Communication sera null lorsque hasAnsweredCom = 0 – Serge

+0

wow, je ne comprends pas ce qui se passe ici, mais ça fonctionne;) thx beaucoup! – MacTee

1

Si vous êtes prêt à mettre les deux résultats sur une seule ligne, les travaux suivants:

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 

La deuxième valeur sera NULL s'il n'y a pas de réponses.

Comme pour le retour de deux lignes. Ma conjecture est que subject est une chaîne tandis que hasAnsweredCom est un nombre entier. Les types entrent en conflit, donc toute sorte de union ou en rassemblant les résultats entraînera probablement un conflit de type sur la deuxième ligne.

+0

thx à vous aussi !!! cela fonctionne aussi – MacTee