 |
| 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
|
|
|
|

November 20th, 2003, 04:24 PM
|
|
Authorized User
|
|
Join Date: Oct 2003
Posts: 29
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
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...
|
|

November 20th, 2003, 05:44 PM
|
|
Friend of Wrox
|
|
Join Date: Jun 2003
Posts: 839
Thanks: 0
Thanked 1 Time in 1 Post
|
|
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
|
|

November 20th, 2003, 05:54 PM
|
|
Authorized User
|
|
Join Date: Oct 2003
Posts: 29
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
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...
|
|

November 20th, 2003, 06:01 PM
|
|
Friend of Wrox
|
|
Join Date: Jun 2003
Posts: 839
Thanks: 0
Thanked 1 Time in 1 Post
|
|
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
|
|

November 20th, 2003, 06:16 PM
|
|
Authorized User
|
|
Join Date: Oct 2003
Posts: 29
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
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...
|
|

November 20th, 2003, 06:22 PM
|
|
Friend of Wrox
|
|
Join Date: Jun 2003
Posts: 839
Thanks: 0
Thanked 1 Time in 1 Post
|
|
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
|
|

November 20th, 2003, 06:29 PM
|
|
Authorized User
|
|
Join Date: Oct 2003
Posts: 29
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
it doesn't work. I can't return any data from database...
Thanks
|
|

November 20th, 2003, 06:44 PM
|
|
Friend of Wrox
|
|
Join Date: Jun 2003
Posts: 839
Thanks: 0
Thanked 1 Time in 1 Post
|
|
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
|
|

November 20th, 2003, 06:47 PM
|
|
Friend of Wrox
|
|
Join Date: Jun 2003
Posts: 839
Thanks: 0
Thanked 1 Time in 1 Post
|
|
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
|
|

November 20th, 2003, 06:55 PM
|
|
Authorized User
|
|
Join Date: Oct 2003
Posts: 29
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
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.
|
|
 |