-1

我有以下

(SELECT 
    ts.PID, 
    tt.ID, 
    tt.ItemOrder,
    tt.DisplayVal as Task, 
    tt.Responsible as  ResParty, 
    tt.DaysDue, 
    DATEADD(day, tt.DaysDue, PgStartDate) as DueDate,
    ts.spate As ActualDate , 
    ps.PgStartDate as ProgramDate
FROM tblTimeline tt 
CROSS JOIN tblSers ts
JOIN PG ps ON ps.PID = ts.PId
WHERE tt.ID = 2 
AND ps.cancelled = 0 ) 

t1
INNER JOIN(

SELECT 
    ts.PID,  
    max(DATEADD(day, tt.DaysDue, PgStartDate)) as DueDate 
FROM tblTimeline tt 
CROSS JOIN tblSers ts
JOIN PG ps on ps.PID = ts.PId
WHERE tt.ID = 2 
AND ps.cancelled = 0) 

t2
  ON t1.PID = t2.PID 
  AND t1.DueDate = t2.DueDate

t1中,我所拥有的是PID, DueDate给定行的重复项。

我喜欢为每个获得完整的行PID,我喜欢显示具有 Max 的行DueDate

我在 t1, t2 附近得到不正确的语法

4

2 回答 2

1

您似乎缺少GROUP BY初始SELECT陈述的开头和开头:

SELECT *  --missing this
FROM      --missing this
(
  select ts.PID, 
     tt.ID, 
     tt.ItemOrder,
     tt.DisplayVal as Task,
     tt.Responsible as  ResParty, 
     tt.DaysDue, 
     DATEADD(day, tt.DaysDue, PgStartDate) as DueDate,
     ts.spate As ActualDate , 
     ps.PgStartDate as ProgramDate
  from tblTimeline tt 
  cross join tblSers ts
  join PG ps on ps.PID = ts.PId
  where tt.ID = 2 and ps.cancelled = 0 
) t1
inner join 
(
  select ts.PID,  
    max(DATEADD(day, tt.DaysDue, PgStartDate)) as DueDate 
  from tblTimeline tt cross join tblSers ts
  join PG ps 
    on ps.PID = ts.PId
  where tt.ID = 2 and ps.cancelled = 0 
  GROUP BY ts.PID   --missing this
) t2
  on t1.PID = t2.PID 
  and t1.DueDate = t2.DueDate
于 2012-08-10T01:39:50.867 回答
0

您缺少分组依据。

select ts.PID,  max(DATEADD(day, tt.DaysDue, PgStartDate)) as DueDate 
   from tblTimeline tt cross join tblSers ts
   join PG ps on ps.PID = ts.PId
   where tt.ID = 2 and ps.cancelled = 0
   group by ts.PID
于 2012-08-10T01:32:11.090 回答