Solved

ADo select double numbers

Posted on 2009-04-03
9
191 Views
Last Modified: 2013-11-23
I have a table with
Name, Number, Extention, Group
Eacht name has 2 extentions but the number is the same.

Now I need to selct one name from each Number. I am still getting double records as result.
I have
Jack, 3, 124, Marketing and I have Jack 1, 3, 123, Marketing
I need to select only one of these
My code is 

     SQL.Add('Select  distinct M_number, M_name, M_Extention, Group');

     SQL.Add('From Employees);

     SQL.Add('Where (Group = '+QuotedStr(Group)+')');
 
 

What is wrong in this?

Open in new window

0
Comment
Question by:Nayel
  • 5
  • 4
9 Comments
 
LVL 36

Expert Comment

by:Geert Gruwez
Comment Utility
you are selecting multiple different ones with distinct not 1

and use parameters for performance, allways better !
and Group is a reserved word (bettter to use other column name)





as i dunno what database ... for oracle:

SELECT M_number, M_name, M_Extention, Group

FROM (SELECT M_number, M_name, M_Extention, Group, RANK() OVER (PARTITION BY M_Number ORDER BY M_Number) X

            WHERE GROUP = :AGRoup)

WHERE X = 1

Open in new window

0
 

Author Comment

by:Nayel
Comment Utility
I am using Access
0
 
LVL 36

Expert Comment

by:Geert Gruwez
Comment Utility
that's not a database ... it's a flat file :)

SELECT E.*
FROM Employees E,
  (SELECT M_Number, Min(M_Name) XName FROM Employees Group By M_Number) X
WHERE E.M_Number = X.M_Number
  AND E.M_Name = X.XName

0
 

Author Comment

by:Nayel
Comment Utility
No no.. I am using Delphi which is connected to Access. And the query should be written in an ADOQUERY
0
How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

 
LVL 36

Expert Comment

by:Geert Gruwez
Comment Utility
just kidding
var AdoQuery: TAdoQuery;

begin

  AdoQuery.SQL.Text := 

    'SELECT E.* FROM Employees E,           '+

    '  (SELECT M_Number, Min(M_Name) XName  '+ 

    '   FROM Employees Group By M_Number) X '+

    'WHERE E.M_Number = X.M_Number          '+

    '  AND E.M_Name = X.XName               '+

    '  AND E.Group = :GROUP                 ';

  AdoQuery.Parameters.ParamByName('GROUP').Value := Group;

  AdoQuery.Open;

Open in new window

0
 

Author Comment

by:Nayel
Comment Utility
Getting a fault.
0
 
LVL 36

Expert Comment

by:Geert Gruwez
Comment Utility
and that would be ?

i tried to smell what it was, but i have a small cold ...
0
 

Author Comment

by:Nayel
Comment Utility
A syntaxerror in expression Min(M_Name) XName
0
 
LVL 36

Accepted Solution

by:
Geert Gruwez earned 50 total points
Comment Utility
I'm not supergood in access,
I try to stay away from it,
but try this:
var AdoQuery: TAdoQuery;

begin

  AdoQuery.SQL.Text := 

    'SELECT E.* FROM Employees E,              '+

    '  (SELECT M_Number, Min(M_Name) AS XName  '+ 

    '   FROM Employees Group By M_Number) AS X '+

    'WHERE E.M_Number = X.M_Number             '+

    '  AND E.M_Name = X.XName                  '+

    '  AND E.Group = :GROUP                    ';

  AdoQuery.Parameters.ParamByName('GROUP').Value := Group;

  AdoQuery.Open;

Open in new window

0

Featured Post

What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

The uses clause is one of those things that just tends to grow and grow. Most of the time this is in the main form, as it's from this form that all others are called. If you have a big application (including many forms), the uses clause in the in…
This article explains how to create forms/units independent of other forms/units object names in a delphi project. Have you ever created a form for user input in a Delphi project and then had the need to have that same form in a other Delphi proj…
Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, Just open a new email message.  In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…
Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…

772 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now