$SQL = "SELECT ShiftEntry.UID, COUNT(ShiftEntry.UID) AS NR, SUM(Shifts.Len) as LEN ".
"FROM `ShiftEntry` ".
"LEFT JOIN `Shifts` ON ShiftEntry.SID=Shifts.SID ".
"WHERE NOT UID=0 ".
"GROUP BY UID ".
"ORDER BY LEN DESC, NR DESC, UID ";
$SQL = "
SELECT d.UID, d.nr, d.len, d.lenWeight, f.nr AS nrFree, f.len AS lenFree, d.lenWeight - COALESCE(f.len, 0) as lenReal
FROM
(SELECT e.UID, COUNT(s.Len) as nr, SUM(s.Len) as len, SUM( s.Len*(1+(((HOUR(s.DateS)+2)%24)<10and((HOUR(s.DateE)+2)%24)<10)))aslenWeightFROM`Shifts`ASsINNERJOIN`ShiftEntry`ASeUSING(SID)WHERENOTUID=0GROUPBYUID)asd
LEFT JOIN
(SELECT UID, COUNT(Length) AS nr, SUM(Length) AS len FROM `ShiftFreeloader` GROUP BY UID) AS f