我有以下SQL查询;
SELECT DISTINCT
Contacts.ContactId,
ROUND(SUM(LandingDetails.Quantity * LandingDetails.UnitPrice), 2) AS Owed,
SocietyMemberships.WeeklyDeductionRate,
SocietyMemberships.FromMinimumReturn,
Deductions.DeductionRate
FROM
dbo.LandingDetails
INNER JOIN
dbo.LandingHeaders ON LandingDetails.LandingId = LandingHeaders.LandingId
INNER JOIN
dbo.Vessels ON LandingHeaders.VesselId = Vessels.VesselId
INNER JOIN
dbo.Contacts ON Vessels.OwnerId = Contacts.ContactId
INNER JOIN
dbo.SocietyMemberships ON Contacts.SocietyId = SocietyMemberships.SocietyId
INNER JOIN
dbo.Deductions ON Vessels.DeductionId = Deductions.DeductionId
WHERE
LandingHeaders.Posted = 0
GROUP BY
Contacts.ContactId,
SocietyMemberships.WeeklyDeductionRate,
SocietyMemberships.FromMinimumReturn,
Deductions.DeductionRate
其中产生下表作为其输出;
我需要一些建议,以便我如何在输出表中添加另一列,为了参数,我们将调用TotalDeductions
。
如果我们以表格的第一行为例,总计扣除额将通过获取所欠列的百分比(来自DeductionRate列)(在本例中为1.5%)来计算。如果从欠去的列总数中删除1.5%并删除WeeklyDeductionRate中的总和,在这种情况下为10.0,则剩下一个至少为110的总和,然后可以采用WeeklyDeductionRate。然而,如果在删除WeeklyDeductionRate之后总和低于110,则不应将其删除。
我确信这可以在SQL中完成,只是不确定从哪里开始。
注意虽然在返回数据表中显示所有列WeeklyDeductionRate
,FromMinimumReturn
和DeductionRate
显示相同的数字,但它们可能会有所不同,因此“硬编码”数字不是一个选项。
由于
修改
数据库平台是SQL Server(2012或2014)。作为我最想要的结果的一个例子,再以第一行为例,我想要一个结果集如下:
ContactId Owed TotalDeductions
39 1609.390000 34.14
其中总扣除额为1609.39 * 1.5%,其中= 24.14加10,因为从1609.39扣除的总计34.14将至少留下110的余额。
答案 0 :(得分:2)
我认为将以下case表达式添加到select语句中应该这样做:
CASE WHEN
SUM(LandingDetails.Quantity * LandingDetails.UnitPrice)
-
SUM(LandingDetails.Quantity * LandingDetails.UnitPrice) * DeductionRate + WeeklyDeductionRate
> FromMinimumReturn
THEN SUM(LandingDetails.Quantity * LandingDetails.UnitPrice) * DeductionRate + WeeklyDeductionRate
ELSE 0 END
AS TotalDeductions
但是,这有很多重复代码(Owed计算),所以我将原始查询包装在一个公用表表达式中,并按照以下方式执行:
WITH cte AS (
<<<your original query here>>> -- I left it out to save space...
)
SELECT
ContactId,
Owed,
WeeklyDeductionRate,
FromMinimumReturn,
DeductionRate,
CASE
WHEN Owed - (Owed * DeductionRate + WeeklyDeductionRate) > FromMinimumReturn
THEN Owed * DeductionRate + WeeklyDeductionRate
ELSE 0 END
AS TotalDeductions
FROM cte
这将返回计算的TotalDeductions,如果从Owed中减去它,则结果超过FromMinimumReturn,否则它将为TotalDeductions返回0。