简体   繁体   中英

Using subquery in Case expression

I have a table called Property that has parcels in it. The relevant fields in my property table for this query are Taxyear, Parcel, Roll and Exemption amounts. The Exemption field has an exemption amount for that parcel. In another table called PropertyFlags I have the taxyear, parcel, roll and propertyflag.

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. If it doesn't exist in the property flags table or have a flag that = 20 then just show 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

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. 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 .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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