Sqlserver
 sql >> Database >  >> RDS >> Sqlserver

Istruzione di selezione multipla nella procedura memorizzata per ottenere il risultato desiderato

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