Wrox Programmer Forums

Need to download code?

View our list of code downloads.

Go Back   Wrox Programmer Forums > SQL Server > SQL Server 2000 > SQL Server 2000
Password Reminder
Register
| FAQ | Members List | Search | Today's Posts | Mark Forums Read
SQL Server 2000 General discussion of Microsoft SQL Server -- for topics that don't fit in one of the more specific SQL Server forums. version 2000 only. There's a new forum for SQL Server 2005.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the SQL Server 2000 section of the Wrox Programmer to Programmer discussions. This is a community of tens of thousands of software programmers and website developers including Wrox book authors and readers. As a guest, you can read any forum posting. By joining today you can post your own programming questions, respond to other developers’ questions, and eliminate the ads that are displayed to guests. Registration is fast, simple and absolutely free .
DRM-free e-books 300x50
Reply
 
Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old May 8th, 2007, 12:54 PM
Friend of Wrox
Points: 718, Level: 10
Points: 718, Level: 10 Points: 718, Level: 10 Points: 718, Level: 10
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Jan 2006
Location: Ennis, Clare, Ireland.
Posts: 131
Thanks: 10
Thanked 0 Times in 0 Posts
Default New Insert Query

Hi,

I have two tables (Table1, Table2) containing a 'Year' field (i.e. Values of '1998', '1999', '2000' ..... '2007').
Both are data Source tables.

I have a third (Table3) which I need to update depending on a SELECT DISTINCT YEAR statement form the other two tables. Once I populate Table3 with the Years from Table1, how do I then only add records from Table2 which aren't already contained in Table3.

EXAMPLE

TABLE1
Year
1992
1994
1995
1996

TABLE2
Year
1992
1993
1994
1995
1997

So TABLE3 should contain:
Year
1992
1994
1995
1996
1993
1997

I know the first query is:

Code:
INSERT INTO dbo.TABLE3
(
[Year]
)
SELECT DISTINCT dbo.TABLE1.[YEAR] 
FROM dbo.TABLE1
GROUP BY dbo.TABLE1.[YEAR]
But how do I add the values from Table2?

Thanks in advance,




Neal

A Northern Soul
__________________
Neal

A Northern Soul
Reply With Quote
  #2 (permalink)  
Old May 8th, 2007, 12:59 PM
Friend of Wrox
 
Join Date: Jun 2003
Location: Bangalore, KA, India.
Posts: 2,480
Thanks: 0
Thanked 1 Time in 1 Post
Default

Is there any reason why you use DISTINCT and still do a GROUP BY
Code:
INSERT INTO dbo.TABLE3
(
[Year]
)
SELECT DISTINCT dbo.TABLE1.[YEAR] 
FROM dbo.TABLE1
GROUP BY dbo.TABLE1.[YEAR]
UNION
SELECT DISTINCT dbo.TABLE2.[YEAR] 
FROM dbo.TABLE2
GROUP BY dbo.TABLE2.[YEAR]
Cheers

_________________________
- Vijay G
Strive for Perfection
Reply With Quote
  #3 (permalink)  
Old May 8th, 2007, 01:06 PM
Friend of Wrox
 
Join Date: Nov 2003
Location: Lehigh Acres, FL, USA.
Posts: 625
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via MSN to jemacc
Default

Try using a sub query similar to this one
SELECT [YEAR]
FROM Table3
WHERE NOT EXISTS
   (SELECT *
   FROM table2
   WHERE table2.pkID = table3.pkid)

Jaime E. Maccou
Reply With Quote
  #4 (permalink)  
Old May 8th, 2007, 01:22 PM
Friend of Wrox
Points: 718, Level: 10
Points: 718, Level: 10 Points: 718, Level: 10 Points: 718, Level: 10
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Jan 2006
Location: Ennis, Clare, Ireland.
Posts: 131
Thanks: 10
Thanked 0 Times in 0 Posts
Default

Thanks for the prompt responses, Guys.

Neal

A Northern Soul
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 Off
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Insert Into Query. rupen Access VBA 5 July 30th, 2007 09:58 AM
Where does insert query go? bph Access VBA 5 June 12th, 2007 12:26 PM
INSERT Query Neal SQL Server 2000 2 May 3rd, 2007 05:18 PM
I solved insert query.now see this Update Query. amit_mande@yahoo.com VB.NET 2002/2003 Basics 2 September 21st, 2006 12:48 AM
INSERT INTO QUERY sanjna000 Excel VBA 5 November 26th, 2004 12:41 AM



All times are GMT -4. The time now is 04:42 PM.


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