I am using SQL Server 2012. I have taken this query about as far as I am able. As you can see I am deriving values with CTEs then using the derived values in a select statement. In this example this happens just once when I concatenate Quarter +Year = Period
I could use this query in a dataset for an SSRS report to achieve the groupings I want, but I'd like to know how to do it in SQL. I have struggled when I need to derive data then perform other functions on those derivations. CTEs are one way. I have used temp tables to a limited degree, but am not well versed in their usage. Here is the query.
USE SampleDW
WITH gifts AS
(
SELECT ClientBriefName,AccountIDKey,AdventID,AccountName,AccountType,FlipProvision,FlipDate,ExpectedFlipDate,GiftCategory,GiftType,
GiftDate,GiftAmount,AssetClass,SecurityType,UserDefinedCode,
COALESCE(
CASE WHEN MONTH(giftdate)IN(1,2,3) THEN 'Q1' ELSE NULL END,
CASE WHEN MONTH(giftdate)IN(4,5,6) THEN 'Q2' ELSE NULL END,
CASE WHEN MONTH(giftdate)IN(7,8,9) THEN 'Q3' ELSE NULL END,
CASE WHEN MONTH(giftdate)IN(10,11,12) THEN 'Q4' ELSE NULL END
) AS Quarter,
CASE WHEN AccountType IN('CORP','ENDOW','OTHER','990','DCA','PRIV-TAX') THEN 'NO' ELSE 'YES' END AS ISPG,
YEAR(Giftdate) YEAR,
CASE WHEN AccountType IN('CRUT','CGA','CRUTNI','CRAT','PIF','NQI','CRUTMU','LEAD-PCT','LEAD-FIXED','GRANTOR-OWNER','PRE-69')
THEN 'YES' ELSE 'NO' END AS LIFEINCOME
FROM Generic.dbo.PO_GiftDetails
)
SELECT ClientBriefName,AccountIDKey,AdventID,AccountName,AccountType,FlipProvision,FlipDate,ExpectedFlipDate,GiftCategory,GiftType,
GiftDate,GiftAmount,AssetClass,SecurityType,UserDefinedCode, ISPG, QUARTER,YEAR,gifts.LIFEINCOME, CAST(Quarter AS VARCHAR)+CAST(YEAR AS varchar) AS Period
FROM gifts
Result Set
ClientBriefName AccountIDKey AdventID AccountName AccountType FlipProvision FlipDate ExpectedFlipDate GiftCategory GiftType GiftDate GiftAmount AssetClass SecurityType UserDefinedCode ISPG QUARTER YEAR LIFEINCOME Period
Client1 103859 Jsmith John Smith CRUT 0 NULL NULL Standard Unitrusts New Trusts 00:00.0 82300 e lcus 1 YES Q4 2012 YES Q42012
Client1 100281 Jsmith John Smith CRAT 0 NULL NULL Additions to Trusts Additions to Trusts 00:00.0 5000 c caus 2 YES Q4 2012 YES Q42012
Client2 100281 JaSmith Jane Smith CRUT 0 NULL NULL Additions to Trusts Additions to Trusts 00:00.0 5000 c caus 2 YES Q4 2012 YES Q42012
Client2 100281 JaSmith Jane Smith CRAT 0 NULL NULL Additions to Trusts Additions to Trusts 00:00.0 2000 c caus 2 YES Q4 2012 YES Q42012
Client1 103859 Jsmith John Smith CRUT 0 NULL NULL Standard Unitrusts New Trusts 00:00.0 82300 e lcus 1 YES Q1 2012 YES Q12012
Client1 100281 Jsmith John Smith CRAT 0 NULL NULL Additions to Trusts Additions to Trusts 00:00.0 5000 c caus 2 YES Q1 2012 YES Q12012
Client2 100281 JaSmith Jane Smith CRUT 0 NULL NULL Additions to Trusts Additions to Trusts 00:00.0 5000 c caus 2 YES Q1 2012 YES Q12012
Client2 100281 JaSmith Jane Smith CRAT 0 NULL NULL Additions to Trusts Additions to Trusts 00:00.0 2000 c caus 2 YES Q1 2012 YES Q12012
What if I then want to achieve a pivot table-like output. I have used pivot in sQL before but I am not sure how I would in this query in the simplest form possible. The desired output would group by client summing the various gift types, with a sum of all sums lastly grouped by period
AdventID Annuity Trusts Flip Trust Gift Annuity Pools Net Income Unitrusts Grand Total Period
CLIENT1 0 0 1187880.16 800 1188680.16 Q12012
CLIENT2 0 0 25014.6 500 25514.6 Q12012
CLIENT1 0 0 550123 800 550923 Q42012
CLIENT2 0 0 4851061 600 4851661 Q42012