Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 3624484
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T23:30:49+00:00 2026-05-18T23:30:49+00:00

Alright so i am not even sure if this is possible I have a

  • 0

Alright so i am not even sure if this is possible
I have a q_00 and q_01 and q_02 which are all in my stored procedure.
then on the bottom i have 3 select statements
that select a certain catagory for example Sales,Net Sales and INS sales

What i want to be able to do is if the user types exec (name of my sp) (sales) (and a year which is the @yearparameter) it will run the sales select statement

If they type Exec (name of my SP) netsales (@Yeartoget) it will show the net sales is this possible or do i need multiple stored procedures

   ALTER PROCEDURE [dbo].[casof]
 @YearToGet int,
 @mode VARCHAR(20)
 as
;
with
q_00 as (
select
      DIVISION
    , SDESCR
    , DYYYY
    , sum(APRICE)        as asofSales 
    , sum(PARTY)         as asofPAX        
    , sum(NetAmount)     as asofNetSales        
    , sum(InsAmount)     as asofInsSales        
    , sum(CancelRevenue) as asofCXSales        
    , sum(OtherAmount)   as asofOtherSales        
    , sum(CXVALUE)       as asofCXValue  
from dbo.B101BookingsDetails 
where Booked <= CONVERT(int,DateAdd(year, @YearToGet - Year(getdate()), DateAdd(day, DateDiff(day, 1, getdate()), 0)))
  and DYYYY = @YearToGet
group by DIVISION, SDESCR, DYYYY 
),
q_01 as (
select     
      DIVISION 
    , SDESCR
    , DYYYY 
    , sum(APRICE)        as YESales 
    , sum(PARTY)         as YEPAX 
    , sum(NetAmount)     as YENetSales
    , sum(InsAmount)     as YEInsSales 
    , sum(CancelRevenue) as YECXSales 
    , sum(OtherAmount)   as YEOtherSales
    , sum(CXVALUE)       as YECXValue
from  dbo.B101BookingsDetails 
where DYYYY=@YearToGet
group by DIVISION, SDESCR, DYYYY 
),
q_02 as (
select
      DIVISION
    , SDESCR
    , DYYYY
    , sum(APRICE)        as CurrentSales 
    , sum(PARTY)         as CurrentPAX        
    , sum(NetAmount)     as CurrentNetSales        
    , sum(InsAmount)     as CurrentInsSales        
    , sum(CancelRevenue) as CurrentCXSales        
    , sum(OtherAmount)   as CurrentOtherSales        
    , sum(CXVALUE)       as CurrentCXValue  
from dbo.B101BookingsDetails 
where Booked <= CONVERT(int,DateAdd(year, (year( getdate() )) - Year(getdate()), DateAdd(day, DateDiff(day, 1, getdate()), 0)))
  and DYYYY = (year( getdate() ))
group by DIVISION, SDESCR, DYYYY 
)

IF @mode = 'sales'
select
      a.DIVISION 
    , a.SDESCR
    , a.DYYYY
    , asofSales 
    , asofPAX        
    , YESales 
    , YEPAX 
    , CurrentSales 
    , CurrentPAX 
    , asofsales/ ISNULL(NULLIF(yesales,0),1) as percentsales
    , asofpax/yepax as percentpax
    ,currentsales/ISNULL(NULLIF((asofsales/ISNULL(NULLIF(yesales,0),1)),0),1) as projectedsales
    ,currentpax/ISNULL(NULLIF((asofpax/ISNULL(NULLIF(yepax,0),1)),0),1) as projectedpax
from q_00 as a
join q_01 as b on (b.DIVISION = a.DIVISION and b.SDESCR = a.SDESCR and b.DYYYY = a.DYYYY) 
join q_02 as c on (b.DIVISION = c.DIVISION and b.SDESCR = c.SDESCR)
order by a.DIVISION, a.SDESCR, a.DYYYY ;


else if @mode= 'netsales'

select
      a.DIVISION 
    , a.SDESCR
    , a.DYYYY
    , asofPAX        
    , asofNetSales        
    , YEPAX 
    , YENetSales
    , CurrentPAX 
    , CurrentNetSales
    , asofnetsales/ ISNULL(NULLIF(yenetsales,0),1) as percentnetsales
    , asofpax/yepax as percentpax


,currentnetsales/ISNULL(NULLIF((asofnetsales/ISNULL(NULLIF(yenetsales,0),1)),0),1) as projectednetsales
,currentpax/ISNULL(NULLIF((asofpax/ISNULL(NULLIF(yepax,0),1)),0),1) as projectedpax

from q_00 as a
join q_01 as b on (b.DIVISION = a.DIVISION and b.SDESCR = a.SDESCR and b.DYYYY = a.DYYYY) 
join q_02 as c on (b.DIVISION = c.DIVISION and b.SDESCR = c.SDESCR)
order by a.DIVISION, a.SDESCR, a.DYYYY ;

 ELSE IF @mode = 'inssales'

select
      a.DIVISION 
    , a.SDESCR
    , a.DYYYY
    , asofPAX     
    , asofInsSales        
    , YEPAX 
    , YEInsSales 
    , CurrentPAX 
    , CurrentInsSales 
    , asofinssales/ ISNULL(NULLIF(yeinssales,0),1) as percentsales
    , asofpax/yepax as percentpax
    ,currentinssales/ISNULL(NULLIF((asofinssales/ISNULL(NULLIF(yeinssales,0),1)),0),1) as projectedinssales

from q_00 as a
join q_01 as b on (b.DIVISION = a.DIVISION and b.SDESCR = a.SDESCR and b.DYYYY = a.DYYYY) 
join q_02 as c on (b.DIVISION = c.DIVISION and b.SDESCR = c.SDESCR)
order by a.DIVISION, a.SDESCR, a.DYYYY ;
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-18T23:30:50+00:00Added an answer on May 18, 2026 at 11:30 pm

    Just add another parameter called say @mode and use if @mode='sales' to perform conditional logic.

    Following your update though I’d be tempted to create two helper parameterised inline TVFs.

    CREATE FUNCTION dbo.AggregateBookingDetails  
    (   
        @Booked datetime, 
        @YearToGet int
    )
    RETURNS TABLE 
    AS
    RETURN 
    (
    select
          DIVISION
        , SDESCR
        , DYYYY
        , sum(APRICE)        as Sales 
        , sum(PARTY)         as PAX        
        , sum(NetAmount)     as NetSales        
        , sum(InsAmount)     as InsSales        
        , sum(CancelRevenue) as CXSales        
        , sum(OtherAmount)   as OtherSales        
        , sum(CXVALUE)       as CXValue  
    from dbo.B101BookingsDetails 
    where @Booked IS NULL OR Booked <= @Booked
      and DYYYY = @YearToGet
    group by DIVISION, SDESCR, DYYYY 
    )
    

    GO

    CREATE FUNCTION fn_casof
    (   
        @YearToGet int
    )
    RETURNS TABLE 
    AS
    RETURN 
    (
    select       
          a.DIVISION
        , a.SDESCR
        , a.DYYYY
        , a.Sales as a_Sales
        , b.Sales as b_Sales
        , c.Sales as c_Sales
        , .... /*etc. etc*/
    from dbo.AggregateBookingDetails(CONVERT(int,DateAdd(year, @YearToGet - Year(getdate()), DateAdd(day, DateDiff(day, 1, getdate()), 0))), @YearToGet) as a
    join dbo.AggregateBookingDetails(NULL, @YearToGet) as b on (b.DIVISION = a.DIVISION and b.SDESCR = a.SDESCR and b.DYYYY = a.DYYYY) 
    join dbo.AggregateBookingDetails(CONVERT(int,DateAdd(year, (year( getdate() )) - Year(getdate()), DateAdd(day, DateDiff(day, 1, getdate()), 0))), year( getdate() )) as c on (b.DIVISION = c.DIVISION and b.SDESCR = c.SDESCR)
    )
    

    Your stored procedure conditional logic would then just need to select the desired columns from the second of these TVFs.

    ALTER PROCEDURE [dbo].[casof]
    @YearToGet int,
    @mode VARCHAR(20)
    as
    IF (@mode='sales')
    SELECT collist1 FROM dbo.fn_casof(@yeartoget)
    ELSE
        IF (@mode='netsales')
        SELECT collist2 FROM dbo.fn_casof(@yeartoget)
        ELSE
        SELECT collist3 FROM dbo.fn_casof(@yeartoget)
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Alright, I'm not sure if this question has been asked before so if it
Alright so I have no idea how to even begin doing this But basically
Alright, I am not entirely sure if I will explain this sufficiently, but here
Alright, so I'm about 90% sure that this isn't possible, but I thought I'd
Alright, I'm not very knowledgeable in Java, and I don't even know if this
Alright, I'll try to explain this the best I can. I have an iPhone
Alright, I have been breaking my head over this one for more than a
Alright, I'm new to VBA but I know this has to be possible. I
Alright, so I know about the Settings.Properties.Default.* Stuff, but thats not what im trying
Alright I don't see why this isnt working. It seems pretty simple. Here is

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.