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 3676274
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 19, 20262026-05-19T03:04:47+00:00 2026-05-19T03:04:47+00:00

Need help speeding up a stored procedure ALTER PROCEDURE [dbo].[yearlynetsales] ( @YearToGet int, @current

  • 0

Need help speeding up a stored procedure

  ALTER PROCEDURE [dbo].[yearlynetsales]
    (
        @YearToGet int, 
        @current datetime
    ) 
    AS
        SET @YearToGet = 2006;

        WITH Years AS (
            SELECT DATEPART(year, GETDATE()) [Year]
            UNION ALL
            SELECT [Year]-1 FROM Years WHERE [Year]>@YearToGet 
        ),
    q_00 as (
    select
          DIVISION
         , DYYYY
         , sum(PARTY)         as asofPAX        
        , sum(NetAmount)     as asofSales 
        , sum(OtherAmount)     as otherSales         
    from dbo.B101BookingsDetails 
    INNER JOIN Years                     ON B101BookingsDetails.DYYYY = Years.Year
    where SDESCR not in (select * from dbo.regionexlude) and Booked <= CONVERT(int,DateAdd(year, Years.Year  - Year(getdate()), DateAdd(day, DateDiff(day, 0, getdate()), 1)))
      and DYYYY = Years.Year and SDESCR not in (select * from dbo.regionexlude) and SDESCR  IS NOT NULL
    group by DIVISION, DYYYY, years.year
    having  DYYYY = years.year
    ),
    q_01 as (
    select     
          DIVISION 
        , DYYYY 
        , sum(PARTY)         as YEPAX 
        , sum(NetAmount)     as YESales
     , sum(OtherAmount)     as YEotherSales
    from  dbo.B101BookingsDetails 
    INNER JOIN Years                     ON B101BookingsDetails.DYYYY = Years.Year
    where SDESCR not in (select * from dbo.regionexlude) 
    group by DIVISION,  DYYYY , years.year
    having  DYYYY = years.year
    ),
    q_02 as (
    select
          DIVISION
        , DYYYY
        , sum(PARTY)         as CurrentPAX        
        , sum(NetAmount)     as CurrentSales  
        , sum(OtherAmount)     as CurrentotherSales           
        from dbo.B101BookingsDetails 
    INNER JOIN Years                     ON B101BookingsDetails.DYYYY = Years.Year
    where SDESCR not in (select * from dbo.regionexlude) and  Booked <= CONVERT(int,@current)
      and DYYYY = (year( getdate() )) and SDESCR not in (select * from dbo.regionexlude) 
    group by DIVISION,  DYYYY 

    )

    select
          a.DIVISION 
         , a.DYYYY
        , asofPAX        
        , (asofSales + othersales) as asofSales      
       , YEPAX 
        , (YESales + YEothersales) as YESales
          , CurrentPAX 
        , (CurrentSales + Currentothersales) as CurrentSales  
      ,(asofSales + othersales)/ ISNULL(NULLIF((YESales + YEothersales),0),1) as percentsales,
               CAST((asofpax) AS DECIMAL(5,1))/yepax as percentpax 




    from q_00 as a
    join q_01 as b on (b.DIVISION = a.DIVISION  and b.DYYYY = a.DYYYY) 
    join q_02 as c on (b.DIVISION = c.DIVISION)
    JOIN Years as d on (b.dyyyy = d.year)
    where A.DYYYY <> (year( getdate() )) 
    order by a.DIVISION,  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-19T03:04:47+00:00Added an answer on May 19, 2026 at 3:04 am

    Wow, lots of places to tweak this; let’s start with changing the NOTs to LEFT JOIN’s with NULLs:

         q_01 AS ( SELECT    DIVISION
              , DYYYY
              , SUM(PARTY) AS YEPAX
              , SUM(NetAmount) AS YESales
              , SUM(OtherAmount) AS YEotherSales
      FROM      dbo.B101BookingsDetails
                INNER JOIN Years ON B101BookingsDetails.DYYYY = Years.Year
                LEFT JOIN dbo.regionexlude r ON SDESCR = r.columnname
      WHERE     r.columnname IS NULL
         /*where SDESCR not in (select * from dbo.regionexlude)*/
      GROUP BY  DIVISION
              , DYYYY
              , years.year
      HAVING    DYYYY = years.year  ),    
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I need your help. I have stored selected indexpath values in an NSMutableArray from
Need help with this SQL Server 2000 procedure. The problem is made difficult because
Need help. It may be weird. First activity has listview(like lazyadapter) once i click
need help to create regular expression matching string www.*.abc.*/somestring Here * is wild card
Need help writing a script downloads data from google insight using c# this is
need help/guide for sql select query, I have 2 table stock and stock_history, in
need help regarding USSD Gateway. I have to develop an app, which will directly
need help in error in database pivot. i have table tamed table_score like below:
I need help how to use correctly the javascript : document.cookie or how to
I need help getting the this week full date range in the following format:

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.