将两个表放在一起求和

时间:2018-07-10 15:35:07

标签: sql-server sql-server-2008

如何从newsaleshistory表中加上BWlease表的所有数据?仅包括租赁pymts和零新闻销售历史记录。

仅当它们具有来自BWlease表的租约amt时,我的输出才是总和。所有其他帐户均为零。

输出应该是newsaleshistory表中的所有修订,以及BWlease表中的所有租赁,无论是否存在。这将是按月份和帐户。同样,将包括BWlease表中没有出现在帐户newsaleshistory表中的任何帐户租赁资产。像一个联盟所有人。

我有下面列出的代码。

SELECT  top 50 [NewSalesHistory].[Region]
                ,[NewSalesHistory].[ShipTo]
                ,[Customer].[Name]
                ,[CustomerMap].[SP]
                ,[Customer].[SalesOffice]
                ,[SalesOffice].[City] + ' , ' + ' ' + [SalesOffice].[State] SOName
                ,[Customer].[SalesOffice] + '-' + [SalesOffice].[City] + ' , ' + ' ' + [SalesOffice].[State] BRLocation
                ,coalesce([CustomerMap].[SH-Y1], ' ') as 'Y1 Partner'
                ,coalesce([customer].[strat1], ' ') as 'STRAT 1'
                ,coalesce([customer].[strat3], ' ') as 'STRAT 3' 
                ,[salesoffice].[AVPName]
                ,[salesoffice].[DMName]
                ,SUBSTRING ([IndustryCode].[Description],1,3) as 'Market'
                ,[IndustryCode].[Description] as 'Industry'
                ,[newsaleshistory].[AccountType]
                ,SUM(case when [NewSalesHistory].[billingdate] between '6/1/2016' and '6/30/2016' then newbillingdata else 0 end ) + [X].[Jun-16]  AS 'Jun-16'
                ,SUM(case when [NewSalesHistory].[billingdate] between '7/1/2016' and '7/31/2016' then newbillingdata else 0 end ) + [X].[Jul-16]  as 'Jul-16'
                ,SUM(case when [NewSalesHistory].[billingdate] between '8/1/2016' and '8/31/2016' then newbillingdata else 0 end ) + [X].[Aug-16]  as 'Aug-16'
                ,SUM(case when [NewSalesHistory].[billingdate] between '9/1/2016' and '9/30/2016' then newbillingdata else 0 end ) + [X].[Sep-16]  as 'Sep-16'
                ,SUM(case when [NewSalesHistory].[billingdate] between '10/1/2016' and '10/31/2016' then newbillingdata else 0 end ) + [X].[Oct-16]  as 'Oct-16'
                ,SUM(case when [NewSalesHistory].[billingdate] between '11/1/2016' and '11/30/2016' then newbillingdata else 0 end ) + [X].[Nov-16]  as 'Nov-16'
                ,SUM(case when [NewSalesHistory].[billingdate] between '12/1/2016' and '12/31/2016' then newbillingdata else 0 end ) + [X].[Dec-16]  as 'Dec-16'
                ,SUM(case when [NewSalesHistory].[billingdate] between '1/1/2017' and '1/31/2017' then newbillingdata else 0 end ) + [X].[Jan-17]  as 'Jan-17'
                ,SUM(case when [NewSalesHistory].[billingdate] between '2/1/2017' and '2/28/2017' then newbillingdata else 0 end ) + [X].[Feb-17]  as 'Feb-17'
                ,SUM(case when [NewSalesHistory].[billingdate] between '3/1/2017' and '3/31/2017' then newbillingdata else 0 end ) + [X].[Mar-17]  as 'Mar-17'
                ,SUM(case when [NewSalesHistory].[billingdate] between '4/1/2017' and '4/30/2017' then newbillingdata else 0 end ) + [X].[Apr-17]  as 'Apr-17'
                ,SUM(case when [NewSalesHistory].[billingdate] between '5/1/2017' and '5/31/2017' then newbillingdata else 0 end ) + [X].[May-17]  as 'May-17'
                ,SUM(case when [NewSalesHistory].[billingdate] between '6/1/2017' and '6/30/2017' then newbillingdata else 0 end ) + [X].[Jun-17]  as 'Jun-17'
                ,SUM(case when [NewSalesHistory].[billingdate] between '7/1/2017' and '7/31/2017' then newbillingdata else 0 end ) + [X].[Jul-17]  as 'Jul-17'
                ,SUM(case when [NewSalesHistory].[billingdate] between '8/1/2017' and '8/31/2017' then newbillingdata else 0 end ) + [X].[Aug-17]  as 'Aug-17'
                ,SUM(case when [NewSalesHistory].[billingdate] between '9/1/2017' and '9/30/2017' then newbillingdata else 0 end ) + [X].[Sep-17]  as 'Sep-17'
                ,SUM(case when [NewSalesHistory].[billingdate] between '10/1/2017' and '10/31/2017' then newbillingdata else 0 end ) + [X].[Oct-17]  as 'Oct-17'
                ,SUM(case when [NewSalesHistory].[billingdate] between '11/1/2017' and '11/30/2017' then newbillingdata else 0 end ) + [X].[Nov-17]  as 'Nov-17'
                ,SUM(case when [NewSalesHistory].[billingdate] between '12/1/2017' and '12/31/2017' then newbillingdata else 0 end ) + [X].[Dec-17]  as 'Dec-17'
                ,SUM(case when [NewSalesHistory].[billingdate] between '1/1/2018' and '1/31/2018' then newbillingdata else 0 end ) + [X].[Jan-18]  as 'Jan-18'
                ,SUM(case when [NewSalesHistory].[billingdate] between '2/1/2018' and '2/28/2018' then newbillingdata else 0 end ) + [X].[Feb-18]  as 'Feb-18'
                ,SUM(case when [NewSalesHistory].[billingdate] between '3/1/2018' and '3/31/2018' then newbillingdata else 0 end ) + [X].[Mar-18]  as 'Mar-18'
                ,SUM(case when [NewSalesHistory].[billingdate] between '4/1/2018' and '4/30/2018' then newbillingdata else 0 end ) + [X].[Apr-18]  as 'Apr-18'
                ,SUM(case when [NewSalesHistory].[billingdate] between '5/1/2018' and '5/31/2018' then newbillingdata else 0 end ) + [X].[May-18]  as 'May-18'
                ,SUM(case when [NewSalesHistory].[billingdate] between '6/1/2016' and '5/31/2018' then newbillingdata else 0 end ) + [X].[24 Month]  as '24 Month'
                ,SUM(case when [NewSalesHistory].[billingdate] between '6/1/2017' and '5/31/2018' then newbillingdata else 0 end ) + [X].[12 Month]  as '12 Month'
      FROM [NewSalesHistory]
      left  join [Customer] on [newsaleshistory].[ShipTo] = [customer].[customer]
      left  join [SalesOffice] on [Customer].[SalesOffice] = [salesoffice].[salesoffice]
      left  join [IndustryCode] on [NewSalesHistory].[IndustryCode] = [IndustryCode].[IndustryCode]
      left  join [CustomerMap] on [NewSalesHistory].[ShipTo] = [CustomerMap].[SH]
      FULL join (SELECT      [Shipto]
                ,SUM(case when [BWLease].[billingdate] between '6/1/2016' and '6/30/2016' then ExtPrice else 0 end ) as 'Jun-16'
                ,SUM(case when [BWLease].[billingdate] between '7/1/2016' and '7/31/2016' then ExtPrice else 0 end ) as 'Jul-16'
                ,SUM(case when [BWLease].[billingdate] between '8/1/2016' and '8/31/2016' then ExtPrice else 0 end ) as 'Aug-16'
                ,SUM(case when [BWLease].[billingdate] between '9/1/2016' and '9/30/2016' then ExtPrice else 0 end ) as 'Sep-16'
                ,SUM(case when [BWLease].[billingdate] between '10/1/2016' and '10/31/2016' then ExtPrice else 0 end ) as 'Oct-16'
                ,SUM(case when [BWLease].[billingdate] between '11/1/2016' and '11/30/2016' then ExtPrice else 0 end ) as 'Nov-16'
                ,SUM(case when [BWLease].[billingdate] between '12/1/2016' and '12/31/2016' then ExtPrice else 0 end ) as 'Dec-16'
                ,SUM(case when [BWLease].[billingdate] between '1/1/2017' and '1/31/2017' then ExtPrice else 0 end ) as 'Jan-17'
                ,SUM(case when [BWLease].[billingdate] between '2/1/2017' and '2/28/2017' then ExtPrice else 0 end ) as 'Feb-17'
                ,SUM(case when [BWLease].[billingdate] between '3/1/2017' and '3/31/2017' then ExtPrice else 0 end ) as 'Mar-17'
                ,SUM(case when [BWLease].[billingdate] between '4/1/2017' and '4/30/2017' then ExtPrice else 0 end ) as 'Apr-17'
                ,SUM(case when [BWLease].[billingdate] between '5/1/2017' and '5/31/2017' then ExtPrice else 0 end ) as 'May-17'
                ,SUM(case when [BWLease].[billingdate] between '6/1/2017' and '6/30/2017' then ExtPrice else 0 end ) as 'Jun-17'
                ,SUM(case when [BWLease].[billingdate] between '7/1/2017' and '7/31/2017' then ExtPrice else 0 end ) as 'Jul-17'
                ,SUM(case when [BWLease].[billingdate] between '8/1/2017' and '8/31/2017' then ExtPrice else 0 end ) as 'Aug-17'
                ,SUM(case when [BWLease].[billingdate] between '9/1/2017' and '9/30/2017' then ExtPrice else 0 end ) as 'Sep-17'
                ,SUM(case when [BWLease].[billingdate] between '10/1/2017' and '10/31/2017' then ExtPrice else 0 end ) as 'Oct-17'
                ,SUM(case when [BWLease].[billingdate] between '11/1/2017' and '11/30/2017' then ExtPrice else 0 end ) as 'Nov-17'
                ,SUM(case when [BWLease].[billingdate] between '12/1/2017' and '12/31/2017' then ExtPrice else 0 end ) as 'Dec-17'
                ,SUM(case when [BWLease].[billingdate] between '1/1/2018' and '1/31/2018' then ExtPrice else 0 end ) as 'Jan-18'
                ,SUM(case when [BWLease].[billingdate] between '2/1/2018' and '2/28/2018' then ExtPrice else 0 end ) as 'Feb-18'
                ,SUM(case when [BWLease].[billingdate] between '3/1/2018' and '3/31/2018' then ExtPrice else 0 end ) as 'Mar-18'
                ,SUM(case when [BWLease].[billingdate] between '4/1/2018' and '4/30/2018' then ExtPrice else 0 end ) as 'Apr-18'
                ,SUM(case when [BWLease].[billingdate] between '5/1/2018' and '5/31/2018' then ExtPrice else 0 end ) as 'May-18'
                ,SUM(case when [BWLease].[billingdate] between '6/1/2016' and '5/31/2018' then ExtPrice else 0 end ) as '24 Month'
                ,SUM(case when [BWLease].[billingdate] between '6/1/2017' and '5/31/2018' then ExtPrice else 0 end ) as '12 Month'
      FROM [AirgasPricing].[dbo].[BWLease]
      GROUP BY [Shipto]) X ON [NewSalesHistory].[ShipTo] = [X].[Shipto]
            where [NewSalesHistory].[Region] = 'MSO'
                and [NewSalesHistory].[PH1] not in ('LL')
                --and [NewSalesHistory].[ShipTo] = '1002996'
      GROUP BY [NewSalesHistory].[Region]
                ,[NewSalesHistory].[ShipTo]
                ,[Customer].[Name]
                ,[CustomerMap].[SP]
                ,[Customer].[SalesOffice]
                ,[SalesOffice].[City] + ' , ' + ' ' + [SalesOffice].[State]
                ,[Customer].[SalesOffice] + '-' + [SalesOffice].[City] + ' , ' + ' ' + [SalesOffice].[State]
                ,[CustomerMap].[SH-Y1]
                ,[customer].[strat1]
                ,[customer].[strat3]
                ,[salesoffice].[AVPName]
                ,[salesoffice].[DMName]
                ,SUBSTRING ([IndustryCode].[Description],1,3)
                ,[IndustryCode].[Description]
                ,[newsaleshistory].[AccountType]
                ,[X].[Jun-16]
                ,[X].[Jul-16]
                ,[X].[Aug-16]
                ,[X].[Sep-16]
                ,[X].[Oct-16]
                ,[X].[Nov-16]
                ,[X].[Dec-16]
                ,[X].[Jan-17]
                ,[X].[Feb-17]
                ,[X].[Mar-17]
                ,[X].[Apr-17]
                ,[X].[May-17]
                ,[X].[Jun-17]
                ,[X].[Jul-17]
                ,[X].[Aug-17]
                ,[X].[Sep-17]
                ,[X].[Oct-17]
                ,[X].[Nov-17]
                ,[X].[Dec-17]
                ,[X].[Jan-18]
                ,[X].[Feb-18]
                ,[X].[Mar-18]
                ,[X].[Apr-18]
                ,[X].[May-18]
                ,[X].[24 Month]
                ,[X].[12 Month]
          ORDER BY [NewSalesHistory].[Region]
                ,[NewSalesHistory].[ShipTo]
                ,[Customer].[Name]
                ,[CustomerMap].[SP]
                ,[Customer].[SalesOffice]
                ,[SalesOffice].[City] + ' , ' + ' ' + [SalesOffice].[State]
                ,[Customer].[SalesOffice] + '-' + [SalesOffice].[City] + ' , ' + ' ' + [SalesOffice].[State]
                ,[CustomerMap].[SH-Y1]
                ,[customer].[strat1]
                ,[customer].[strat3]
                ,[salesoffice].[AVPName]
                ,[salesoffice].[DMName]
                ,SUBSTRING ([IndustryCode].[Description],1,3)
                ,[IndustryCode].[Description]
                ,[newsaleshistory].[AccountType]

0 个答案:

没有答案