Please start any new threads on our new
site at https://forums.sqlteam.com. We've got lots of great SQL Server
experts to answer whatever question you can come up with.
Author |
Topic |
Tonekene
Starting Member
6 Posts |
Posted - 2014-09-04 : 12:08:36
|
Here is my table Id Name Acct Amount1 mike sav $1002 mike check $2003 mike mm $3004 mike cd $4005 mike ira $5006 mike 401k $600 I need to produce a report for mike which will list his name and StatusFor example:Name StatusMike Great InvestorStatus is determined as follows (do not take numbers literally)Status:-if Mike has check acct and amount > 0 and he has 401k and amount is >$500 he is "Good Investor"-if Mike has check acct and amount > 100 and he has 401k and amount is >$500 and he has cd > $750 he is "Great Investor"-if Mike has sav acct and amount > 0 and he has 401k and amount is >$1000 and he has ira > 200 and cd > 0 he is "Excellent Investor"What would be the easiest, or neat way of doing it?Your advice is greatly appreciatedin the meanwhile i am bulding a grid, unless you or i come up with another idea Thanks |
|
MichaelJSQL
Constraint Violating Yak Guru
252 Posts |
Posted - 2014-09-04 : 12:25:46
|
I am pretty sure the precedence will workCREATE TABLE #I ( ID int, Name varchar(10), AccountType varchar(5), Amount money )INSERT INTO #IVALUES(1 , 'mike', 'sav' ,100),(2 , 'mike', 'check' ,200),(3 , 'mike', 'mm' ,300),(4 , 'mike', 'cd' ,400),(5 ,'mike', 'ira' ,500),(6 , 'mike', '401k' ,600 )SELECT *,CASE WHEN [check] > 0 and [401k] >1000 AND ira > 200 AND cd > 0 THEN 'Excellent Investor' WHEN [check] > 100 and [401k] >500 AND cd > 750 THEN 'Great Investor' WHEN [check] > 0 and [401k] >500 THEN 'Good Investor' ELSE 'Slacker' ENDFROM(SELECT Name,AccountType,Amount FROM #i) AS SourceTablePIVOT(Sum(Amount)FOR AccountType IN ([sav], [check], [mm], [cd], [ira],[401k])) AS InvestorAccounts; |
|
|
Tonekene
Starting Member
6 Posts |
Posted - 2014-09-04 : 12:35:14
|
I did not think of pivot, trying now. thank you very much!great success! |
|
|
|
|
|
|
|