If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

 
Go Back  dBforums > Data Access, Manipulation & Batch Languages > ANSI SQL > Can't quite figure this one out... any help?

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 11-30-04, 06:10
bdonegan bdonegan is offline
Registered User
 
Join Date: Nov 2004
Posts: 3
Can't quite figure this one out... any help?

I have 2 tables, one contains the details for all the documents stored on a server such as the documents name and which directory it is stored in, and the other contains the details for each document version (if there are any) these are paired up using the documents id, per example below

Documents
ID, Name, Dir
1, word.doc, /docs
2, excel.xls, /docs
3, webpage.html, /docs

Document Versions
ID
1
1
1
2
2
3

so, as you can see, in the versions table there are 3 versions of 'word.doc' 2 of 'excel.xls' and 1 of 'webpage.html'. i need to write a query that will return all the documents in the versions table who have a matching document in the documents table (that is not the major problem though) the main problem is that i only want to return it if the matching record in the documents table passes certain criteria, this being that it is from a certain site and has a particular type of file name. for example, i would like to return only the document versions whose parent records are '.doc' or '.xls' files, but not html. this would leave me with :

Versions
ID
1
1
1
2
2

leaving the last record out, as it's parent is a html file.

As a bonus, if you could tell me how to append the parents name to each record so i get a table like this :

Results
ID Name
1 'word.doc'
1 'word.doc'
1 'word.doc'
2 'excel.xls'
2 'excel.xls'

that would be great, thank you
Reply With Quote
  #2 (permalink)  
Old 11-30-04, 07:26
Littlefoot Littlefoot is offline
Lost Boy
 
Join Date: Jan 2004
Location: Croatia, Europe
Posts: 3,629
You didn't specify database engine you use; but, in Oracle it would be something like this:
Code:
SELECT d.id, d.name
FROM DOCUMENTS d, VERSIONS v
WHERE v.id = d.id
  AND SUBSTR(d.name, INSTR(d.name, '.', 1) + 1, LENGTH(d.name)) <> 'html';
WHERE clause will be different for another requirements, of course.
Reply With Quote
  #3 (permalink)  
Old 11-30-04, 08:10
bdonegan bdonegan is offline
Registered User
 
Join Date: Nov 2004
Posts: 3
hmmm, not sure if that's quite it. and the database engine is SQL server
Reply With Quote
  #4 (permalink)  
Old 11-30-04, 08:28
Pat Phelan Pat Phelan is offline
Resident Curmudgeon
 
Join Date: Feb 2004
Location: In front of the computer
Posts: 12,605
As I can't see a generic SQL solution, I'd recommend using:
Code:
SELECT d.id, d.name
   FROM documents AS d
   JOIN versions AS v
      ON (v.id = d.id)
   WHERE  d.name NOT LIKE '%.html'
-PatP
Reply With Quote
  #5 (permalink)  
Old 11-30-04, 08:59
bdonegan bdonegan is offline
Registered User
 
Join Date: Nov 2004
Posts: 3
Got It! Thanks for your help though. This was the final solution for anyone who's interested

SELECT Docs.Name, DocVersions.TimeCreated, DocVersions.Content
FROM DocVersions INNER JOIN
Docs ON DocVersions.Id = Docs.Id
WHERE (Docs.LeafName LIKE '%.doc' OR
Docs.Name LIKE '%.xls' OR
Docs.Name LIKE '%.ppt' OR
Docs.Name LIKE '%.pdf' OR
Docs.Name LIKE '%.mpp' OR
Docs.Name LIKE ' % .txt ') AND (Docs.DirName LIKE 'sites/archive/%')

I think i confused u all with my talk of leaving out the html document. It wasn't so much i wanted to leave that out as include the others if you know what i mean
Reply With Quote
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
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 On
Pingbacks are On
Refbacks are On