Wrox Programmer Forums
| Search | Today's Posts | Mark Forums Read
Beginning VB 6 For coders who are new to Visual Basic, working in VB version 6 (not .NET).
Welcome to the p2p.wrox.com Forums.

You are currently viewing the Beginning VB 6 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
  #1 (permalink)  
Old October 11th, 2007, 07:24 AM
Friend of Wrox
 
Join Date: Jan 2006
Location: Hardwar, Uttranchal, India.
Posts: 180
Thanks: 1
Thanked 1 Time in 1 Post
Send a message via ICQ to dpkbahuguna Send a message via MSN to dpkbahuguna Send a message via Yahoo to dpkbahuguna
Default SQL Query!!

Hi friends!!

            I have this table:

     CODE Name
        a d1
        a
        b d2

            I have to find distinct code but all the rows are coming
i am doing like : SELECT DISTINCT(CODE), NAME FROM TABLENAME;
            But result is coming like:
      CODE Name
        a
        a d1
        b d2
            while I just expecting two rows with unique codes.
help me please here.
     Thanks Readers!!!!!!!


DPK..
__________________
DPK..
  #2 (permalink)  
Old October 11th, 2007, 07:34 AM
Friend of Wrox
Points: 7,395, Level: 36
Points: 7,395, Level: 36 Points: 7,395, Level: 36 Points: 7,395, Level: 36
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Jun 2003
Location: Capital Federal, , Argentina.
Posts: 2,189
Thanks: 5
Thanked 59 Times in 57 Posts
Send a message via MSN to gbianchi
Default

well.. you in fact have 3 codes (empty, d1 and d2).. if you want to avoid empty fields you will have to add a where to avoid them....

HTH

Gonzalo

================================================== =========
Read this if you want to know how to get a correct reply for your question:
http://www.catb.org/~esr/faqs/smart-questions.html
^^Took that from dparsons signature and he Took that from planoie's profile
================================================== =========
My programs achieved a new certification (can you say the same?):
WORKS ON MY MACHINE
http://www.codinghorror.com/blog/archives/000818.html
================================================== =========
I know that CVS was evil, and now i got the proof:
http://worsethanfailure.com/Articles...-Hate-You.aspx
================================================== =========
  #3 (permalink)  
Old October 11th, 2007, 07:45 AM
Wrox Author
Points: 13,255, Level: 49
Points: 13,255, Level: 49 Points: 13,255, Level: 49 Points: 13,255, Level: 49
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Oct 2005
Location: Ohio, USA
Posts: 4,104
Thanks: 1
Thanked 64 Times in 64 Posts
Send a message via AIM to dparsons
Default

Quote:
quote:Originally posted by gbianchi
 well.. you in fact have 3 codes (empty, d1 and d2)..


To better elaborate that you do, in fact, have 3 distinct rows but only 2 distinct codes.

If you execute this query:

SELECT Distinct Code from <table>

You will return :
a
b

but since name contains 3 unique values, d1, d2, and an empty string you get the result set you mentioned in your original post. DISTINCT is not an Aggregate function as you have tried to do in your query and, as such, you cant simply execte distinct on one column and not the rest of the columns in your query.

To get your desired results your query needs to be:

SELECT DISTINCT Code, Name from <table> where Name <> ''

hth.

================================================== =========
Read this if you want to know how to get a correct reply for your question:
http://www.catb.org/~esr/faqs/smart-questions.html
================================================== =========
Technical Editor for:
Beginning Visual C# 2008
Professional Search Engine Optimization with ASP.NET
Professional IIS 7 and ASP.NET Integrated Programming
Wrox Blox: Introduction to Google Gears
Wrox Blox: Create Amazing Custom User Interfaces with WPF and .NET 3.0
================================================== =========
  #4 (permalink)  
Old October 11th, 2007, 02:42 PM
Friend of Wrox
 
Join Date: Nov 2004
Location: Port Orchard, WA, USA.
Posts: 1,621
Thanks: 1
Thanked 3 Times in 3 Posts
Default

I thought DISTINCT returned distinct rows, not distinct columns. Since the following three rows aren’t any of them the exact same:
Code:
    "a"   "d1"
    "a"   ""    
    "b"   "d2"
    all 3 are returned.

But further, if the SQL had operated as intended, the value of the NAME column would be poorly defined. He would have gotten [u]a</u> name value, but not one that had much significance.
  #5 (permalink)  
Old October 11th, 2007, 11:54 PM
Friend of Wrox
 
Join Date: Jan 2006
Location: Hardwar, Uttranchal, India.
Posts: 180
Thanks: 1
Thanked 1 Time in 1 Post
Send a message via ICQ to dpkbahuguna Send a message via MSN to dpkbahuguna Send a message via Yahoo to dpkbahuguna
Default

Thanks Dparsons!!!

    But as you gave me the query, It will not work for me because if I have a row that does not has NAME i mean it has <BLANK> in NAME field then I will miss that row and I want to fetch all the codes.

   I hope you get me..

Thanks again!!!

Quote:
quote:Originally posted by dparsons
 
Quote:
quote:Originally posted by gbianchi
Quote:
 well.. you in fact have 3 codes (empty, d1 and d2)..


To better elaborate that you do, in fact, have 3 distinct rows but only 2 distinct codes.

If you execute this query:

SELECT Distinct Code from <table>

You will return :
a
b

but since name contains 3 unique values, d1, d2, and an empty string you get the result set you mentioned in your original post. DISTINCT is not an Aggregate function as you have tried to do in your query and, as such, you cant simply execte distinct on one column and not the rest of the columns in your query.

To get your desired results your query needs to be:

SELECT DISTINCT Code, Name from <table> where Name <> ''

hth.

================================================== =========
Read this if you want to know how to get a correct reply for your question:
http://www.catb.org/~esr/faqs/smart-questions.html
================================================== =========
Technical Editor for:
Beginning Visual C# 2008
Professional Search Engine Optimization with ASP.NET
Professional IIS 7 and ASP.NET Integrated Programming
Wrox Blox: Introduction to Google Gears
Wrox Blox: Create Amazing Custom User Interfaces with WPF and .NET 3.0
================================================== =========
DPK..
  #6 (permalink)  
Old October 12th, 2007, 12:39 AM
Friend of Wrox
 
Join Date: Jan 2006
Location: Hardwar, Uttranchal, India.
Posts: 180
Thanks: 1
Thanked 1 Time in 1 Post
Send a message via ICQ to dpkbahuguna Send a message via MSN to dpkbahuguna Send a message via Yahoo to dpkbahuguna
Default

Thanks you all to give your time!!

   well I have code it by using this:

select distinct id,name from temp1
where name is not null
group by id,name
union all
select id,name from temp1
where id in ( select id from temp1 group by id having count(id) = 1)
and name is null;


Thanks again!!

DPK..


Similar Threads
Thread Thread Starter Forum Replies Last Post
sql query i need seearam MySQL 7 November 30th, 2008 03:14 AM
sql query ldp101068 SQL Server 2000 7 December 18th, 2007 04:52 PM
Output Query to txt file from SQL Query everest SQL Server 2005 4 November 22nd, 2007 01:49 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.