Sqlserver
 sql >> Base de Dados >  >> RDS >> Sqlserver

Instrução de seleção múltipla no procedimento armazenado para obter o resultado desejado


http://www.sqlfiddle.com/#!3/f5fe6/5
WITH A AS (
    SELECT top(1) WITH ties Q.TechID
        ,QuestionID
        ,QuestionTitle
        ,DatePosted
        ,Username
    FROM tblTechnology T LEFT JOIN tblQuestions Q ON Q.TechID = T.TechID
    ORDER BY row_number() over(partition BY Q.TechID ORDER BY Dateposted DESC)
)
SELECT * FROM A
OUTER apply (SELECT count(QuestionDesc) Totalposts, sum(ReplyCount) ReplyCount
    FROM tblQuestions WHERE A.TechID=tblQuestions.TechID) D