Wrox Programmer Forums
Go Back   Wrox Programmer Forums > SQL Server > SQL Server 2005 > SQL Server 2005
|
SQL Server 2005 General 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 software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old May 29th, 2007, 06:20 PM
Registered User
 
Join Date: May 2007
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts
Default 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.

brux2dc:(
 
Old May 30th, 2007, 03:53 AM
Friend of Wrox
 
Join Date: Jun 2003
Posts: 2,480
Thanks: 0
Thanked 1 Time in 1 Post
Default

You can always delete your own post that you feel is mis-posted.

_________________________
- Vijay G
Strive for Perfection
 
Old June 1st, 2007, 11:35 PM
Friend of Wrox
 
Join Date: Oct 2006
Posts: 475
Thanks: 0
Thanked 9 Times in 9 Posts
Default

Quote:
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

Brux...

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...

Code:
 SELECT t.* 
   FROM yourtable t,
        (
         SELECT X,COUNT(*) AS FullCount
           FROM yourtable
          GROUP BY X
        ) fc
        ,
        (
         SELECT X,COUNT(*) AS TrueCount
           FROM yourtable
          WHERE Z='T'
          GROUP BY X
        ) tc
  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...


--Jeff Moden
 
Old June 4th, 2007, 04:23 AM
Friend of Wrox
 
Join Date: Jun 2003
Posts: 2,480
Thanks: 0
Thanked 1 Time in 1 Post
Default

Jeff,

I remember replying to his other post in SQL forum. Infact there he mentioned that he is not known how to move this post, thats why this reply. I think he will be really impressed as you said.

_________________________
- Vijay G
Strive for Perfection
 
Old June 4th, 2007, 07:08 AM
Friend of Wrox
 
Join Date: Oct 2006
Posts: 475
Thanks: 0
Thanked 9 Times in 9 Posts
Default

Ugh... I get it... thanks, Vijay...

--Jeff Moden
 
Old June 4th, 2007, 04:42 PM
Registered User
 
Join Date: May 2007
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Sorry I was out of town for the last few days. Thanks for the replies. Sorry about the cross-post. I'll give the syntax a try tonight when I settle in for the evening.

As far as being impressed... Anybody who can give advice, on things I don't know, impresses me. I came here to learn, and I did.

Thanks again,

Brux2dc






Similar Threads
Thread Thread Starter Forum Replies Last Post
Making sense of of PHP/SQL C/M/R lord_midnight Beginning PHP 0 November 23rd, 2008 05:50 PM
T-SQL Code to get last 6 months midway11 SQL Language 1 October 15th, 2006 08:59 AM
No joy making the downloaded sample code run RMagruder BOOK: ASP.NET Website Programming Problem-Design-Solution 3 February 5th, 2006 12:57 AM
AdsManager SQL Code Missing justin_dago BOOK: ASP.NET Website Programming Problem-Design-Solution 4 April 9th, 2004 03:50 PM
Beginning SQL, where is source code? docker All Other Wrox Books 1 January 7th, 2004 04:28 PM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.