CREATE PROCEDURE [dbo].[fetch_projects_by_month]
    @monthDate nvarchar(max) = '',
    @whereCondn nvarchar(max) = '',
    @salesId nvarchar(max) = ''
AS
    declare @select_query nvarchar(max) = '';
    SET @select_query = 'select dc.customer_id,dc.company_name,dca.account_id,dca.account_name,fp.project_id, dcp.project_name, dcp.amount_in_inr,dcp.project_amount,
       fp.sales_person_id,fp.shared_with, salesperson.display_name as salesperson_name, shared.display_name as shared_name,
       dcp.closed_date, dcp.start_date, dcp.actual_delivery_date, dcp.expected_delivery_date,' +
                        '(fp.inr_rate * dcp.project_amount) as project_amount_inr,' +
                        ' fp.inr_rate as inr_rate,' +
                        '(invoice_amount*inr_rate) as invoice_amount,' +
                        '((isnull(project_amount,0) - isnull(project_billed,0)) * isnull(inr_rate,1)) as unrealized_amount,(invoice_pending_amount) as invoice_pending_amount
from fact_projects as fp
    join dim_customer_project dcp on fp.project_id = dcp.project_id
    join dim_customer dc on dc.customer_id = fp.customer_id
    join dim_customer_accounts dca on fp.account_id = dca.account_id
    left join dim_users as salesperson on salesperson.user_id = fp.sales_person_id
    left join dim_users as shared on shared.user_id = fp.shared_with ' ;

    SET @select_query = @select_query + @whereCondn
    IF  @salesId != ''
        BEGIN
            SET @select_query = @select_query + ' AND ( fp.sales_person_id IN (' + @salesId + ') OR fp.shared_with IN ('+ @salesId+'))';
        END
    SET @select_query = @select_query + ' ORDER BY dcp.customer_id,dcp.closed_date'
    EXEC sp_executesql @select_query;
go

