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 > Another Noob question

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 01-02-06, 14:38
noble24 noble24 is offline
Registered User
 
Join Date: Jan 2006
Posts: 2
Another Noob question

I am using SQL server 2000. I am trying to create the simplist of queries in my mind and I keep getting duplications and its driving me nuts. I have two tables. (MASTER, SLAVE we will call them) All I am trying to do is "show me data within this date range" and it gives me results but duplicates. Help. PLEASE do not refer me to http://www.dbforums.com/t1201578.html because that is unclear to me! Here is my code:

SELECT PLNAME, PFNAME, PMNAME, DOB, ACCOUNT, SERVICE DATE
FROM MASTER, SLAVE
WHERE TECH='TECHNAME' AND(SERVICE DATE>='20060101' AND SERVICE DATE<='20061230')
ORDER BY SERVICE DATE

I am a noob, but I can't see this being complex.
Reply With Quote
  #2 (permalink)  
Old 01-02-06, 15:24
Littlefoot Littlefoot is offline
Lost Boy
 
Join Date: Jan 2004
Location: Croatia, Europe
Posts: 3,629
SELECT DISTINCT ...

might solve your problem.

Oh, yes ... this query has two tables. Where did you join them? If you do not do that, you'll get cartesian product of all values. Therefore, joining tables properly might be another possible solution. Such as

...
WHERE slave.some_column = master.some_column
AND ...
Reply With Quote
  #3 (permalink)  
Old 01-02-06, 16:24
noble24 noble24 is offline
Registered User
 
Join Date: Jan 2006
Posts: 2
Hmmmmm

Well, I tried the distinct and it made my outcome worse. I do not have a join to the tables because I am not clear how to do it yet.
Reply With Quote
  #4 (permalink)  
Old 01-02-06, 17:16
Littlefoot Littlefoot is offline
Lost Boy
 
Join Date: Jan 2004
Location: Croatia, Europe
Posts: 3,629
Well, you'd better figure that out

I have no idea how your tables look like (it is unclear which table contains which column you provided in the sample query), but seeing "DOB" (Date Of Birth?), you might have something like PERSON_ID in both tables which *might* be the column you are looking for.
Reply With Quote
  #5 (permalink)  
Old 01-04-06, 16:33
napatan napatan is offline
Registered User
 
Join Date: Dec 2005
Posts: 3
Duplicate Values using SQL 2000 Server

Agree with Littlefoot. You must join the tables by creating a relationship between the two. In order to do this, each of the tables must contain a common KEY upon which the tables are to be linked.

To create a relationship between the tables, click on the relationship icon on the toolbar, follow the steps that ultimately will permit you to make a connection between the common KEY, a field in the database, usually holding unique data for each record, eg. SS#.
Reply With Quote
  #6 (permalink)  
Old 01-09-06, 11:51
healdem healdem is offline
Jaded Developer
 
Join Date: Nov 2004
Location: out on a limb
Posts: 9,246
The join syntax is pretty straightforward
lets assume that TECH is the common column

SELECT PLNAME, PFNAME, PMNAME, DOB, ACCOUNT, SERVICE DATE
FROM MASTER
join SLAVE on Slave.TECH=master.tech
WHERE MASTER.TECH='TECHNAME' AND(SERVICE DATE>='20060101' AND SERVICE DATE<=#20061230#)
ORDER BY SERVICE DATE

incidentally I think you will have a problem with spaces in column names
I'd suggest developing or using a naming convention.
Personally I've always used something that distinguishs tables form columnsm used captialisation to make things easier to read, but there as many naming conventions as politicians want to grab all the cash in your pocket. Find one htat works for you, or your organistion and stick with it.

Select PLName, PFName, PMName, DoB, Acct, SrvDate
FROM DTMaster
join DTSlave on Slave.Tech=DTMaster.Tech
WHERE DTMaster.Tech='TECHNAME' AND(SERVICE DATE>='20060101' AND SrvDate<='20061230')
ORDER BY SrvDate


HTH
Reply With Quote
  #7 (permalink)  
Old 01-13-06, 19:25
disruptivehair disruptivehair is offline
Registered User
 
Join Date: Dec 2005
Location: Texas
Posts: 100
Quote:
Originally Posted by noble24
I am using SQL server 2000. I am trying to create the simplist of queries in my mind and I keep getting duplications and its driving me nuts. I have two tables. (MASTER, SLAVE we will call them) All I am trying to do is "show me data within this date range" and it gives me results but duplicates. Help. PLEASE do not refer me to http://www.dbforums.com/t1201578.html because that is unclear to me! Here is my code:

SELECT PLNAME, PFNAME, PMNAME, DOB, ACCOUNT, SERVICE DATE
FROM MASTER, SLAVE
WHERE TECH='TECHNAME' AND(SERVICE DATE>='20060101' AND SERVICE DATE<='20061230')
ORDER BY SERVICE DATE

I am a noob, but I can't see this being complex.

What the others said, basically!

What columns correspond to what tables? Where is Tech? Is it on Master or Slave? The others are right; this syntax looks like it'll get you a cartesian product which isn't what you want. You want an inner join!

Also, if you're looking for a date range, maybe try using BETWEEN instead of 'SERVICE DATE>='20060101' AND SERVICE DATE<='20061230''
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