Hi! I'm trying to put together some reports for an attorney's office. I'm having trouble constructing this query. What I'm doing is querying for defendants who do not have a particular charge against them.
Here are my tables:
DCase
----------
VBKey CaseNumber
1 33365
2 66585
etc..
DCharge
----------
VBKey ChargeNum
1 27
2 19
3 20
3 21
etc..
Since both tables are linked via VBKey, I joined them together and this is where I got stuck.
I tried using HAVING ChargeNum<>21 but it would still return a result b/c VBkey=3 has two charges against him. So, when do my queries, I'm still getting individuals who have multiple charges. I tried doing this with SELECT DISTINCT but it would still give me results from people with 3 or more charges. I'm lost at how to complete this query. Any help will be greatly appreciated! Thank you!SELECT *
FROM DCase
WHERE NOT EXISTS (SELECT *
FROM DCharge
WHERE DCharge.VBKey = DCase.VBKey
AND 21 = ChargeNum)-PatP|||Thank you very much Pat! That worked perfectly! Do you know of any good books I can use to better my sql skills?|||There are quite literally thousands of good books on using SQL. There are lots of tips and tricks that are product specific, and those are especially important for someone who's getting started.
If your primary environment is VB, I'd recommend Hitchhiker's Guide to Visual Basic and SQL Server (http://search.barnesandnoble.com/booksearch/isbnInquiry.asp?z=y&isbn=1572318481&itm=2). If you're looking for something a bit more generic, I'd suggest SQL Queries for Mere Mortals: A Hands-On Guide to Data Manipulation in SQL (http://search.barnesandnoble.com/booksearch/isbnInquiry.asp?z=y&isbn=0201433362&itm=1). If you really want to "grok" SQL, then I'd recommend almost anything by Joe Celko, but especially Joe Celko's SQL for Smarties: Advanced SQL Programming (http://search.barnesandnoble.com/booksearch/isbnInquiry.asp?z=y&isbn=0123693799&itm=1). In the interest of "full disclosure", while I count all of these folks as my friends or acquaintances, I make nothing from their books.
-PatP
No comments:
Post a Comment