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 June 15th, 2005, 01:41 PM
Registered User
 
Join Date: Apr 2005
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts
Default SQL query?

I have a table as following,
Field1 Field2 Field3
1 a 1
2 a 2
3 a 3
4 b 4
5 c 5
I would like to create a query only retrieve records which Value of field2 is unique.
results will be:
Field1 Field2 Field3
1 b 4
1 c 5

Thanks

spark
 
Old June 15th, 2005, 01:50 PM
Friend of Wrox
 
Join Date: Jun 2003
Posts: 2,189
Thanks: 5
Thanked 59 Times in 57 Posts
Send a message via MSN to gbianchi
Default

hi there

Code:
 
SELECT Last(field1), Last(field2) , Last(field3) 
FROM table1
GROUP BY field2
HAVING Count(field2)=1
.

HTH

Gonzalo
 
Old June 15th, 2005, 02:20 PM
Registered User
 
Join Date: Apr 2005
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts
Default

I know it works for access. But I am using showcase query now. It does not have last(),First(). Thanks for this idea.

spark

 
Old June 15th, 2005, 02:24 PM
Friend of Wrox
 
Join Date: Jun 2003
Posts: 2,189
Thanks: 5
Thanked 59 Times in 57 Posts
Send a message via MSN to gbianchi
Default

a replace with max or min will do the same trick since you are always retriving unique rows..

HTH

Gonzalo
 
Old June 15th, 2005, 08:39 PM
Friend of Wrox
 
Join Date: Jun 2003
Posts: 839
Thanks: 0
Thanked 1 Time in 1 Post
Default

Quote:
quote:
I would like to create a query only retrieve records which Value of field2 is unique.
results will be:
Field1 Field2 Field3
1 b 4
1 c 5
I do not understand how you arrive at the values of 'Field1' in your resultset. For example, where does the value of '1' for Field1 come from for the row in the resultset where the Field2 value is 'b'?

If you want the rows in the table where Field2 is unique, i.e. those rows where a given value of Field2 is present in only one row then:
Code:
SELECT * FROM yourtable WHERE Field2 IN
    (SELECT Field2 FROM yourtable GROUP BY Field2 HAVING COUNT(*)=1)
Jeff Mason
Custom Apps, Inc.
www.custom-apps.com





Similar Threads
Thread Thread Starter Forum Replies Last Post
sql query i need seearam MySQL 7 November 30th, 2008 03:14 AM
Output Query to txt file from SQL Query everest SQL Server 2005 4 November 22nd, 2007 01:49 AM
SQL Query!! dpkbahuguna Beginning VB 6 5 October 12th, 2007 12:39 AM
Help with SQL query sattaluri Access 2 August 11th, 2006 09:26 AM
SQL query PinkyCat Classic ASP Databases 3 March 11th, 2005 01:41 PM





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