lemmin Posted June 17, 2010 Share Posted June 17, 2010 Is there a way to merge data over NULL values? I have a result table that has two rows with the same id, but one has a value for a column that the other has a null value for (and the likewise for the other row). Something like this: ID Value1 Value2 1 hi NULL 1 NULL bye Is it possible to merge the similar IDs together instead of just dropping the first one (like GROUP BY does)? Thanks Quote Link to comment https://forums.phpfreaks.com/topic/205098-merging-columns/ Share on other sites More sharing options...
Mchl Posted June 17, 2010 Share Posted June 17, 2010 How does the query that produces this result look like? Quote Link to comment https://forums.phpfreaks.com/topic/205098-merging-columns/#findComment-1073612 Share on other sites More sharing options...
lemmin Posted June 17, 2010 Author Share Posted June 17, 2010 Ridiculous. SELECT ae.FilledPositions, ae.MaxPositions, ae.EventDate, ae.TotalEvents, a.Description, a.ActivityID, a.Title, CONCAT("images/", d.Image16) as Dot, IF( DATEDIFF(IF(ae.EventDate IS NULL, ae.PositionDate, ae.EventDate), NOW())=1, "<b>Tomorrow</b>", IF( DATEDIFF(IF(ae.EventDate IS NULL, ae.PositionDate, ae.EventDate), NOW())=0, "<b>Today!</b>", DATE_FORMAT(IF(ae.EventDate IS NULL, ae.PositionDate, ae.EventDate), "<b>%b %D</b> at <b>%l:%i %p</b>") ) ) as NextEvent FROM dots d, activities a LEFT JOIN ( SELECT COUNT(assoc.PositionID) as FilledPositions, (SELECT COALESCE(SUM(MaxPositions),0) FROM activity_event_positions WHERE EventID = e.EventID GROUP BY EventID) MaxPositions, aep.PositionDate, COALESCE(COUNT(e.EventID),0) TotalEvents, e.EventDate, e.ActivityID, e.EventID FROM activity_events e LEFT JOIN ( SELECT p.PositionID, p.PositionDate, p.EventID FROM activity_event_positions p ORDER BY p.PositionDate IS NULL, p.PositionDate ) aep ON aep.EventID = e.EventID LEFT JOIN activity_event_positions_assoc assoc ON assoc.PositionID = aep.PositionID WHERE (e.EventDate > NOW() OR e.EventDate IS NULL) GROUP BY e.EventID ORDER BY e.EventDate > aep.PositionDate, e.EventDate IS NULL, e.EventDate ) ae ON a.ActivityID = ae.ActivityID My goal is to return the next occurring of the two fields, EventDate and PositionDate. Quote Link to comment https://forums.phpfreaks.com/topic/205098-merging-columns/#findComment-1073615 Share on other sites More sharing options...
fenway Posted June 18, 2010 Share Posted June 18, 2010 Not a chance that anyone but you can read through that. Quote Link to comment https://forums.phpfreaks.com/topic/205098-merging-columns/#findComment-1073704 Share on other sites More sharing options...
Mchl Posted June 18, 2010 Share Posted June 18, 2010 You could try using GROUP_CONCAT() (look it up in MySQL manual) Quote Link to comment https://forums.phpfreaks.com/topic/205098-merging-columns/#findComment-1073780 Share on other sites More sharing options...
lemmin Posted June 21, 2010 Author Share Posted June 21, 2010 Oh come on, fenway. Have more confidence! Just kidding. I didn't post it originally because of that reason, but Mchl asked for it! I supposed I might be able to make GROUP_CONCAT() work, but it would return the values as a string, wouldn't it? I was hoping that, based on the result table I showed in my first post, someone would recognize a way to query for that differently. Maybe I can come up with a simple concept query that would be easier to look at. Thanks for the help. Quote Link to comment https://forums.phpfreaks.com/topic/205098-merging-columns/#findComment-1075166 Share on other sites More sharing options...
Mchl Posted June 21, 2010 Share Posted June 21, 2010 I supposed I might be able to make GROUP_CONCAT() work, but it would return the values as a string, wouldn't it? Yes it would, which might or might not be an issue depending on what you're going to do with this dataset. If it's fetched by PHP script, it doesn't really matter if these are strings or not. Quote Link to comment https://forums.phpfreaks.com/topic/205098-merging-columns/#findComment-1075167 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.