2

我正在使用 Server 2012 中的以下架构和查询创建年度捐赠报告:

CREATE TABLE Donation
(
   HouseholdID INT, 
   HouseholdName VARCHAR(150), 
   [Donation] REAL, 
   DateOrdered DATE
);

INSERT Donation VALUES
(1,'The Smiths',50,'2011-01-01'),
(1,'The Smiths',75,'2012-02-02'),
(2,'The Wilsons',10,'2011-03-01'),
(2,'The Wilsons',60,'2011-07-01'),
(3,'The Kellys',500,'2012-06-01');

--Params
DECLARE @RunYear   INT=2012
DECLARE @PriorYear INT=@RunYear-1

SELECT 
    [HouseholdID],[HouseholdName],YEAR(DateOrdered) [Year], MAX(Donation) [MaxGift], SUM(Donation) [TotalDonated]
FROM 
    Donation 
WHERE 
    YEAR([DateOrdered]) IN (@RunYear,@PriorYear)
    GROUP BY HouseholdID, [HouseholdName], YEAR(DateOrdered)
    ORDER BY HouseholdID, YEAR(DateOrdered)

我想添加一个BIT类似于 [DonatedInPriorYearOnly] 的列,仅当家庭在前一年(2011 年)有捐款但在运行年(2012 年)没有捐款时才为 1。

如何实现?

4

4 回答 4

3

根据您的 SQLServer 版本,您可以包装摘要查询(在子查询或 CTE 中)并使用LAG。如果 HouseholdID 和 Year 与 LAG HouseholdID 和 (Year - 1) 不匹配,则上一年没有捐款。

大致是这样的(未经测试):

SELECT [HouseholdID], [HouseholdName], [Year], [MaxGift], [TotalDonated]
      ,CASE WHEN YEAR = previous_year +1 THEN 1 ELSE 0 END AS DonatedPreviousYear

   FROM ( SELECT [HouseholdID], [HouseholdName], [Year], [MaxGift], [TotalDonated]
                ,LAG(Year, 1, 0) OVER (PARTITION BY HouseholdID ORDER BY Year) AS previous_year

            FROM ( SELECT [HouseholdID],[HouseholdName],YEAR(DateOrdered) [Year], MAX(Donation) [MaxGift], SUM(Donation) [TotalDonated]
                     FROM  Donation 
                     WHERE [DateOrdered] BETWEEN @StartDt AND @EndDt
                     GROUP BY HouseholdID, [HouseholdName], YEAR(DateOrdered)
                     ORDER BY HouseholdID, YEAR(DateOrdered)
                  )
        )
于 2013-02-14T02:09:42.333 回答
1

您可以查询每个家庭在上一年所做的任何捐赠OUTER APPLYSELECT TOP

SELECT 
     <your select list>
    ,ISNULL(lastyear.donated_bit,0) AS donated_last_year
FROM Donation AS d
OUTER APPLY(
    SELECT TOP 1 1 AS donated_bit
    FROM Donation AS d_lastyear
    WHERE d_lastyear.HouseholdID = d.HouseholdID
        AND DATEPART(year, d_lastyear.DateOrdered) = @RunYear - 1
        AND d_lastyear.Donation IS NOT NULL
        AND d_lastyear.Donation > 0
) AS lastyear
于 2013-02-14T02:40:16.903 回答
1
select blah, blah, blah
from donation
where householdid in 
(select householdid
from donation
where year(dateordered) = @runyear -1
except
select householdid
from donation
where year(dateordered) = @runyear )

这就是逻辑。如果您希望它运行得更快,请创建一组日期以与每年的第一天和年底同时使用,并使用这些日期而不是年份(dateordered)

在下面编辑

要拥有基于此逻辑的位列,请执行此操作。

select blah, blah, blah
, case 
when householdid in 
(select householdid
from donation
where year(dateordered) = @runyear -1
except
select householdid
from donation
where year(dateordered) = @runyear )
then 1 else 0 end nameofcolumn
from donation
where whatever
于 2013-02-14T03:30:57.717 回答
1

在查看了各种建议后,我能够解决它。

[DonatedPriorYearOnly] = 
(
   CASE
   WHEN EXISTS 
   (
      SELECT HouseholdID FROM Donation d2
      WHERE YEAR(d2.DateOrdered)=@PriorYear AND d2.HouseholdID=d.HouseholdID
      EXCEPT
      SELECT HouseholdID FROM Donation d3 
      WHERE YEAR(d3.DateOrdered)=@RunYear AND d3.HouseholdID=d.HouseholdID

   )
   THEN 1 ELSE 0 END
)

谢谢大家的回复。他们都很有帮助。

于 2013-02-14T04:01:51.807 回答