简体   繁体   English

在 Case 表达式中使用子查询

[英]Using subquery in Case expression

I have a table called Property that has parcels in it.我有一个名为 Property 的表,其中包含包裹。 The relevant fields in my property table for this query are Taxyear, Parcel, Roll and Exemption amounts.此查询的属性表中的相关字段是 Taxyear、Parcel、Roll 和 Exemption 金额。 The Exemption field has an exemption amount for that parcel.免税字段包含该地块的免税金额。 In another table called PropertyFlags I have the taxyear, parcel, roll and propertyflag.在另一个名为 PropertyFlags 的表中,我有纳税年度、包裹、卷和财产标志。

What I'm trying to accomplish is if that parcel in propertyflags has a flag type of 20 and also exists in the property table I want it show the exemption amount.我想要完成的是,如果 propertyflags 中的那个包裹的标志类型为 20 并且也存在于属性表中,我希望它显示豁免金额。 If it doesn't exist in the property flags table or have a flag that = 20 then just show 0.如果它不存在于属性标志表中或具有 = 20 的标志,则只显示 0。

I've tried to do this in a inner join, however if I join the tables I only get the parcels that exists in both tables.我试图在内部连接中执行此操作,但是如果我连接表,我只会得到两个表中都存在的包裹。 I think what I'm wanting is to take the results from a query on the propertyflags table like this我想我想要的是像这样从对 propertyflags 表的查询中获取结果

SELECT PF.Parcel 
FROM PropertyFlags AS PF 
WHERE PF.TaxYear = 2019 AND PF.Roll IN ('RE', 'CA') AND PF.FlagType = 20

and then compare those parcels against whats in the property table.然后将这些地块与属性表中的内容进行比较。 This is what I have and I may be way off base.这就是我所拥有的,我可能离题太远了。 Perhaps a subquery is not even what I need here.也许子查询甚至不是我在这里所需要的。

DECLARE @PadSpaces varchar(4000) = SPACE(4000),
            @PadZeroes varchar(20) = REPLACE(SPACE(20), ' ', '0');
    
    SELECT 
    P.TaxYear AS TAXYEAR,
    P.Juri AS CNTY,
    LEFT(dbo.fnformatparcel(P.Parcel, P.Roll) + @PadSpaces, 18) AS PACEL_ID,
    LEFT(dbo.fnFormatAddress(P.SsNumber, P.SsSubNumber, P.SsDirection, P.SsStreet, P.SsType, 
    P.SsDirection2, P.SsApartment, '') + @PadSpaces, 100) AS PROP_ADDR,
    P.SsCity AS PROP_CITY,
    LEFT(dbo.fnFormatAddress(P.OsNumber, P.OsSubNumber, P.OsDirection, P.OsStreet, P.OsType, 
    P.OsDirection2, P.OsApartment, '') + @PadSpaces, 100) AS OWNER_ADDR,
    P.OsCity AS OWNER_CITY,
    P.OsState AS OWNER_STATE,
    P.OsZipcode AS OWNER_ZIP,
    P.LegalDescription AS LEGAL_DESC,
    P.FinalAssessed AS ASSESED_VAL,
    P.Taxable AS TAXABLE_VAL,
    SUM(L.Millage) AS TAX_RATE,
    P.Base AS TIF_BASEVAL,
    P.Cif AS TIF_EXCESSVAL,
    *CASE Flag.Parcel WHEN Flag.Parcel = P.Parcel 
    THEN P. Exemption ELSE 0 END FROM 
     (SELECT PF.Parcel AS Parcel FROM PropertyFlags AS PF  WHERE PF.TaxYear = 2019 AND PF.Roll IN 
     ('RE', 'CA') AND PF.FlagType = 20) 
    AS HE_CREDIT,*
    P.TaxCredit AS NONAG_TAXCREDIT,
    P.AgLandCredit AS AG_TAXCREDIT,
    SUM(PT.PaidAmount) AS TOTAL_TAX_PAID
    FROM Property AS P
        INNER JOIN PropertyTax AS PT ON (P.Juri = PT.Juri) AND (P.Roll = PT.Roll) AND (P.TaxYear = 
        PT.TaxYear) AND (P.Parcel = PT.Parcel) 
        INNER JOIN Levy AS L ON (PT.TaxYear = L.TaxYear) AND (PT.Juri = L.Juri) AND (P.Roll = L.Roll) 
        AND (PT.Fund = L.Fund) AND (PT.Authority = L.Authority)
        WHERE P.Roll IN ('RE','CA') AND P.TaxYear = 2019
    GROUP BY P.taxyear, P.Juri, P.Parcel, P.Roll, P.SsNumber, P.SsSubNumber, P.SsDirection, 
             P.SsStreet, P.SsType, P.SsDirection2, P.SsApartment, P.SsCity, P.OSNumber, 
             P.OsSubNumber, P.OsDirection, P.OsStreet, P.OsType, P.OsDirection2, P.OsApartment, 
             P.OsCity, P.OsState, P.OsZipcode, P.LegalDescription, P.FinalAssessed,P.Base, P.Taxable, 
             P.Cif, P.Exemption, P.TaxCredit, P.AgLandCredit

I've tried to do this in a inner join, however if I join the tables I only get the parcels that exists in both tables.我试图在内部连接中执行此操作,但是如果我连接表,我只会得到两个表中都存在的包裹。

You can switch to a LEFT join and add AND PF.FlagType = 20 to the join-condition in ON.您可以切换到 LEFT 联接并将AND PF.FlagType = 20添加到 ON 中的联接条件。 This only joins to matching rows with Flag 20, otherwise you get NULL. In the Select-list you need CASE WHEN PF.Parcel IS NOT NULL THEN P.Exemption ELSE 0 END .这只会连接到带有标志 20 的匹配行,否则你会得到 NULL。在选择列表中你需要CASE WHEN PF.Parcel IS NOT NULL THEN P.Exemption ELSE 0 END

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM