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 |
|
trombley
Starting Member
9 Posts |
Posted - 2006-04-18 : 14:33:39
|
| I read around the forum about case statements (and the web) - can you not put in multiple conditions in a statement before deciding the outcome? here is my statement:TargetPrice =CASE WHEN (rtrim(dbo.R_MAC_Dales_Sales_Report.PrimStat) = 'Active' AND rtrim(R_MAC_Dales_Sales_Report.Legal) = 'No Stat') THEN (((dbo.R_MAC_NoteSalesInfo.CurrentAppValue)* .5)-dbo.R_MAC_Dales_Sales_Report.TotalTax) ELSE -999ENDI receive the -999 flag everytime UNLESS I remove the legal='NoStat" statement. Then it calculates. I am using Microsoft SQL 2000 SP3. The fields PrimStat and Legal are both varchar. It has been several years since I have done SQL statements so my memory and knowledge is rusty. The books have one example of two fields, but I notice all of the other examples use just one field. So maybe I am writing something that won't work.Thanks!!! |
|
|
Srinika
Master Smack Fu Yak Hacker
1378 Posts |
Posted - 2006-04-18 : 14:37:42
|
| is it legal='NoStat' or legal='No Stat' ?Try putting a True condition as ==> 1=1 in After the ANDSrinika |
 |
|
|
trombley
Starting Member
9 Posts |
Posted - 2006-04-18 : 14:44:12
|
| it's "No Stat", with a space. I don't understand adding the true condition after the "and"? The Legla field may have many other values in it, I only want the calculation where "No Stat" is in the field. |
 |
|
|
tkizer
Almighty SQL Goddess
38200 Posts |
Posted - 2006-04-18 : 14:44:53
|
| Yes you can use multiple conditions in the CASE. Could you post your actual query?Tara Kizeraka tduggan |
 |
|
|
trombley
Starting Member
9 Posts |
Posted - 2006-04-18 : 15:08:36
|
| Here is the whole query .. but the only peice I am having issues with is what I posted up on top. Once that is working, I have about another half-dozen "when's" to issue.SELECT TOP 100 PERCENT dbo.R_MAC_NoteSalesInfo.SaleableGroup, dbo.Loan.LoanID, dbo.R_MAC_Dales_Sales_Report.PropAdd1, dbo.R_MAC_Dales_Sales_Report.PropCity, dbo.R_MAC_Dales_Sales_Report.PropState, dbo.R_MAC_Dales_Sales_Report.PropZip, dbo.R_MAC_Dales_Sales_Report.Collateral, dbo.R_MAC_NoteSalesInfo.BorrName, dbo.R_MAC_NoteSalesInfo.OriginalAmt, dbo.R_MAC_Dales_Sales_Report.BoardingUPB, dbo.R_MAC_Dales_Sales_Report.PrincipalBal, dbo.R_MAC_NoteSalesInfo.DueDate, dbo.R_MAC_NoteSalesInfo.LastPmtRecvdDate, dbo.R_MAC_NoteSalesInfo.[Lien Position], dbo.R_MAC_NoteSalesInfo.ServicerAdvances, dbo.R_MAC_NoteSalesInfo.CurrentAppValue, dbo.R_MAC_NoteSalesInfo.CurrentAppDate, dbo.R_MAC_Dales_Sales_Report.AssetCosts, dbo.R_MAC_Dales_Sales_Report.TotalTax, dbo.R_MAC_Dales_Sales_Report.TotalCostsTD, dbo.R_MAC_Dales_Sales_Report.TotalLienAmts, dbo.R_MAC_Dales_Sales_Report.NumberLiens, dbo.R_MAC_Dales_Sales_Report.Warning, isnull(dbo.R_MAC_Dales_Sales_Report.Legal,'No Stat') as Legal, dbo.R_MAC_Dales_Sales_Report.PoolGroupNumber, dbo.R_MAC_Dales_Sales_Report.InvestorName, dbo.R_MAC_Dales_Sales_Report.PrimStat, 'Assigned' AS Assigned, TargetPrice = CASE WHEN (dbo.R_MAC_Dales_Sales_Report.PrimStat = 'Active' AND R_MAC_Dales_Sales_Report.Legal = 'No Stat') THEN (((dbo.R_MAC_NoteSalesInfo.CurrentAppValue)* .5)-dbo.R_MAC_Dales_Sales_Report.TotalTax) ELSE -999 ENDFROM dbo.R_MAC_Dales_Sales_Report INNER JOIN dbo.Loan ON dbo.R_MAC_Dales_Sales_Report.LoanID = dbo.Loan.LoanID INNER JOIN dbo.R_MAC_NoteSalesInfo ON dbo.Loan.LoanID = dbo.R_MAC_NoteSalesInfo.LoanIDWHERE (NOT (dbo.R_MAC_NoteSalesInfo.SaleableGroup IN ('Deficiency', 'Note Returned -PC', 'Affiliate-Not Avail', 'Affiliate-Deficiency', 'Note Returned', 'AFS -Temp Off Market', 'PEND-TAXSURP'))) |
 |
|
|
tkizer
Almighty SQL Goddess
38200 Posts |
Posted - 2006-04-18 : 15:16:11
|
| I'm not sure, but perhaps you are having issues with the parenthesis. Try this:TargetPrice =CASE WHEN r.PrimStat = 'Active' AND r.Legal = 'No Stat' THEN (l.CurrentAppValue * .5) - r.TotalTaxELSE -999ENDFROM dbo.R_MAC_Dales_Sales_Report rINNER JOIN dbo.Loan l Use aliases to make your query more readable. r and l can now be used instead of OwnerName.ObjectName.You can even remove the other two parenthesis as order of operations says * will be before -, but including them probably makes it easier to read for those not mathematically inclined.Tara Kizeraka tduggan |
 |
|
|
Srinika
Master Smack Fu Yak Hacker
1378 Posts |
Posted - 2006-04-18 : 15:35:27
|
quote: Originally posted by trombley it's "No Stat", with a space. I don't understand adding the true condition after the "and"? The Legla field may have many other values in it, I only want the calculation where "No Stat" is in the field.
Just put it to see what makes that problem (ie, whether it is paranthesis, data, or any other)If you try with all, its hard to find where the problem is. First find what causes the problem.eg. SELECT TargetPrice =CASEWHEN (dbo.R_MAC_Dales_Sales_Report.PrimStat = 'Active' AND 1 = 1 ) THEN ((([Use Alias].CurrentAppValue)* .5)- [Use Alias].TotalTax)ELSE -999ENDFROM dbo.R_MAC_Dales_Sales_Report [Alias Name] INNER JOIN............. If u do get the correct results ==>Then query the DB for the 2 conditions in "When"if u do not get the correct results, ==>Check for other syntaxes like paranthesis etc.Srinika |
 |
|
|
trombley
Starting Member
9 Posts |
Posted - 2006-04-18 : 15:43:59
|
| what causes the problem (at the moment) is the addition of a second when, in this case "AND R_MAC_Dales_Sales_Report.Legal = 'No Stat'"If I remove that, the expression completes and calculates without using the else -999. Of course, the calculation is wrong because I removed the second part of the expression. I am going to mess with the () and see what happens.Mike |
 |
|
|
trombley
Starting Member
9 Posts |
Posted - 2006-04-18 : 15:54:44
|
| well, if works with 1=1 or any other field that is number based from my query. Will not work with a varchar field (although the first condition IS varchar). Does this jog anyone's memory? Changing the () around didn't help either. |
 |
|
|
tkizer
Almighty SQL Goddess
38200 Posts |
Posted - 2006-04-18 : 16:03:41
|
It works fine for me on my sample table and data. Check this out:CREATE TABLE Table1 (Column1 int, Column2 varchar(50), Column3 varchar(50), Column4 int, Column5 int)INSERT INTO Table1 VALUES(1, 'Active', 'No Stat', 1, 2)INSERT INTO Table1 VALUES(2, 'Active', 'No Stat', 2, 3)INSERT INTO Table1 VALUES(3, 'Active', 'Stat', 3, 4)INSERT INTO Table1 VALUES(4, 'Active', 'No Stat', 4, 5)INSERT INTO Table1 VALUES(5, 'Active', 'Stat', 5, 6)INSERT INTO Table1 VALUES(6, 'Not Active', 'Stat', 6, 7)SELECT Column3, TargetPrice = CASE WHEN (Column2 = 'Active' AND Column3 = 'No Stat') THEN (((Column4)* .5)-Column5) ELSE -999 ENDFROM Table1DROP TABLE Table1 Do both of these return data:SELECT * FROM dbo.R_MAC_Dales_Sales_Report WHERE Legal = 'No Stat'SELECT * FROM dbo.R_MAC_Dales_Sales_Report WHERE PrimStat = 'Active' Does this work fine:SELECT TargetPrice = CASE WHEN (r.PrimStat = 'Active' AND r.Legal = 'No Stat') THEN (((l.CurrentAppValue)* .5)-r.TotalTax) ELSE -999 ENDFROM dbo.R_MAC_Dales_Sales_Report rINNER JOIN dbo.Loan lON r.LoanID = l.LoanID Tara Kizeraka tduggan |
 |
|
|
trombley
Starting Member
9 Posts |
Posted - 2006-04-18 : 16:08:50
|
| solved!!!!!!!!!!!!!!!!!!!!!!!!!!!! I removed my ISNULL function from the top part of the query (changing NULLS into 'No Stats'). This left column as NULL. Then I changed the CASE statement as "Legal IS NULL". That made it work. Don't understand why though. I know NULLS are a pain anyway, which is why I removed them in the 1st place and replaced them with some english. Anyway, here is the corrected portion of the code:TargetPrice =CASEWHEN dbo.R_MAC_Dales_Sales_Report.PrimStat = 'Active' AND dbo.R_MAC_Dales_Sales_Report.Legal IS NULL THEN (dbo.R_MAC_NoteSalesInfo.CurrentAppValue *.5)-dbo.R_MAC_Dales_Sales_Report.TotalTax ELSE -999 END |
 |
|
|
trombley
Starting Member
9 Posts |
Posted - 2006-04-18 : 16:13:03
|
Thanks. yes, the queries return data. I hink the problem is I converted existing NULLS in that legal field into "No Stats". Somehow, that really hosed everything. I went back and left them as NULLS and everything works fine. Does SQL do something funky when you use the ISNULL() function?quote: Originally posted by tkizer It works fine for me on my sample table and data. Check this out:CREATE TABLE Table1 (Column1 int, Column2 varchar(50), Column3 varchar(50), Column4 int, Column5 int)INSERT INTO Table1 VALUES(1, 'Active', 'No Stat', 1, 2)INSERT INTO Table1 VALUES(2, 'Active', 'No Stat', 2, 3)INSERT INTO Table1 VALUES(3, 'Active', 'Stat', 3, 4)INSERT INTO Table1 VALUES(4, 'Active', 'No Stat', 4, 5)INSERT INTO Table1 VALUES(5, 'Active', 'Stat', 5, 6)INSERT INTO Table1 VALUES(6, 'Not Active', 'Stat', 6, 7)SELECT Column3, TargetPrice = CASE WHEN (Column2 = 'Active' AND Column3 = 'No Stat') THEN (((Column4)* .5)-Column5) ELSE -999 ENDFROM Table1DROP TABLE Table1 Do both of these return data:SELECT * FROM dbo.R_MAC_Dales_Sales_Report WHERE Legal = 'No Stat'SELECT * FROM dbo.R_MAC_Dales_Sales_Report WHERE PrimStat = 'Active' Does this work fine:SELECT TargetPrice = CASE WHEN (r.PrimStat = 'Active' AND r.Legal = 'No Stat') THEN (((l.CurrentAppValue)* .5)-r.TotalTax) ELSE -999 ENDFROM dbo.R_MAC_Dales_Sales_Report rINNER JOIN dbo.Loan lON r.LoanID = l.LoanID Tara Kizeraka tduggan
|
 |
|
|
tkizer
Almighty SQL Goddess
38200 Posts |
Posted - 2006-04-18 : 16:19:05
|
| Your data didn't have 'No Stat' in it! Your data still had the nulls. This doesn't update your data: isnull(dbo.R_MAC_Dales_Sales_Report.Legal,'No Stat') as Legal. It only changes the output for that column in your select statement. The Legal column was still Null and not No Stat. This query would not have returned rows: SELECT * FROM dbo.R_MAC_Dales_Sales_Report WHERE Legal = 'No Stat'Tara Kizeraka tduggan |
 |
|
|
trombley
Starting Member
9 Posts |
Posted - 2006-04-18 : 16:20:51
|
By the way, than thanks to evryone who replied and help me. I found this web site today and you have all been a stress reliever and life saver. I really apprecaite you all and hope to speend time on the forums talking to you all! |
 |
|
|
tkizer
Almighty SQL Goddess
38200 Posts |
Posted - 2006-04-18 : 16:29:29
|
| Do you understand why No Stat didn't work for you? I tried to explain it in my last post.Here's an example that you can run in Query Analyzer:CREATE TABLE Table1 (Column1 int, Column2 varchar(50), Column3 varchar(50), Column4 int, Column5 int)INSERT INTO Table1 VALUES(1, 'Active', NULL, 1, 2)INSERT INTO Table1 VALUES(2, 'Active', NULL, 2, 3)INSERT INTO Table1 VALUES(3, 'Active', 'Stat', 3, 4)INSERT INTO Table1 VALUES(4, 'Active', NULL, 4, 5)INSERT INTO Table1 VALUES(5, 'Active', 'Stat', 5, 6)INSERT INTO Table1 VALUES(6, 'Not Active', 'Stat', 6, 7)SELECT Column3 AS ActualData, ISNULL(Column3, 'No Stat') AS DisplayNoStatInsteadFROM Table1WHERE Column3 IS NULLSELECT Column3 AS ActualData, ISNULL(Column3, 'No Stat') AS DisplayNoStatInsteadFROM Table1WHERE Column3 = 'No Stat'DROP TABLE Table1The first column in the result set is the actual data in the table. The second column in the result set substitutes NULL data with No Stat. Notice in the first result set we get data back if we search for NULLs. In the second result set, we don't get any data back as Column3 does not have any rows equal to No Stat. When I asked if this query returned any rows SELECT * FROM dbo.R_MAC_Dales_Sales_Report WHERE Legal = 'No Stat', the answer would have been No as your data doesn't have this value. You answered Yes, which wasn't true.Let me know if it is still unclear.Tara Kizeraka tduggan |
 |
|
|
trombley
Starting Member
9 Posts |
Posted - 2006-04-18 : 17:00:01
|
No, I understand. The ISNULL didn't change the underlying data, just the visible data. My case statement saw the NULL, not the 'No Stat'. I really appreciate you clearing this up for me. Stupid mistake on my part. Mikequote: Originally posted by tkizer Do you understand why No Stat didn't work for you? I tried to explain it in my last post.Here's an example that you can run in Query Analyzer:CREATE TABLE Table1 (Column1 int, Column2 varchar(50), Column3 varchar(50), Column4 int, Column5 int)INSERT INTO Table1 VALUES(1, 'Active', NULL, 1, 2)INSERT INTO Table1 VALUES(2, 'Active', NULL, 2, 3)INSERT INTO Table1 VALUES(3, 'Active', 'Stat', 3, 4)INSERT INTO Table1 VALUES(4, 'Active', NULL, 4, 5)INSERT INTO Table1 VALUES(5, 'Active', 'Stat', 5, 6)INSERT INTO Table1 VALUES(6, 'Not Active', 'Stat', 6, 7)SELECT Column3 AS ActualData, ISNULL(Column3, 'No Stat') AS DisplayNoStatInsteadFROM Table1WHERE Column3 IS NULLSELECT Column3 AS ActualData, ISNULL(Column3, 'No Stat') AS DisplayNoStatInsteadFROM Table1WHERE Column3 = 'No Stat'DROP TABLE Table1The first column in the result set is the actual data in the table. The second column in the result set substitutes NULL data with No Stat. Notice in the first result set we get data back if we search for NULLs. In the second result set, we don't get any data back as Column3 does not have any rows equal to No Stat. When I asked if this query returned any rows SELECT * FROM dbo.R_MAC_Dales_Sales_Report WHERE Legal = 'No Stat', the answer would have been No as your data doesn't have this value. You answered Yes, which wasn't true.Let me know if it is still unclear.Tara Kizeraka tduggan
|
 |
|
|
|
|
|
|
|