tags:

views:

70

answers:

2

Basically after executing this query:

SELECT
`view_customer_locations`.customerid,
`view_customer_locations`.community_groupid,
`view_customer_locations`.community_group,


`view_sip_user_agents`.sip_user_agentid,
`view_sip_user_agents`.didid,
`view_sip_user_agents`.temporary_didid,
`view_sip_user_agents`.active_did,
GROUP_CONCAT( (IF(`view_sip_user_agents`.active_did = 'permanent', cast(`permanent_dids`.did  as char(10)), cast(`temporary_dids`.did as char(10)))) SEPARATOR ', ') as did,

`view_sip_user_agents`.sip_user_agents_date_archived

FROM `view_customer_locations`
LEFT JOIN `view_sip_user_agents` on `view_customer_locations`.customerid = `view_sip_user_agents`.customerid
LEFT JOIN `dids` AS permanent_dids ON `view_sip_user_agents`.didid = `permanent_dids`.id
LEFT JOIN `dids` AS temporary_dids ON `view_sip_user_agents`.temporary_didid = `temporary_dids`.id
Group by `view_customer_locations`.customerid

i still want all the rows from the view_customer_locations table.. but i am losing any entries in the view_customer_locations table that don't have a corresponding record in the view_sip_user_agents table. I also want the entries to be grouped by customerid .. so that each customer only has one entry in the resulting query.

If i remove the group by clause, i get all the entires from the view_customer_locations table but naturally i have multiple entries per customer which is not what i want.

please help

+2  A: 

Although MySQL does let you "get away" with expressing a GROUP BY clause with fields in the SELECT tha that might conceivable vary over the GROUP BY fields (theoretically picking an "arbitrary/random value"), the results of this ill-conceived, logically not well-founded operation are sometimes surprising, as you've noticed.

Try using correct SQL, e.g. with a MAX operator over the fields you're not "grouping by". If the implied assumption that those fields are strictly determined by the grouped-by fields is right, this can't possibly damage your results in any way, right? And yet sometimes you'll find that results do appear, or change (meaning the implied assumption was, simply, wrong).

In your case, since some of the fields might be uniformly NULL in a group, and MAX in that case is not necessarily well-defined, you might further try to use IFNULL there, of course.

Alex Martelli
I tried using MAX everywhere where I knew the result would be defined and then i used a combination of of max(ifnull) on the tables that could lead to null values and i still got the same result (not all my entries from view_customer_locations were present)CREATE VIEW `crm`.`view_search1` AS SELECTMAX(`view_customer_locations`.customerid),...MAX(`view_customer_locations`.customers_date_archived),MAX(IFNULL(`view_sip_user_agents`.sip_user_agentid, 0)),...GROUP_CONCAT( ..) as did,any ideas?
Blake McKeeby
@Blake, I'm a bit stumped -- what happens if you use a plain `SELECT` instead of a `CREATE VIEW`, still missing data? Just trying to simplify and see what's actually causing the problem -- if plain `SELECT` still does I'd try removing one of the `LEFT JOIN`ed tables, then a different one, by way of debugging the issue...
Alex Martelli
Alex,I really appreciate you working with me on this. I've been stumped for days. If I add the where clause "where view_customer_locations.customerid = " the customerid of the normally missing row, the row appears by its self in the results as expected. Any ideas?
Blake McKeeby
When i run it as just a select (not a create view), the row doesn't appear either. I've been trying all different queries to try to figure it out and I can't. It seems be removing the row because there is not view_sip_user_agents row that matches with it but i dont know why
Blake McKeeby
When i remove the last two left joins (permanent_dids and temporary dids) the problem still exists.
Blake McKeeby
when i remove the "group by" clause the problem goes away. The problem seems to be that "group by" removes the rows b/c it doesn't know how to group this row which didn't have a row to join to but I don't believe this is how it should handle it. I would like it to just display NULL for the columns in the joined table
Blake McKeeby
@Blake, you may be right (i.e., I agree: I don't think it should handle it this way either!), but if you're running into a MySQL bug (what version? any chance you can update it maybe?) I don't know how to work around it:-(.
Alex Martelli
Alex, I really appreciate your help. I just wrote a completely different (about 3x as long) query, which i was trying to avoid having to do and i got it to work and it worked as expected. I'm still baffled about why this other wouldn't work. Thanks again for your time and advice.
Blake McKeeby
A: 

I don't think GROUP BY is what you really want here. DISTINCT is more correct because it will eliminate duplicates but the results are defined on the non-grouped by fields

SELECT DISTINCT
`view_customer_locations`.customerid,
`view_customer_locations`.community_groupid,
`view_customer_locations`.community_group,


`view_sip_user_agents`.sip_user_agentid,
`view_sip_user_agents`.didid,
`view_sip_user_agents`.temporary_didid,
`view_sip_user_agents`.active_did,
GROUP_CONCAT( (IF(`view_sip_user_agents`.active_did = 'permanent', cast(`permanent_dids`.did  as char(10)), cast(`temporary_dids`.did as char(10)))) SEPARATOR ', ') as did,

`view_sip_user_agents`.sip_user_agents_date_archived

FROM `view_customer_locations`
LEFT JOIN `view_sip_user_agents` on `view_customer_locations`.customerid = `view_sip_user_agents`.customerid
LEFT JOIN `dids` AS permanent_dids ON `view_sip_user_agents`.didid = `permanent_dids`.id
LEFT JOIN `dids` AS temporary_dids ON `view_sip_user_agents`.temporary_didid = `temporary_dids`.id
Group by `view_customer_locations`.customerid
Rob Van Dam
I definitely don't want to use distinct b/c then i won't be able to use the Group_Concat function which is the whole purpose of this query.
Blake McKeeby