Woe the Problem:
A table contains multiple entries for each student and has a row for each type of financial aid they have received.
The record item types fall into ranges which represent Grants, Loans, and Other (Scholarships, etc).
For each student and item type range, the NetAward amounts must be summed. The maximum amount of records returned for each student is three; one for Grants, one for Loans, and one for Other (if amounts exist in each type).
What is the best way to do this; a temp table, direct query, or something else?
Desired Output
StudentID FAType NetAward
10000001 1 150.00
10000001 3 50.00
10000002 1 1550.00
10000002 2 100.00
10000003 1 200.00
10000003 2 2000.00
10000003 3 50.00
10000004 3 90.00
This is what I have so far that's so not working:
declare @FAAwards table( StudentID nvarchar(8) not null, ItemType nvarchar(4), NetAward decimal(6,2) ) insert into @FAAwards (StudentID ,ItemType ,NetAward) select '10000001','1201',150.00 union all select '10000001','1455',50.00 union all select '10000002','1201',50.00 union all select '10000002','1205',1500.00 union all select '10000002','1350',100.00 union all select '10000003','1241',200.00 union all select '10000003','1385',2000.00 union all select '10000003','1460',50.00 union all select '10000004','1420',20.00 union all select '10000004','1425',30.00 union all select '10000004','1455',40.00 select StudentID ,FAType = case when ItemType between '1200' and '1300' then 1 -- Grant when ItemType between '1300' and '1400' then 2 -- Loan when ItemType between '1400' and '1500' then 3 -- Other end ,NetAward from @FAAwards where NetAward > 0 group by StudentID, FAType, NetAward --Invalid column name 'FAType'
I know the error is because the constructed column alias can’t be used in the “Group By” statement but I’m not sure how to restructure the statement to make it work.