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 |
|
sqlnovice123
Constraint Violating Yak Guru
262 Posts |
Posted - 2010-03-06 : 19:52:20
|
| HiI am using the following final select in SQL 2005. I want to show the values for ExecteToCoverDiff_OLD and ResponseCount_OLD as NULL when the ExecteToCoverDiff_ALL and ResponseCount_ALL match ExecteToCoverDiff_NEW and ResponseCount_NEW. Same is the case when ExecteToCoverDiff_ALL and ResponseCount_ALL values match ExecteToCoverDiff_OLD and ResponseCount_OLD values, the ExecteToCoverDiff_NEW and ResponseCount_NEW values should be NULL which I believe is the additional purpose of using Outer joins.Currently for matched values of ExecteToCoverDiff_ALLand ResponseCount_ALL with ExecteToCoverDiff_OLDResponseCount_OLD , ExecteToCoverDiff_NEWResponseCount_NEW values show NULL which is great. But for matched values of ExecteToCoverDiff_ALL ,ResponseCount_ALLand ExecteToCoverDiff_NEWResponseCount_NEW values, I expect the ExecteToCoverDiff_OLD ,ResponseCount_OLD values to be NULL. Instead it shows values. Similarly, this pattern continues even for the other case where earlier I had NULL values. What is wrong? Should I include and If..statement to force NULL values when this is the case? Should I use temp tables instead? Note that the order in the #AllDealer is by TradeYear and then TradeMonth.Any help is appreciated. Thanks a bunchselect alld.TradeMonthName, alld.TradeYear, alld.ExecuteToCoverDiff , alld.ResponseCount , New.ExecuteToCoverDiff , New.ResponseCount , Old.ExecuteToCoverDiff , Old.ResponseCount , alld.TradeMonth , alld.QuarterByYear, alld.DealerTypeDatefrom #AllDealer alld LEFT OUTER JOIN #NewDealer NewON alld.TradeMonthName = New.TradeMonthNameAND alld.TradeYear = New.TradeYearLEFT OUTER JOIN #OldDealer OldON alld.TradeMonthName = Old.TradeMonthNameAND alld.TradeYear = Old.TradeYearThe data is as below: I cold not format to fit in the page but hopefully the column names and values are readable.The columns are as belowTradeMonthNameTradeYearExecteToCoverDiff_ALLResponseCount_ALLExecteToCoverDiff_OLDResponseCount_OLDExecteToCoverDiff_NEWResponseCount_NEWTradeMonthQuarterByYearDealerTypeDateJanuary 2009 24.4241149147541 3.28196721311475 NULL NULL 24.4241149147541 3.28196721311475 01 Q1 OLDFebruary 2009 24.6610613651376 3.04770642201835 NULL NULL 24.6610613651376 3.04770642201835 02 Q1 OLDMarch 2009 21.4489570791075 3.06896551724138 NULL NULL 21.4489570791075 3.06896551724138 03 Q1 OLDApril 2009 22.797960170068 3.2687074829932 NULL NULL 22.797960170068 3.2687074829932 04 Q2 OLDMay 2009 25.1883597044335 3.33497536945813 NULL NULL 25.1883597044335 3.33497536945813 05 Q2 OLDJune 2009 19.4323144223827 3.76173285198556 NULL NULL 19.4323144223827 3.76173285198556 06 Q2 OLDJuly 2009 71.918232464455 4.05687203791469 NULL NULL 71.918232464455 4.05687203791469 07 Q3 OLDAugust 2009 18.2744382670808 3.82298136645963 NULL NULL 18.2744382670808 3.82298136645963 08 Q3 OLDSeptember 2009 14.3352155945946 4.17027027027027 NULL NULL 14.3352155945946 4.17027027027027 09 Q3 OLDOctober 2009 0.200000000000003 9.5 0.200000000000003 9.5 23.3343639556962 4.51898734177215 10 Q4 NEWOctober 2009 23.3343639556962 4.51898734177215 0.200000000000003 9.5 23.3343639556962 4.51898734177215 10 Q4 OLDNovember 2009 6.06 3.6 6.06 3.6 17.0942585403424 4.18807339449541 11 Q4 NEWNovember 2009 17.0942585403424 4.18807339449541 6.06 3.6 17.0942585403424 4.18807339449541 11 Q4 OLDDecember 2009 13.3842857142857 4.90476190476191 13.3842857142857 4.90476190476191 12.4545201873831 4.20089285714286 12 Q4 NEWDecember 2009 12.4545201873831 4.20089285714286 13.3842857142857 4.90476190476191 12.4545201873831 4.20089285714286 12 Q4 OLDJanuary 2010 11.4792311538462 3.92307692307692 11.4792311538462 3.92307692307692 17.6750053985208 3.98975409836066 01 Q1 NEWJanuary 2010 17.6750053985208 3.98975409836066 11.4792311538462 3.92307692307692 17.6750053985208 3.98975409836066 01 Q1 OLD |
|
|
sqlnovice123
Constraint Violating Yak Guru
262 Posts |
Posted - 2010-03-06 : 19:59:54
|
| I tried using a filer but to no avail. I need some way to identify the first mismatch and replace those values with NULL.WHERE New.ExecuteToCoverDiff = CASE WHEN New.ExecuteToCoverDiff NOT IN (select alld.ExecuteToCoverDiff from #AllDealer) THEN 'NULL' |
 |
|
|
sqlnovice123
Constraint Violating Yak Guru
262 Posts |
Posted - 2010-03-06 : 23:12:52
|
| I think I fixed it. Whew! What a relief :-)I added the DealerTypeDate which holds values 'Old' and 'New' to teh #OldDealer and #Newdealer tables and grouped by the DealerTypeDate field as well. Then I added the condition where All.DealerTypeDate = New.DealerTypeDate and where All.DealerTypeDate = Old.DealerTypeDate to both LEFT OUTER JOINS in the final select. Now I see NULL for values that do not match.select alld.TradeMonthName, alld.TradeYear, alld.ExecuteToCoverDiff , alld.ResponseCount , New.ExecuteToCoverDiff , New.ResponseCount , Old.ExecuteToCoverDiff , Old.ResponseCount , alld.TradeMonth , alld.QuarterByYear, alld.DealerTypeDatefrom #AllDealer alld LEFT OUTER JOIN #NewDealer NewON alld.TradeMonthName = New.TradeMonthNameAND alld.TradeYear = New.TradeYearAND alld.DealerTypeDate = New.DealerTypeDateLEFT OUTER JOIN #OldDealer OldON alld.TradeMonthName = Old.TradeMonthNameAND alld.TradeYear = Old.TradeYearAND alld.DealerTypeDate = Old.DealerTypeDateTrade Trade Execute Response Execute Response Execute Response Trade Quarter DealerMonth Year To Count_ALL To Count_NEW To Count_OLD Month By Type Name CoverDiff_ALL CoverDiff_NEW CoverDiff_OLD YearDateJanuary 2009 24.4241149147541 3.28196721311475 NULL NULL 24.4241149147541 3.28196721311475 01 Q1 OLDFebruary 2009 24.6610613651376 3.04770642201835 NULL NULL 24.6610613651376 3.04770642201835 02 Q1 OLDMarch 2009 21.4489570791075 3.06896551724138 NULL NULL 21.4489570791075 3.06896551724138 03 Q1 OLDApril 2009 22.797960170068 3.2687074829932 NULL NULL 22.797960170068 3.2687074829932 04 Q2 OLDMay 2009 25.1883597044335 3.33497536945813 NULL NULL 25.1883597044335 3.33497536945813 05 Q2 OLDJune 2009 19.4323144223827 3.76173285198556 NULL NULL 19.4323144223827 3.76173285198556 06 Q2 OLDJuly 2009 71.918232464455 4.05687203791469 NULL NULL 71.918232464455 4.05687203791469 07 Q3 OLDAugust 2009 18.2744382670808 3.82298136645963 NULL NULL 18.2744382670808 3.82298136645963 08 Q3 OLDSeptember 2009 14.3352155945946 4.17027027027027 NULL NULL 14.3352155945946 4.17027027027027 09 Q3 OLDOctober 2009 0.200000000000003 9.5 0.200000000000003 9.5 NULL NULL 10 Q4 NEWOctober 2009 23.3343639556962 4.51898734177215 NULL NULL 23.3343639556962 4.51898734177215 10 Q4 OLDNovember 2009 6.06 3.6 6.06 3.6 NULL NULL 11 Q4 NEWNovember 2009 17.0942585403424 4.18807339449541 NULL NULL 17.0942585403424 4.18807339449541 11 Q4 OLDDecember 2009 13.3842857142857 4.90476190476191 13.3842857142857 4.90476190476191 NULL NULL 12 Q4 NEWDecember 2009 12.4545201873831 4.20089285714286 NULL NULL 12.4545201873831 4.20089285714286 12 Q4 OLDJanuary 2010 11.4792311538462 3.92307692307692 11.4792311538462 3.92307692307692 NULL NULL 01 Q1 NEWJanuary 2010 17.6750053985208 3.98975409836066 NULL NULL 17.6750053985208 3.98975409836066 01 Q1 OLD |
 |
|
|
|
|
|
|
|