473,402 Members | 2,046 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,402 software developers and data experts.

Here's a Curve Ball: Limit number of records on Left Join

dlite922
1,584 Expert 1GB
I've heard this may not be possible, but never hurts to ask.

I've got three tables, Case, Violator, Alias

Case is a court police case
Violator is the person
Alias is different names (aliases) that person has.

Here's my Query:

Expand|Select|Wrap|Line Numbers
  1.  
  2. SELECT 
  3. c.id , 
  4. c.caseNumber , 
  5. v.lastName , 
  6. v.firstName , 
  7. a.firstName  AS aliasFirstName, 
  8. a.lastName AS aliasLastName, 
  9. v.driverLicenseNumber , 
  10. v.dateOfBirth , 
  11. t.courtDate , 
  12. c.status 
  13.  
  14. FROM 
  15. `case` as c LEFT JOIN trial as t ON c.id = t.caseID, 
  16. violator as v LEFT JOIN  alias as a ON v.id = a.violatorID 
  17.  
  18. WHERE 
  19. c.violatorID = v.id AND 
  20. ( c.status IN ('CONT','ARR','DE','FTAP','OJW','PRO','STAY','WRNT')) 
  21.  
  22. ORDER BY 1 DESC;
  23.  
  24.  
  25. //Here's some test results from fake data, some fields are cut off because of no room. 
  26.  
  27. +-----+------------+-----------+-------------+----------------+---------------+
  28. | id  | caseNumber | lastName  | firstName   | aliasFirstName | aliasLastName 
  29. +-----+------------+-----------+-------------+----------------+---------------+
  30. | 117 | A001       | DOE       | JOHN        | NULL           | NULL
  31. | 116 | 07PA36503  | AVENT     | CHRISTOPHER | NULL
  32. | 115 | 07PA33856  | APPLEBY   | KENT        | NULL
  33. | 114 | 07PA36620  | ANDERSON  | JOSHUA      | Josh
  34. | 114 | 07PA36620  | ANDERSON  | JOSHUA      | Joshi
  35. | 113 | 07PA35300  | ADNDERSON | DONALD      | NULL           | NULL
  36. | 112 | 07PA36487  | AKHTAR    | NASER       | NULL           | NULL
  37. | 111 | 07PA36488  | AKHTAR    | NASER       | NULL           | NULL
  38. | 110 | CA0003     | ARANJUEZ  | ALAN        | NULL           | NULL
  39. | 109 | CA0001     | CARINOS   | JOHNY       | Jimmy          | Car
  40. | 106 | CA0002     | MILLS     | CORRINE     | NULL           | NULL
  41. +-----+------------+-----------+-------------+----------------+---------------+
  42.  
  43. //expect result is for the second caseID 114 to not be displayed. 
  44.  
  45.  
As you can see CaseID 114 is listed twice because Joshua Anderson has 2 alias records (Josh and Joshi)

How can I change my query so that it only displays the first alias, and each case only ONCE, but i need it to search all the aliases.

That is, If i type in Joshi, it will still pull up that record, but the listing will display Josh, because that's the first alias.

Complicated I know.

Anybody up for the challenge if even possible?


Thanks,



Dan
Jul 11 '08 #1
2 1909
dlite922
1,584 Expert 1GB
Okay, I feel stupid.

Just add GROUP BY c.id !!

Tada!!

Problem Solved.


Dan
Jul 12 '08 #2
coolsti
310 100+
Just in case you are not aware of this option, if you wish to also retrieve a delineated list of all the aliases as one string variable (which you can display or manipulate after retrieving from the database), you can use the GROUP_CONCAT function when you have a GROUP BY clause.

The default deliniator in group_concat is a comma, but you can specify instead what should be used. For direct display purposes, I usually set the delineator to ', ' which is a comma followed by a space, as this looks better.
Jul 12 '08 #3

Sign in to post your reply or Sign up for a free account.

Similar topics

1
by: Steve | last post by:
I've run in to a problem with a query I'm trying to write. I have attached a sample SQL script at the end of this post to show an overview of what I'm working with. I want to be able to use...
5
by: Haisoo Shin | last post by:
Hello.. I am working on a PHP/PEAR web page that shows statistics of how many people read a certain article during given period of time. I have, say, two tables called 'books' and 'logs'. The...
2
by: Edward | last post by:
SQL Server 7.0 If I run the following in Query Analyzer I get no records returned: exec GetLeadsOutcome_Dealer '1/1/2003','12/2/2003',10, '176, 183' If, however, I run either : exec...
2
by: AG | last post by:
I have a very big table with 20 million records DistinctProjectionKey which i join several times to different tables in this query. select distinct distinctprojectionkeyid,dpk.MarketID,...
2
by: Dom | last post by:
I need to run a query based on a query in Access. The second query has a number of conditions which all work well, but there is one more contition I need to set to make it run properly. the...
3
by: deko | last post by:
Is there any way to limit the number of records loaded into a ListBox? I looked at qdf.MaxRecords (to apply to the query that is the RowSource of the ListBox) but that only applies to ODBC data...
1
by: aaron.reese | last post by:
Guys, this is my problem:- I have records which are linked by two fields on a left inner join (there may be 0,1 or more records in the right hand table) The relationship is not unique (it's...
2
by: mmitchell_houston | last post by:
I'm working on a .NET project and I need a single query to return a result set from three related tables in Access 2003, and I'm having trouble getting the results I want. The details: ...
2
by: Ivor Somerset | last post by:
Hi, I've an Access DB table ("Groups") where data are as follow: Id Group Rank Item 1 1 1 7364 2 1 2 283 3 1 3 34888 4 2 1 277 5 2 2 8233
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.