如何在单独的列中进行两个单独的和结果

时间:2015-11-12 22:49:00

标签: sql-server database join sum

我有这个问题,我只是不确定这是否可行。我仍然是MSSQL的新手。

我想要完成的是在12个月期间和6个月期间显示客户的传记数据和他们的奖励收入,并显示相同的结果,以便在他们自己的专栏中显示总和。

我可以通过单独运行每个查询并更改我的Where条件时间来单独完成此操作,但我无法弄清楚如何为客户获得每列的独立结果。我只是不知道如何使我的where语句仅适用于相加的列。我尝试了第二个12个月列的子查询,但它返回的结果超过1,这是不允许的,我需要多个结果。以下是我到目前为止的6个月和12个月的列,没有子查询,第二个声明:

SELECT distinct dbo.tCustomerCard.Acct, dbo.tCustomer.FirstName, dbo.tCustomer.LastName, dbo.tCustomerAddress.Address1, dbo.tCustomerAddress.Address2, dbo.tCustomerAddress.City, dbo.tCustomerAddress.StateName, dbo.tCustomerAddress.PostalCode, dbo.tCustomerPhone.PhoneNumber, tCustomerEmail.EmailAddress, tCustomerAttributeDtl.BirthDt, tCustomerBalanceDtl.Ptsbal, dbo.tCustomer.ClubState, 
Sum(dbo.1Day.BasePts) as "6mnth_Tier_Points_Earned", Sum(dbo.1Day.BasePts) as "12mnth_Tier_Points_Earned"


FROM dbo.tCustomer (NOLOCK)

join dbo.tCustomerCard 
on dbo.tCustomer.CustomerId = tCustomerCard.CustomerId 

full outer join dbo.tCustomerAddress 
on dbo.tCustomer.CustomerId = tCustomerAddress.CustomerId 

full outer join dbo.tCustomerPhone 
on dbo.tCustomer.CustomerId = dbo.tCustomerPhone.CustomerId 

full outer join dbo.tCustomerEmail 
on dbo.tCustomer.CustomerId = dbo.tCustomerEmail.CustomerId 

full outer join dbo.tCustomerAttributeDtl 
on dbo.tCustomer.CustomerId = dbo.tCustomerAttributeDtl.CustomerId 

full outer join dbo.tCustomerBalanceDtl 
on dbo.tCustomer.CustomerId = dbo.tCustomerBalanceDtl.CustomerId 

full outer join dbo.1Day 
on dbo.tCustomerCard.Customerid = dbo.1Day.CustomerId 


WHERE dbo.tCustomerAddress.ContactTypeId = dbo.tCustomer.MailingContactTypeID 

---下面是注释掉的,因为我来回使用这个12个月的列。 - 和dbo.1Day.PeriodBeginDtm> =(选择convert(varchar(4),getdate(),120)) 和dbo.1Day.PeriodBeginDtm> = DATEADD(月,-6,GETDATE())

group by (dbo.tCustomerCard.Acct), (dbo.tCustomer.FirstName), (dbo.tCustomer.LastName), (dbo.tCustomerAddress.Address1), (dbo.tCustomerAddress.Address2), (dbo.tCustomerAddress.City), (dbo.tCustomerAddress.StateName), (dbo.tCustomerAddress.PostalCode), (dbo.tCustomerPhone.PhoneNumber), (tCustomerEmail.EmailAddress), (tCustomerAttributeDtl.BirthDt), (tCustomerBalanceDtl.Ptsbal), (dbo.tCustomer.ClubState)

1 个答案:

答案 0 :(得分:1)

您可以像这样使用case表达式:

SELECT distinct 
  cc.Acct, 
  c.FirstName, 
  c.LastName, 
  ca.Address1, ca.Address2, ca.City, ca.StateName, ca.PostalCode, 
  cp.PhoneNumber, ce.EmailAddress, 
  cad.BirthDt, 
  cbd.Ptsbal, c.ClubState, 
  SUM(CASE 
        WHEN d.PeriodBeginDtm > = (select convert(varchar(4), getdate(),120)) 
        THEN d.BasePts 
      ELSE 0 END ) AS "12mnth_Tier_Points_Earned",
  SUM(CASE 
        WHEN d.PeriodBeginDtm > = DATEADD(month, -6, GETDATE()) 
        THEN d.BasePts 
       ELSE 0 END) AS "6mnth_Tier_Points_Earned"
FROM dbo.tCustomer (NOLOCK) AS c
join dbo.tCustomerCard                    AS cc  on c.CustomerId  = cc.CustomerId 
full outer join dbo.tCustomerAddress      AS ca  on c.CustomerId  = ca.CustomerId 
full outer join dbo.tCustomerPhone        AS cp  on c.CustomerId  = cp.CustomerId 
full outer join dbo.tCustomerEmail        AS ce  on c.CustomerId  = ce.CustomerId 
full outer join dbo.tCustomerAttributeDtl AS cad on c.CustomerId  = cad.CustomerId 
full outer join dbo.tCustomerBalanceDtl   AS cbd on c.CustomerId  = cbd.CustomerId 
full outer join dbo.1Day                  AS d   on cc.Customerid = d.CustomerId 
WHERE ca.ContactTypeId = c.MailingContactTypeID 
group by ca.Acct, c.FirstName, c.LastName, 
         ca.Address1, ca.Address2, ca.City, 
         ca.StateName, ca.PostalCode, cp.PhoneNumber, 
         ce.EmailAddress, cad.BirthDt, cbd.Ptsbal, 
         c.ClubState