Group BY MySQL- what column to use? -


hi have following result set. need find min(diff) each id(ex.2904 min 36).if use group id not showing 36 min

# id, id_contact,name,optention_date,send_date, diff 2904, 28,version 2, 2014-11-05, 2014-12-11 16:45:41, 36 2904, 28, version 1, 2014-09-01, 2014-12-11 16:45:41, 101 2903, 178,version 2, 2014-11-05, 2014-12-09 16:06:39, 34 2903, 178,version 1, 2014-09-01, 2014-12-09 16:06:39, 99 

the query have

select    email.id,   a_email_contact.id_contact,   email.subject,   x.name,   x.optention_date,   email.send_date,   min(datediff(email.send_date, x.optention_date)) diff   classification_element y,   classification_version x ,   email,a_email_contact   x.id_project=y.id_project   , y.id_project=11   , y.id_company=3   , y.id_version=x.id   , email.send_date>x.optention_date   , a_email_contact.id_email=email.id group email.id order diff asc 

on column should use group by?

the result of query

# id, id_contact, subject, name, optention_date, send_date, diff 2904, 28,version 1, 2014-09-01, 2014-12-11 16:45:41, 36 2903, 178,version 1, 2014-09-01, 2014-12-09 16:06:39, 34 2902, 168,version 1, 2014-09-01, 2014-10-16 10:22:42, 45 

which wrong because #2904 should have version 2 36 diff shows version1 instead.

based on volume of records working with, may not optimal, should work -

select id, min(diff) ( select    email.id,   a_email_contact.id_contact,   email.subject,   x.name,   x.optention_date,   email.send_date,   datediff(email.send_date, x.optention_date) diff   classification_element y,   classification_version x ,   email,a_email_contact   x.id_project=y.id_project   , y.id_project=11   , y.id_company=3   , y.id_version=x.id   , email.send_date>x.optention_date   , a_email_contact.id_email=email.id order diff asc ) tmp group id 

Comments

Popular posts from this blog

qt - Using float or double for own QML classes -

Create Outlook appointment via C# .Net -

ios - Swift Array Resetting Itself -