HI all, I'm having some difficulty understanding the rationale behind group by aggregation in sql server 2005.
I have the following query which works fine and returns one row for each contact.id and the 1st occurence of event
SELECT
contact.id
,MIN(eve.date_created)
FROM _contact contact WITH(nolock)
INNER JOIN table2 tb2 WITH (nolock) ON contact.id = tb2.id1
INNER JOIN _event eve WITH (nolock) ON tb2.id2 = eve.id
INNER JOIN _cashtable cash WITH (nolock) ON cash.contact_id = contact.id
GROUP BY contact.id
However what I'm looking for is to have the following query and still only pull out one row per contact.id
SELECT
contact.id
,MIN(eve.date_created)
,cash.id2 -- the cash linked to the first event
,eve.id -- the first event linked to the contact
FROM _contact contact with (nolock)
INNER JOIN _table2 tb2 WITH (nolock) ON contact.id = tb2.id1
INNER JOIN _event eve WITH (nolock) ON tb2.id2 = eve.id
INNER JOIN _cashtable cash WITH (nolock) ON cash.contact_id = contact.id
GROUP BY contact.id
I get the standard error message saying that I need to add in cash.id2 and eve.id into the group by clause, which returns results I don't want.
I know that there are the potential options of using rank() / partitioning or even including the
select(MIN(eve.date_created)
in the FROM clause, but I am not sure which would be best to put this and am still quite confused as to why SQL requires everything to be included in the group by statement and so any advice would be great :)
Thanks in advance!