Wrox Programmer Forums
Go Back   Wrox Programmer Forums > Database > SQL Language
|
SQL Language SQL Language discussions not specific to a particular RDBMS program or vendor.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the SQL Language 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 November 11th, 2003, 06:50 PM
Authorized User
 
Join Date: Sep 2003
Posts: 83
Thanks: 0
Thanked 0 Times in 0 Posts
Default Beginner's SQL question

Hi everyone,

I have not been in touch with SQL for a long time. I had to start using it again since a few weeks and it has been quite a struggle.

I have a question and I would be really grateful if someone can help me with it. Consider the following table structure:

ID TESTNUMBER STATE VALUE
1 100 1 0
2 100 1 1
3 100 2 1
4 100 2 0
5 101 1 1
6 101 1 0
7 101 2 1

Now, what I want to do is get the DISTINCZ count of TESTNUMBER, say where
(State = 1, Value = 0) and (State = 2, Value = 1).
So basically this should give me a count of 2 because TestNumber 100 has an entry for (State = 1, Value = 0) and (State = 2, Value = 1).
Also, entry 101 has (State = 1, Value = 0) and (State = 2, Value = 1)
entries. However, I am unable to write this using a query. Please help!

Thanks,
Sincerely,
Pankaj
 
Old November 12th, 2003, 03:39 AM
Authorized User
 
Join Date: Sep 2003
Posts: 83
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Hi,

I think I did not explain my question well. Let me explain the scenario. I have a code that has to generate all possible combinations of chosen States and Values.. Say for our example the combinations are (State = 1, 0) (State = 2, 1).

Now, I have to go in the database and find the TestNumber which has an entry for (State = 1, 0) AND (State = 2, 0) (which would only be TestNumber 100. If I had used an OR in my Select query it would return both 100 and 101. Also, if I use AND for the same field it would return me nothing)... So all combinations exist for this state. What SQL construct exists for performing such queries?

I hope this explains my problem a bit more clearly.

Thanks,
Pankaj
 
Old November 12th, 2003, 05:02 AM
Friend of Wrox
 
Join Date: Jun 2003
Posts: 1,212
Thanks: 0
Thanked 1 Time in 1 Post
Default

You could put all possible combinations of State and Value into a temporary table (or a permanent table if they are always the same) and join that into the main table, then pull out only those TESTNUMBERs that have all states and values. Something like this:
Code:
-- code here to create temp table and fill it with all values
-- its not clear to me from your post how these values are generated
SELECT TESTNUMBER
FROM yourTable AS T1
INNER JOIN #tempTable AS T2
ON T1.STATE = T2.STATE AND T1.VALUE = T2.VALUE
GROUP BY T1.TESTNUMBER
HAVING COUNT(*) = (SELECT COUNT(*) FROM #tempTable)
 
Old November 12th, 2003, 07:57 AM
Authorized User
 
Join Date: Sep 2003
Posts: 83
Thanks: 0
Thanked 0 Times in 0 Posts
Default

That did the trick! Thank you so much!

Pankaj






Similar Threads
Thread Thread Starter Forum Replies Last Post
A beginner's question Gene Reginato XSLT 5 May 17th, 2007 10:32 AM
Beginner's Question peytontodd Beginning VB 6 3 March 7th, 2007 11:58 AM
Beginner's question re: WindowsForm DataGrid savoym C# 2 January 19th, 2004 02:51 PM
Help please: another beginner's question savoym C# 1 January 9th, 2004 11:45 AM
Beginner's question savoym C# 2 January 8th, 2004 03:58 PM





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