SQL Development
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
 
 
User Name:
Password:
Remember me
Go Back   ASP Free ForumsDatabaseSQL Development

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread ASP Free Forums Sponsor:
  #1  
Old August 26th, 2004, 11:47 AM
DBAC DBAC is offline
Registered User
ASP Free Newbie (0 - 499 posts)
 
Join Date: Aug 2004
Posts: 7 DBAC User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
Unhappy SQL Multiple Instance Problem

Ok, the problem I'm having is that I'm trying to pull out an entry that satisfies certain conditions, but they can only be satisfied across multiple entries... Does that make sense? I doubt it so here is what I'm working with...

Field1 | Field2
---------------
test1 | one
test1 | two
test1 | three
test2 | one
test2 | three

If( Field2 == one AND Field2 == two ) Then
return Field1

In this case I would hope to get test1.

Something like that. So I want my query to find the entry in field1 that satisfies both constraints found in field2. Sorry if this sounds confusing, I'm getting more confused as I write it!!

Reply With Quote
  #2  
Old August 26th, 2004, 11:54 AM
Memnoch's Avatar
Memnoch Memnoch is offline
Unholy Moderator
Click here for more information.
 
Join Date: Oct 2003
Location: In hell, where did you think?
Posts: 11,781 Memnoch User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)Memnoch User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)Memnoch User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)Memnoch User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)Memnoch User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)Memnoch User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)Memnoch User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)Memnoch User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)Memnoch User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)Memnoch User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)Memnoch User rank is Lieutenant Colonel (40000 - 50000 Reputation Level) 
Time spent in forums: 3 Weeks 5 Days 8 h 45 m 55 sec
Reputation Power: 470
You database isn't designed properly.

1) You're storing duplicate information, which you shouldn't do
2) You can't return your results in that manner because they aren't associated with the same record.

You should put some more thought into your database design first.

Reply With Quote
  #3  
Old August 26th, 2004, 12:07 PM
DBAC DBAC is offline
Registered User
ASP Free Newbie (0 - 499 posts)
 
Join Date: Aug 2004
Posts: 7 DBAC User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
The premise of this design is from a Document Management System. I use one table to hold my document entries and another to hold field values (for searching purposes). The example above was a joined table, here is the tables beforehand:

Document Table

id | name
----------
1 | test1
2 | test2

Document Fields

id | document_id | value
----------
1 | 1 | one
2 | 1 | two
3 | 1 | three
4 | 2 | one
5 | 2 | three

Any suggestions on how I might be able to improve on this design?

Reply With Quote
  #4  
Old August 26th, 2004, 12:18 PM
Memnoch's Avatar
Memnoch Memnoch is offline
Unholy Moderator
Click here for more information.
 
Join Date: Oct 2003
Location: In hell, where did you think?
Posts: 11,781 Memnoch User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)Memnoch User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)Memnoch User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)Memnoch User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)Memnoch User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)Memnoch User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)Memnoch User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)Memnoch User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)Memnoch User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)Memnoch User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)Memnoch User rank is Lieutenant Colonel (40000 - 50000 Reputation Level) 
Time spent in forums: 3 Weeks 5 Days 8 h 45 m 55 sec
Reputation Power: 470
What's the purpose of the multiple values?
You can't return the records the way you want unless the values are associated with the same row.

Reply With Quote
  #5  
Old August 26th, 2004, 12:29 PM
DBAC DBAC is offline
Registered User
ASP Free Newbie (0 - 499 posts)
 
Join Date: Aug 2004
Posts: 7 DBAC User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
The multiple values are to identify documents. Such as Plant, Department, Process, Number, etc. These are attributes for the documents and was designed this way so that attributes can be added/deleted easier by adding/deleting rows instead of columns.

Reply With Quote
  #6  
Old August 26th, 2004, 12:37 PM
Memnoch's Avatar
Memnoch Memnoch is offline
Unholy Moderator
Click here for more information.
 
Join Date: Oct 2003
Location: In hell, where did you think?
Posts: 11,781 Memnoch User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)Memnoch User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)Memnoch User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)Memnoch User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)Memnoch User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)Memnoch User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)Memnoch User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)Memnoch User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)Memnoch User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)Memnoch User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)Memnoch User rank is Lieutenant Colonel (40000 - 50000 Reputation Level) 
Time spent in forums: 3 Weeks 5 Days 8 h 45 m 55 sec
Reputation Power: 470
The way your database is designed, you can't return the results your want.
You can't do
Code:
WHERE Field2 = 'one' AND Field2 = 'two'

because it is looking at the row, rather than the column and none of the rows have 'one' and 'two' in them

You can't do
Code:
WHERE Field2 = 'one' OR Field2 = 'two'

because then it will pull test2 also, because it has a 'one' in Field2.

You should consider redesigning your database structure.

Reply With Quote
  #7  
Old August 26th, 2004, 12:39 PM
Memnoch's Avatar
Memnoch Memnoch is offline
Unholy Moderator
Click here for more information.
 
Join Date: Oct 2003
Location: In hell, where did you think?
Posts: 11,781 Memnoch User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)Memnoch User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)Memnoch User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)Memnoch User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)Memnoch User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)Memnoch User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)Memnoch User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)Memnoch User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)Memnoch User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)Memnoch User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)Memnoch User rank is Lieutenant Colonel (40000 - 50000 Reputation Level) 
Time spent in forums: 3 Weeks 5 Days 8 h 45 m 55 sec
Reputation Power: 470
You could try creating a cursor to loop through the records and return only those records where the docID is the same and field2 contains 'one' and 'two'.

Reply With Quote
  #8  
Old September 1st, 2004, 11:34 AM
DBAC DBAC is offline
Registered User
ASP Free Newbie (0 - 499 posts)
 
Join Date: Aug 2004
Posts: 7 DBAC User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
Thumbs up

Just thought I'd follow up to give a resolution to this thread.

Since I am working with someone elses design and fitting it for my needs, restructuring the tables in an easier to use format appeared to be too time consuming as many things worked off of this structure. Since I could not fit a restructure in my time table, I created a table just to fullfill my current needs.

Using a series of joins, I joined the same table multiple times so I could populate a single row with the information I needed.

For example:

Document Table

id | name
----------
1 | test1
2 | test2

Document Fields

id | document_id | document_field_id | value
----------
1 | 1 | 1 | one
2 | 1 | 2 | two
3 | 1 | 3 | three
4 | 2 | 1 | one
5 | 2 | 3 | three

Code:
 SELECT * FROM document_table AS D 
  LEFT OUTER JOIN document_fields AS DF1
	ON DF1.document_id = D.id AND
	  DF1.document_field_id = 1
  LEFT OUTER JOIN document_fields AS DF2
	ON DF2.document_id = D.id AND
	  DF2.document_field_id = 2
  LEFT OU...


This way I can end up with a table where all fields in one category will be in one column matched with the appropriate document. With a table like this it is much simpler to search for things.

Thanks for the help,
DBAC



Quote:
Originally Posted by DBAC
The premise of this design is from a Document Management System. I use one table to hold my document entries and another to hold field values (for searching purposes). The example above was a joined table, here is the tables beforehand:

Document Table

id | name
----------
1 | test1
2 | test2

Document Fields

id | document_id | value
----------
1 | 1 | one
2 | 1 | two
3 | 1 | three
4 | 2 | one
5 | 2 | three

Any suggestions on how I might be able to improve on this design?

Reply With Quote
Reply

Viewing: ASP Free ForumsDatabaseSQL Development > SQL Multiple Instance Problem


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


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

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump


Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 





© 2003-2008 by Developer Shed. All rights reserved. DS Cluster 1 hosted by Hostway
Stay green...Green IT