Ok, let's see if I can explain this. The query below returns the result set below:
Sorry about the formatting:
group id title completed_dt viewed_dt
GROUP1 1000 BLUE 2010-09-01 2010-09-02
NULL 1003 title1 NULL NULL
GROUP1 1002 BLUEST NULL NULL
NULL 1006 title4 NULL NULL
GROUP1 1001 BLUER NULL NULL
Basically, what I want is, where "group" is NOT null, e.g. "GROUP1", then return the row from the group with the MIN(id) where completed_dt is null. I DO want rows where completed_dt is not null in all cases. So that wouldn't be part of what I'm trying to eliminate.
So the result set I want is actually:
group id title completed_dt viewed_dt
GROUP1 1000 BLUE 2010-09-01 2010-09-02
NULL 1003 title1 NULL NULL
NULL 1006 title4 NULL NULL
GROUP1 1001 BLUER NULL NULL
That is, I don't want "BLUEST". I only want BLUE and BLUER (BLUE because completed_dt is not null, and BLUER because it's the min (id) where completed_dt is null from GROUP1.
The problem is, I don't know how to write this query. And all rows without completed_dt must be randomized.
Any suggestions greatly appreciated!!
Thanks
Code:
select
a.group,
a.id,
a.title,
c.completed_dt,
c.viewed_dt
from myTableA a
left join myTableC c on (a.id = c.id)
and c.id = 123
order by c.completed_dt desc, newid() -- randomizes
Sorry about the formatting:
group id title completed_dt viewed_dt
GROUP1 1000 BLUE 2010-09-01 2010-09-02
NULL 1003 title1 NULL NULL
GROUP1 1002 BLUEST NULL NULL
NULL 1006 title4 NULL NULL
GROUP1 1001 BLUER NULL NULL
Basically, what I want is, where "group" is NOT null, e.g. "GROUP1", then return the row from the group with the MIN(id) where completed_dt is null. I DO want rows where completed_dt is not null in all cases. So that wouldn't be part of what I'm trying to eliminate.
So the result set I want is actually:
group id title completed_dt viewed_dt
GROUP1 1000 BLUE 2010-09-01 2010-09-02
NULL 1003 title1 NULL NULL
NULL 1006 title4 NULL NULL
GROUP1 1001 BLUER NULL NULL
That is, I don't want "BLUEST". I only want BLUE and BLUER (BLUE because completed_dt is not null, and BLUER because it's the min (id) where completed_dt is null from GROUP1.
The problem is, I don't know how to write this query. And all rows without completed_dt must be randomized.
Any suggestions greatly appreciated!!
Thanks