Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

ADo select double numbers

Posted on 2009-04-03
9
Medium Priority
?
203 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 5
  • 4
9 Comments
 
LVL 38

Expert Comment

by:Geert Gruwez
ID: 24057849
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
ID: 24057862
I am using Access
0
 
LVL 38

Expert Comment

by:Geert Gruwez
ID: 24057904
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
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 

Author Comment

by:Nayel
ID: 24057920
No no.. I am using Delphi which is connected to Access. And the query should be written in an ADOQUERY
0
 
LVL 38

Expert Comment

by:Geert Gruwez
ID: 24057950
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
ID: 24058035
Getting a fault.
0
 
LVL 38

Expert Comment

by:Geert Gruwez
ID: 24058039
and that would be ?

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

Author Comment

by:Nayel
ID: 24058073
A syntaxerror in expression Min(M_Name) XName
0
 
LVL 38

Accepted Solution

by:
Geert Gruwez earned 200 total points
ID: 24058080
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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

A lot of questions regard threads in Delphi.   One of the more specific questions is how to show progress of the thread.   Updating a progressbar from inside a thread is a mistake. A solution to this would be to send a synchronized message to the…
Introduction The parallel port is a very commonly known port, it was widely used to connect a printer to the PC, if you look at the back of your computer, for those who don't have newer computers, there will be a port with 25 pins and a small print…
Are you ready to place your question in front of subject-matter experts for more timely responses? With the release of Priority Question, Premium Members, Team Accounts and Qualified Experts can now identify the emergent level of their issue, signal…
In a question here at Experts Exchange (https://www.experts-exchange.com/questions/29062564/Adobe-acrobat-reader-DC.html), a member asked how to create a signature in Adobe Acrobat Reader DC (the free Reader product, not the paid, full Acrobat produ…
Suggested Courses

610 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