Group / Union mysql report

I'm trying to get data out of a huge mysql table

Leader |   Email               |   Name
n          |     |  Orla
y          |     |   Tom
n          |    |   Lary
n          |   |  Richard
n          |   | Iris
y          |    | valery

I want to output the list in the following format

Tom     |                |
            |  Orla       |
            |  Richard  |
            | Iris          |
Valery |                |
            | Larry      |

Tom is leader of group who have email addresses
Valery is leader of group  with email address

There are over 40,000 records

Who is Participating?
vbNewbie2009Connect With a Mentor Commented:
You really need a fourth column or a substring of the email column to just pull out the domain name.  When ordering by the entire email address it simply does an alpha-sort; not alpha-sort by domain name.

Then when ordering by 'Email', 'Name2', 'Name1' it will appear as desired.

I through together a different query option that would use a case instead of Union.  They would both get you to the same end result but using the case only necessitates parsing the table recordset once as opposed to multiple times with the union
select *
	case leader
	     when 'y' then name
	end as Leader
	,case leader
	     when 'n' then name
	end as Follower
	,SUBSTRING(email,INSTR(email,'@') + 1) as domain
     from tblName
order by domain, follower, leader

Open in new window

Select * from (
select name as Name1, '' as Name2, Email from mytable where leader = 'Y' Union
select '' as Name1, name as Name2, Email from mytable where leader = 'N'
) x
order by Email, Name1, Name2

joomlaAuthor Commented:
Hi Ss
thanks for your input
unfortunately it didn't work
obviously it produced an output, but it's when I tried on a subset of 17 records it returns 34 records

i can't find an obvious link to help you intrepret the results
joomlaAuthor Commented:
Hi Ss,
my apolgies, there were actually 34 records in the table I was working on

The output is not quite what I want   (see below)
I tried changing the order in the syntax but it makes do diff

            |  Orla       |
            |  Richard  |
            | Iris          |
Tom     |                |
            | Larry      |
Valery |                |
joomlaAuthor Commented:
thanks for offering this solution.
my apologies for not getting back sooner.
i had been on a deadline and lost focus
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.