Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>The final solution to this problem wasn't any easy to get at but after mashing our heads against a wall, me and a team mate got it down. Although every post from this topic helped us, none could really obtain the values we wanted, there were always flaws, but we are very thankful for all the help provided, without it we'd hardly have reached the answer by now.</p> <p>So the solution is as follows:</p> <pre><code>select P.ParentId RangeSheet ,coalesce(max(P.[End]), max(P.Start)) EventOn ,case when isnull(max(P.[End]), 0) = 0 then 1 else 0 end StartOrEnd from ( select RST.ParentId ,case RST.StartOrEnd when 1 then RST.EventOn else null end Start ,case RST.StartOrEnd when 0 then RST.EventOn else null end [End] from ( select coalesce(MAN.ParentId, AUT.ParentId) ParentId ,coalesce(MAN.StartOrEnd, AUT.StartOrEnd) StartOrEnd ,coalesce(max(MAN.CreatedOn), max(AUT.CreatedOn)) CreatedOn from ( -- Obter os manuais select RST.ParentId ,RST.StartOrEnd ,MAX(RST.CreatedOn) CreatedOn from RangeSheetTime RST where RST.[User] is not null group by RST.ParentId, RST.StartOrEnd ) MAN full outer join ( -- Obter os automáticos select RST.ParentId ,RST.StartOrEnd ,MAX(RST.CreatedOn) CreatedOn from RangeSheetTime RST where RST.[User] is null group by RST.ParentId, RST.StartOrEnd ) AUT on MAN.ParentId=AUT.ParentId and MAN.StartOrEnd=AUT.StartOrEnd group by coalesce(MAN.ParentId, AUT.ParentId), coalesce(MAN.StartOrEnd, AUT.StartOrEnd) ) FOJ inner join RangeSheetTime RST on FOJ.ParentId=RST.ParentId and FOJ.StartOrEnd=RST.StartOrEnd and FOJ.CreatedOn=RST.CreatedOn ) P group by P.ParentId </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload