Wrox Programmer Forums

Need to download code?

View our list of code downloads.

Go Back   Wrox Programmer Forums > SQL Server > SQL Server 2005 > SQL Server 2005
Password Reminder
Register
Register | FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read
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 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 .
DRM-free e-books 300x50
Reply
 
Thread Tools Display Modes
  #1 (permalink)  
Old May 29th, 2007, 07:20 PM
Registered User
 
Join Date: May 2007
Location: , , .
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:(
Reply With Quote
  #2 (permalink)  
Old May 30th, 2007, 04:53 AM
Friend of Wrox
 
Join Date: Jun 2003
Location: Bangalore, KA, India.
Posts: 2,480
Thanks: 0
Thanked 0 Times in 0 Posts
Default

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

_________________________
- Vijay G
Strive for Perfection
Reply With Quote
  #3 (permalink)  
Old June 2nd, 2007, 12:35 AM
Friend of Wrox
 
Join Date: Oct 2006
Location: , MI, USA.
Posts: 473
Thanks: 0
Thanked 7 Times in 7 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
Reply With Quote
  #4 (permalink)  
Old June 4th, 2007, 05:23 AM
Friend of Wrox
 
Join Date: Jun 2003
Location: Bangalore, KA, India.
Posts: 2,480
Thanks: 0
Thanked 0 Times in 0 Posts
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
Reply With Quote
  #5 (permalink)  
Old June 4th, 2007, 08:08 AM
Friend of Wrox
 
Join Date: Oct 2006
Location: , MI, USA.
Posts: 473
Thanks: 0
Thanked 7 Times in 7 Posts
Default

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

--Jeff Moden
Reply With Quote
  #6 (permalink)  
Old June 4th, 2007, 05:42 PM
Registered User
 
Join Date: May 2007
Location: , , .
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

Reply With Quote
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off

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 09: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 04:50 PM
Beginning SQL, where is source code? docker All Other Wrox Books 1 January 7th, 2004 04:28 PM



All times are GMT -4. The time now is 01:58 PM.


Powered by vBulletin® Version 3.7.0
Copyright ©2000 - 2014, Jelsoft Enterprises Ltd.
© 2013 John Wiley & Sons, Inc.