Site Sponsored By: SQLDSC - SQL Server Desired State Configuration
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.
Basically, what I am trying to do is check a table. If a product exists in the table I want the column to say true, if it doesn't exist in the table on want the column to say false.I have 2 tables:Mobility_Solution_ApplicationsAppID intCategoryID intApplication nvarcharMobility_SolutionsSolutionID intProductID intAppID intHere is my current query:SELECT Distinct a.AppID, Application,(Case When ProductID IS Not NULL AND ProductID = 154 Then 'True' Else 'False' End) As [Checked]FROM Mobility_Solution_Applications aLeft Outer JOIN Mobility_Solutions bON a.AppID = b.AppIDWHERE a.CategoryID = 6 Just as an example, if I have a product 154 that is part of category 6, and the product does not exist in the table Mobility_Solutions then it should and does return the following:AppID Application Checked7 Turning Automotive Seating False8 Transfer Seating FalseThe problem happens when 154 DOES exist in the table and another product has the same AppID. It should return the same as above except one entry should say true. This is what it ends up returning:7 Turning Automotive Seating False7 Turning Automotive Seating True8 Transfer Seating FalseI know this is a logic issue, but I am having trouble figuring out the proper syntax. Help!!!
TG
Master Smack Fu Yak Hacker
6065 Posts
Posted - 2009-11-16 : 13:08:11
Try this:
SELECT a.AppID ,Application ,max(Case When ProductID is not null Then 'True' Else 'False' End) As [Checked]FROM Mobility_Solution_Applications aLeft Outer JOIN Mobility_Solutions b ON a.AppID = b.AppID and b.productid = 154WHERE a.CategoryID = 6 group by a.AppID ,Application
Be One with the OptimizerTG
abransom
Starting Member
2 Posts
Posted - 2009-11-16 : 15:57:40
Fantastic! Worked perfectly. Thanks. That one was making my brain bleed.