Please for efficient query

Posted by user278618 on Stack Overflow See other posts from Stack Overflow or by user278618
Published on 2010-12-21T23:29:38Z Indexed on 2010/12/21 23:54 UTC
Read the original article Hit count: 343

I want to have efficient query to get some rows from my table.

Here is I think the best presentation of my table.

-Somedate is not duplicated - it is date of modifiedon
-a,b,c are parent ids, let say countryCode
-1,2,3,4 are subparent, let say citycode
-guids are id of rows
-true, false are values of rows  - one can name this column - freshAir


a 1 GUID somedate true
a 1 GUID somedate true
a 2 GUID somedate false
a 2 GUID somedate false
b 3 GUID somedate false
b 3 GUID somedate false
b 3 GUID somedate false
b 4 GUID somedate false
c 5 GUID somedate true
c 6 GUID somedate true
c 6 GUID somedate false
c 6 GUID somedate false
c 7 GUID somedate false

I want the most recent rows MAX(modifiedon) grouped by countrycode and citycode and in this groups I need elements which have another values (true, false).

And in result I want:

a 1 GUID somedate true
a 2 GUID somedate false
c 5 GUID somedate true
c 6 GUID somedate false
c 7 GUID somedate false

Look that in result I don't want to have records with "b", because all rows have the same value (false).

Best regards

© Stack Overflow or respective owner

Related posts about sql

Related posts about sql-server-2005