开发者

stored procedure issue, has to do with my where clause and if statement

right now my stored procedure is returning 2 different result sets one for @booked and the other for @booked1

if you look closely my query is doing the same thing for each @booked and @booked but one is for a user selected year and the other for the current year.

I don't want two different result sets, i want to join the selected year and the current year side by side by SDESCR(which is a column that they have in common)

another hurdle i am facing is i am use @mode to decide whether the user wants netsales, sales... so on.

I know i need sometype of join but, it isnt working because i have a where statement that says where dyyyy= @yeartoget

which won't allow the current year data to work

ALTER PROCEDURE [dbo].[test1]
@mode varchar(20),
@YearToGet int



AS
SET NOCOUNT ON

Declare @Booked Int
Set @Booked = CONVERT(int,DateAdd(year, @YearToGet - Year(getdate() + 1),                   
                DateAdd(day, DateDiff(day, 1, getdate()), 1) ) )

Declare @Booked1 Int
Set @Booked1 = CONVERT(int,DateAdd(year,  (year( getdate() )) - Year(getdate() + 1),                   
                DateAdd(day, DateDiff(day, 1, getdate()), 1) ) )


 If @mode = 'Sales'
      Select
           Division,
           SDESCR,
           DYYYY,

       Sum(Case When Booked <= @Booked Then NetAmount End) ASofNetSales,        
       SUM(NetAmount) AS YENetSales,

       Sum(Case When Booked <= @Booked Then PARTY End) AS ASofPAX,        
       SUM(PARTY) AS YEPAX


      From   dbo.B101BookingsDetails
      Where  DYYYY = @YearToGet
      Group By SDESCR, DYYYY, Division           
      Order By Division, SDESCR, DYYYY

else if @mode = 'netsales'

Select Division, 
       SDESCR,        
       DYYYY,  


       Sum(Case When Booked <= @Booked Then NetAmount End) ASofNetSales,        
       SUM(NetAmount) AS YENetSales,

       Sum(Case When Booked <= @Booked Then PARTY End) AS ASofPAX,        
       SUM(PARTY) AS YEPAX


From   dbo.B101BookingsDetails 
Where  DYYYY = @YearToGet
Group By SDESCR, DYYYY, Division
Order By Division, SDESCR, DYYYY 


If @mode = 'Sales'
      Select
           Division,
           SDESCR,
           DYYYY,

       Sum(Case When Booked <= @Booked1 Then NetAmount End) currentNetSales,       
       Sum(Case When Booked <= @Booked1 Then PARTY End) AS currentPAX        



      From   dbo.B101BookingsDetails
      Where  DYYYY = (year( getdate() ))
      Group By SDESCR, DYYYY, Division           
      Order By Division, SDESCR, DYYYY

else if @mode = 'netsales'

Select Division, 
       SDESCR,        
       DYYYY,  


       Sum(Case When Booked <= @Booked1 Then NetAmount End) currentNetSales,        


       Sum(Case When Booked <= @Booked1 Then PARTY End) AS currentPAX      开发者_如何学Go



From   dbo.B101BookingsDetails 
Where  DYYYY = (year( getdate() ))
Group By SDESCR, DYYYY, Division
Order By Division, SDESCR, DYYYY 

Else if @mode = 'Inssales'

Select Division, 
       SDESCR,        
       DYYYY,  

       Sum(Case When Booked <= @Booked1 Then InsAmount End) currentInsSales,        

       Sum(Case When Booked <= @Booked1 Then PARTY End) AS currentPAX        

From   dbo.B101BookingsDetails 
Where  DYYYY = (year( getdate() ))
Group By SDESCR, DYYYY, Division
Order By Division, SDESCR, DYYYY 


One easy approach to have the side by side results is to use sub-queries in side the From

Note: this is just for mode = Sales

SELECT
           b.Division,
           b.SDESCR,
           b.DYYYY,
           b.YENetSales,
           b.YEPAX
           b1.Division,
           b1.SDESCR,
           b1.DYYYY,
           b1.currentNetSales,
           b1.currentPAX     

FROM
(Select
       Division,
       SDESCR,
       DYYYY,

   Sum(Case When Booked <= @Booked Then NetAmount End) ASofNetSales,        
   SUM(NetAmount) AS YENetSales,

   Sum(Case When Booked <= @Booked Then PARTY End) AS ASofPAX,        
   SUM(PARTY) AS YEPAX


  From   dbo.B101BookingsDetails
  Where  DYYYY = @YearToGet
  Group By SDESCR, DYYYY, Division         
) b

  FULL OUTER JOIN 
  (
Select
       Division,
       SDESCR,
       DYYYY,

   Sum(Case When Booked <= @Booked1 Then NetAmount End) currentNetSales,       
   Sum(Case When Booked <= @Booked1 Then PARTY End) AS currentPAX     
  From   dbo.B101BookingsDetails
  Where  DYYYY = (year( getdate() ))
  Group By SDESCR, DYYYY, Division  ) b1
  ON b.divsion = b1.divsion
     and
     b.SDESCR = b1.SDESCR   --might not be required

 Order By b.Division, b.SDESCR, b.DYYYY

The other approach is to change your where clause to include both @booked and @booked1 and then do a case statement on each field

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜