Wrox Programmer Forums
|
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 software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old May 8th, 2007, 12:54 PM
Friend of Wrox
 
Join Date: Jan 2006
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
 
Old May 8th, 2007, 12:59 PM
Friend of Wrox
 
Join Date: Jun 2003
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
 
Old May 8th, 2007, 01:06 PM
Friend of Wrox
 
Join Date: Nov 2003
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
 
Old May 8th, 2007, 01:22 PM
Friend of Wrox
 
Join Date: Jan 2006
Posts: 131
Thanks: 10
Thanked 0 Times in 0 Posts
Default

Thanks for the prompt responses, Guys.

Neal

A Northern Soul





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. [email protected] 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





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