Wrox Programmer Forums
|
SQL Language SQL Language discussions not specific to a particular RDBMS program or vendor.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the SQL Language 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 November 20th, 2003, 04:24 PM
Authorized User
 
Join Date: Oct 2003
Posts: 29
Thanks: 0
Thanked 0 Times in 0 Posts
Default SQL

Hi All,

I am trying to create sql code that I would like to get the row Num 2 and row Num4. In other words, I am trying to pull the records that shoud not duplicate in column Num, T and P.

Num X Y Z Q T P
1 A 1 2 4 rm FW45
2 A 2 3 4 rm FW43
3 A 6 7 8 rm FW45
4 B 2 3 4 rmx FW43
5 B 1 2 3 rm FW44
6 B 3 4 5 rm FW44

Thanks...


 
Old November 20th, 2003, 05:44 PM
Friend of Wrox
 
Join Date: Jun 2003
Posts: 839
Thanks: 0
Thanked 1 Time in 1 Post
Default

Not sure I understand what you are looking for for output, but how about:
Code:
SELECT Num FROM yourtable T1
    WHERE NOT EXISTS(SELECT * FROM yourtable T2
        WHERE T1.T=T2.T AND T1.P=T2.P AND T1.Num<>T2.Num);
Jeff Mason
Custom Apps, Inc.
www.custom-apps.com
 
Old November 20th, 2003, 05:54 PM
Authorized User
 
Join Date: Oct 2003
Posts: 29
Thanks: 0
Thanked 0 Times in 0 Posts
Default

I am trying to obtain the following table. And I have only one table.

Num X Y Z Q T P
2 A 2 3 4 rm FW43
4 B 2 3 4 rmx FW43

Thanks...

 
Old November 20th, 2003, 06:01 PM
Friend of Wrox
 
Join Date: Jun 2003
Posts: 839
Thanks: 0
Thanked 1 Time in 1 Post
Default

OK. Just add the additional columns to the SELECT list - this query checks for duplicate values in the T and P columns only. (And Num, but that is always unique as far as I can see).
Code:
SELECT Num, X, Y, Z, Q, T, P FROM yourtable T1
    WHERE NOT EXISTS(SELECT * FROM yourtable T2
        WHERE T1.T=T2.T AND T1.P=T2.P AND T1.Num<>T2.Num);
Jeff Mason
Custom Apps, Inc.
www.custom-apps.com
 
Old November 20th, 2003, 06:16 PM
Authorized User
 
Join Date: Oct 2003
Posts: 29
Thanks: 0
Thanked 0 Times in 0 Posts
Default

I am trying to check for duplicate values in the X,T and P columns only. I don't have second table that I can compare these values, I have only one table.

Thanks...

 
Old November 20th, 2003, 06:22 PM
Friend of Wrox
 
Join Date: Jun 2003
Posts: 839
Thanks: 0
Thanked 1 Time in 1 Post
Default

Quote:
quote:Originally posted by erin
 I am trying to check for duplicate values in the X,T and P columns only. I don't have second table that I can compare these values, I have only one table.
...and the query I posted should do that for you...

Jeff Mason
Custom Apps, Inc.
www.custom-apps.com
 
Old November 20th, 2003, 06:29 PM
Authorized User
 
Join Date: Oct 2003
Posts: 29
Thanks: 0
Thanked 0 Times in 0 Posts
Default

it doesn't work. I can't return any data from database...

Thanks

 
Old November 20th, 2003, 06:44 PM
Friend of Wrox
 
Join Date: Jun 2003
Posts: 839
Thanks: 0
Thanked 1 Time in 1 Post
Default

I don't know what to say.

I set up a table with exactly the data you supplied and ran the query I posted, and it returned the correct results.

Perhaps if you posted the query you tried (along with the exact structure of your table)?

Jeff Mason
Custom Apps, Inc.
www.custom-apps.com
 
Old November 20th, 2003, 06:47 PM
Friend of Wrox
 
Join Date: Jun 2003
Posts: 839
Thanks: 0
Thanked 1 Time in 1 Post
Default

Wait.

I just noticed.

In your original post you said:
Quote:
quote:
I am trying to pull the records that shoud not duplicate in column Num, T and P.
And in a later post you said:
Quote:
quote:
I am trying to check for duplicate values in the X,T and P columns only
So which is it?

Jeff Mason
Custom Apps, Inc.
www.custom-apps.com
 
Old November 20th, 2003, 06:55 PM
Authorized User
 
Join Date: Oct 2003
Posts: 29
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Yes, you are right, second quote is true which is
-I am trying to check for duplicate values in the X,T and P columns only.






Similar Threads
Thread Thread Starter Forum Replies Last Post
How Run .sql Script file in MS SQL Server 2000? aarkaycee SQL Server 2000 5 October 12th, 2009 05:43 AM
creating ssis packagte for sql server to sql serer Laxmikant_it ASP.NET 3.5 Professionals 0 November 26th, 2008 12:23 AM
Converting from MS SQL 2005 to Sql Epress edition saif44 SQL Language 0 February 16th, 2007 04:17 PM
Failed to copy objects from SQL server to SQL Serv monfu SQL Server 2000 4 December 4th, 2005 05:54 PM
Move SQL DB from one sql to another sql server Israr SQL Server 2000 3 January 24th, 2005 02:13 PM





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