If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

 
Go Back  dBforums > Database Server Software > MySQL > Looping through distinct records...

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 03-16-09, 22:56
bla4free bla4free is offline
Registered User
 
Join Date: Jan 2005
Posts: 165
Looping through distinct records...

I don't know how to word this so I'll give it my best shot. I have two tables: a "charges" table and a "people" table:

Code:
C H A R G E S

CASE_ID  PEOPLE_ID  CHARGE_ID
1        3          16
1        3          18
1        3          21
1        3          32
1        7          16
1        7          18
1        7          21
1        9          16
1        9          11
Code:
P E O P L E

PEOPLE_ID  NAME
3          John Doe
7          Jane Smith
9          Duncan Mann
Basically, I want to loop through all these charges and display the distinct charges, with the names of each person associated with those charges. Is this possible? Basically, my results would look like this:

Code:
R E S U L T S

CHARGE_ID   NAME
16          John Doe, Jane Smith, Duncan Mann
18          John Doe, Jane Smith
21          John Doe, Jane Smith
32          John Doe
11          Duncan Munn
How can I accomplish this? I don't even know where to start. Thanks!
Reply With Quote
  #2 (permalink)  
Old 03-17-09, 00:49
r937 r937 is offline
SQL Consultant
 
Join Date: Apr 2002
Location: Toronto, Canada
Posts: 19,534
Code:
SELECT charges.charge_id
     , GROUP_CONCAT(people.name) AS names
  FROM charges
INNER
  JOIN people
    ON people.people_id = charges.people_id
GROUP
    BY charges.charge_id
__________________
r937.com | rudy.ca
please visit Simply SQL and buy my book
Reply With Quote
  #3 (permalink)  
Old 03-17-09, 04:48
healdem healdem is online now
Jaded Developer
 
Join Date: Nov 2004
Location: out on a limb
Posts: 9,262
don't loop, use SQL as R937 has suggested
looping is less efficient than SQL and will almost certainly cost more in terms of processor time, network bandwidth
__________________
I'd rather be riding my Versys or my Tiger 800 let alone the Norton
Reply With Quote
  #4 (permalink)  
Old 03-17-09, 12:17
bla4free bla4free is offline
Registered User
 
Join Date: Jan 2005
Posts: 165
Quote:
Originally Posted by r937
Code:
SELECT charges.charge_id
     , GROUP_CONCAT(people.name) AS names
  FROM charges
INNER
  JOIN people
    ON people.people_id = charges.people_id
GROUP
    BY charges.charge_id
Thanks! That works almost perfectly. If I have a person that has multiple of the same charge_id, their name will appear multiple times. Is there a way to prevent that? Thanks!
Reply With Quote
  #5 (permalink)  
Old 03-17-09, 12:27
r937 r937 is offline
SQL Consultant
 
Join Date: Apr 2002
Location: Toronto, Canada
Posts: 19,534
um... could you repeat that please?

the query aggregates on charge_id, so multiple people are concatenated into the names column, one row per charge_id

what did you get that looked wrong to you?
__________________
r937.com | rudy.ca
please visit Simply SQL and buy my book
Reply With Quote
  #6 (permalink)  
Old 03-17-09, 21:21
bla4free bla4free is offline
Registered User
 
Join Date: Jan 2005
Posts: 165
When I applied it in my situation, it worked almost perfectly. I guess one aspect I left out is people can have multiple of the same charges:

Code:
C H A R G E S

CASE_ID  PEOPLE_ID  CHARGE_ID
1        3          16
1        3          16
1        3          18
1        3          21
1        3          32
1        7          16
1        7          18
1        7          21
1        9          16
1        9          11
Code:
P E O P L E

PEOPLE_ID  NAME
3          John Doe
7          Jane Smith
9          Duncan Mann
Code:
R E S U L T S

CHARGE_ID   NAME
16          John Doe, John Doe, Jane Smith, Duncan Mann
18          John Doe, Jane Smith
21          John Doe, Jane Smith
32          John Doe
11          Duncan Munn
I've modified my example from above to show you what I get. Sometimes, people can have 4 or 5 of the same charges. Does that make more sense? Thanks!
Reply With Quote
  #7 (permalink)  
Old 03-17-09, 21:35
r937 r937 is offline
SQL Consultant
 
Join Date: Apr 2002
Location: Toronto, Canada
Posts: 19,534
Quote:
Originally Posted by bla4free
Does that make more sense?
no, less

why would you allow that?

"people can have multiple of the same charges"

why???

if you store a fact twice, does that make it more believable as a fact?
__________________
r937.com | rudy.ca
please visit Simply SQL and buy my book
Reply With Quote
  #8 (permalink)  
Old 03-18-09, 08:47
bla4free bla4free is offline
Registered User
 
Join Date: Jan 2005
Posts: 165
This is a case management system for a prosecutor's office. The charge_id's represent the various crimes the individual person committed. A person can be charged with multiple counts of armed robbery.
Reply With Quote
  #9 (permalink)  
Old 03-18-09, 09:00
r937 r937 is offline
SQL Consultant
 
Join Date: Apr 2002
Location: Toronto, Canada
Posts: 19,534
Quote:
Originally Posted by bla4free
I guess one aspect I left out is ...
i guess you left out a lot more than you left in, eh
Code:
SELECT charge_id
     , GROUP_CONCAT(name) AS names
  FROM (
       SELECT DISTINCT
              charges.charge_id
            , people.name
         FROM charges
       INNER
         JOIN people
           ON people.people_id = charges.people_id
       ) AS d
GROUP
    BY charge_id
__________________
r937.com | rudy.ca
please visit Simply SQL and buy my book
Reply With Quote
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On