Q:

Using TOP clause parametrically in joined tables

I have joined some tables to gather some information from and I am trying to use TOP clause parametrically in SQL Server 2008 R2. Here is my code:
    DECLARE @num INT
    SELECT 
        TOP (@num) installTicketItem.[id] AS ItemID, it.[id], it.[usr], it.[openDate],it.[closeDate],it.[saleId],it.[ticketType],it.[ticketDesc],it.[agent],
        CASE WHEN(resultId = 3 AND usr= it.usr) THEN 1 ELSE 0 END AS fault,
        CASE WHEN EXISTS(SELECT * FROM installTicket WHERE ticketType= 'modem' AND usr= it.usr AND closeDate IS Null) AND ticketType <> 'modem' THEN 1 ELSE 0 END AS hasModem, 
        CASE WHEN ((payment IS NULL) AND (installer IS NOT NULL)) THEN 1 ELSE 0 END AS notPaid 
    FROM installTicket it 
        JOIN (SELECT ticketId, MAX(id) AS maxID, 

        SET @num = COUNT(DISTINCT ticketId) FROM installTicketItem GROUP BY ticketId) AS iti ON iti.ticketId = it.id

        JOIN operator ON agent = username
        JOIN installPolicy ON installPolicy.agent = parentagent
        JOIN installManage ON installPolicy.panelUsr = installManage.panelUsr
        JOIN installTicketItem ON it.id = installTicketItem.ticketId
        JOIN installResult ON installResult.id = installTicketItem.resultId 

    WHERE   
        it.closeDate IS NULL AND ticketType = 'install' AND managerUsr = 'adminPanel2' AND done = 1

    ORDER BY 
        ItemID DESC, id DESC

I want to set @num parameter in a nested select as you see. 
Is there any way to do that? I appreciate that if anybody could help me.

A:

Hi based on your question, you should use this query as
DECLARE @num INT

        Set @num = 
        ( 
            SELECT 
                COUNT(DISTINCT iti.ticketId) 
            FROM 
                installTicketItem  iti join  installTicket it ON iti.ticketId = it.id 
            WHERE 
                it.closeDate IS NULL AND ticketType = 'install' AND managerUsr = 'adminPanel2' AND done = 1
            GROUP BY 
                ticketId
        )

        SELECT 
            TOP (@num) installTicketItem.[id] AS ItemID, it.[id], it.[usr], it.[openDate],it.[closeDate],it.[saleId],it.[ticketType],it.[ticketDesc],it.[agent],
            CASE WHEN(resultId = 3 AND usr= it.usr) THEN 1 ELSE 0 END AS fault,
            CASE WHEN EXISTS(SELECT * FROM installTicket WHERE ticketType= 'modem' AND usr= it.usr AND closeDate IS Null) AND ticketType <> 'modem' THEN 1 ELSE 0 END AS hasModem, 
            CASE WHEN ((payment IS NULL) AND (installer IS NOT NULL)) THEN 1 ELSE 0 END AS notPaid 
        FROM installTicket it 
            JOIN (SELECT ticketId, MAX(id) AS maxID

            --, SET @num = COUNT(DISTINCT ticketId) 
            FROM installTicketItem GROUP BY ticketId) AS iti ON iti.ticketId = it.id

            JOIN operator ON agent = username
            JOIN installPolicy ON installPolicy.agent = parentagent
            JOIN installManage ON installPolicy.panelUsr = installManage.panelUsr
            JOIN installTicketItem ON it.id = installTicketItem.ticketId
            JOIN installResult ON installResult.id = installTicketItem.resultId 

        WHERE   
            it.closeDate IS NULL AND ticketType = 'install' AND managerUsr = 'adminPanel2' AND done = 1

        ORDER BY 
            ItemID DESC, id DESC

