1

我有如下表的努力表,

-----------------------------------------------------------
Pid WeekendDate Mon Tue Wed Thr Fri Sat Sun
----------------------------------------------
11 14.10.2012 4 4 5 1 0 0 0
11 07.10.2012 2 2 1 5 3 0 0
12 07.10.2012 2 2 0 0 0 0 0

在这里,WeekendDate 是“太阳”的日期(最后一列)

我需要在记录最后一个非零值时找到每个 Pid 的日期。所以我的结果应该是

Pid LastValueDate
11 11.10.2012
12 02.10.2012

11 -> 11.10.2012 是 Thr(值 1)
12 -> 02.10.2012 是星期二(值 2)

由于我的数据在 Excel 中,并且我正在使用 JDBC-ODBC 连接。我希望我们可以仅使用 SQL 来实现这一点。

我不完整的解决方案(需要找到last_effort_day):

选择 pid
      ,max(WeekendDate) - (6- last_effort_day ) AS end_date
 来自努力表
按 pid 分组

请帮忙。

4

5 回答 5

1
SELECT PID, 
       Dateadd(DAY, -1 * ( CASE 
                             WHEN SUN != 0 THEN 0 
                             WHEN SAT != 0 THEN 1 
                             WHEN FRI != 0 THEN 2 
                             WHEN THR != 0 THEN 3 
                             WHEN WED != 0 THEN 4 
                             WHEN TUE != 0 THEN 5 
                             ELSE 6 
                           END ), WEEKENDDATE) AS end_date 
FROM   EFFORT_SHEET 
GROUP  BY PID 
于 2012-10-16T07:18:47.057 回答
1

我将开始使用这个联合查询,它更容易操作:

 Select Pid, WeekendDate-6 As WeekDate, Mon As Num
 From effort_sheet
 Union
 Select Pid, WeekendDate-5 As WeekDate, Tue As Num
 From effort_sheet
 Union
 Select Pid, WeekendDate-4 As WeekDate, Wed As Num
 From effort_sheet

以此类推,一周中的每一天。然后您可以按查询使用组:

 Select Pid, Max(WeekDate)
 From (the union select above)
 Where Num>0
 Group By Pid

这是标准 SQL,应该适用于任何 DBMS。

于 2012-10-16T07:21:17.123 回答
1
SELECT Pid, MAX(WeekendDate) - (8 - MIN(CHARINDEX('0', CAST(Mon AS nvarchar(10)
                                                       + CAST(Tue AS nvarchar(10))
                                                       + CAST(Wed AS nvarchar(10))
                                                       + CAST(Thr AS nvarchar(10))
                                                       + CAST(Fri AS nvarchar(10))
                                                       + CAST(Sat AS nvarchar(10))
                                                       + CAST(Sun AS nvarchar(10))))) AS last_effort_day
FROM effort_sheet
GROUP BY pid
于 2012-10-16T07:39:24.327 回答
1
Try this 
select es.p_id,es.weekenddate-( CASE 
                             WHEN SUN != 0 THEN 0 
                             WHEN SAT != 0 THEN 1 
                             WHEN FRI != 0 THEN 2 
                             WHEN THR != 0 THEN 3 
                             WHEN WED != 0 THEN 4 
                             WHEN TUE != 0 THEN 5 
                             ELSE 6 
                           END )  from(
select p_id,max(weekenddate)dt from effort_sheet group by p_id
  )inner,effort_sheet es
where inner.p_id =es.p_id
AND inner.dt=es.weekenddate;

sqlfiddle 链接

于 2012-10-16T10:42:41.203 回答
0

试试这个:

select pid, max(case
when mon = 0 then weekenddate - 7
when mon != 0 and tue = 0 then weekenddate - 6
when tue != 0 and wed = 0 then weekenddate - 5
when wed != 0 and thr = 0 then weekenddate - 4
when thr != 0 and fri = 0 then weekenddate - 3
when fri != 0 and sat = 0 then weekenddate - 2
when sat != 0 and sun = 0 then weekenddate -1 end)
from effort_sheet
group by pid
于 2012-10-16T07:22:38.747 回答