SQL Server 2005General discussion of SQL Server *2005* version only.
Welcome to the p2p.wrox.com Forums.
You are currently viewing the SQL Server 2005 section of the Wrox Programmer to Programmer discussions. This is a community of tens of thousands of software programmers and website developers including Wrox book authors and readers. As a guest, you can read any forum posting. By joining today you can post your own programming questions, respond to other developers’ questions, and eliminate the ads that are displayed to guests. Registration is fast, simple and absolutely free .
SQL Code Help... Making it harder than needs to be
I have a table that has multiple rows with the same value in column 'x' with 'x' being the name of an item in this case. So 'x' might have 4 or 5 rows with 'y' and 'z' containing different data. Column 'z' is a true/false column and 'y' has different traits about 'x'. I want to have it so that when I select 'x' it only appears in the output when every 'x' row has a 'z' value of 'True'.
I know that the above is verbose. I'll paste what I have below with comments...
SELECT dbo.Drink.Title, dbo.Ingredient.Title AS Ingredient, dbo.Component.NumUnitsMin, dbo.Unit.UnitName, dbo.Ingredient.IsInMyBar
FROM dbo.Drink INNER JOIN
dbo.Component ON dbo.Drink.DrinkID = dbo.Component.DrinkID INNER JOIN
dbo.Ingredient ON dbo.Component.IngredID = dbo.Ingredient.IngredID INNER JOIN
dbo.Unit ON dbo.Component.UnitID = dbo.Unit.UnitID
WHERE IsInMyBar = 'True' //Trying to limit but oviously this is wrong. For every 'x' there should be no 'x' returned while 'z' = 'true'
Thanks, I've played for hours trying to write a proper where clause.
quote:Originally posted by happygv
You can always delete your own post that you feel is mis-posted.
- Vijay G Strive for Perfection
Heh... great post... I'll bet the new guy is really impressed... much better than posting a solution
Here's one way... you'll need to ****************-horn the "test" table into a CTE... I can't do that because I don't have 2005 to test with...
FROM yourtable t,
SELECT X,COUNT(*) AS FullCount
GROUP BY X
SELECT X,COUNT(*) AS TrueCount
GROUP BY X
WHERE t.x = fc.x
AND t.x = tc.x
AND fc.FullCount = tc.TrueCount
Of course, you'll need to modify the code with the correct column names and table names...