迭代列以查找具有最后非零值的列

时间:2012-10-16 06:28:34

标签: java excel jdbc

我有如下表table_sheet,

-----------------------------------------------
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

Here, WeekendDate is date on 'Sun'(last column)

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

Pid  LastValueDate
11   11.10.2012
12   02.10.2012

11 -> 11.10.2012 was Thr(value 1)
12 -> 02.10.2012 was Tue(value 2)

由于我的数据在Excel中,我正在使用JDBC-ODBC连接。我希望我们可以通过SQL实现这一目标。

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

SELECT pid
      ,max(WeekendDate) - (6-last_effort_day) AS end_date
 FROM effort_sheet
GROUP BY pid

请帮忙。

5 个答案:

答案 0 :(得分: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 

答案 1 :(得分: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一起使用。

答案 2 :(得分: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

答案 3 :(得分: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 link

答案 4 :(得分: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