0

我正在寻找对每一行的查询以查找具有最高/最近日期的列 (YYY.),并希望找到相应的列 (XXXX.) 查找具有最近日期的列是可能的,但是得到相应的专栏让我一无所知...欢迎所有建议!

所以从表中:

| id        |       XXXX0|      YYY0 |       XXXX1|      YYY1|      XXXX9|        YYY9|
---------------------------------------------------------------------------------------
|         A |          3 | 10-10-2009|          4 |10-10-2010|         1 |  10-10-2011| 
|         B |          2 | 10-10-2010|          3 |10-10-2012|         6 |  10-10-2011| 
|         C |          4 | 10-10-2011|          1 |10-10-2010|         7 |  10-10-2012| 
|         D |          1 | 10-10-2010|          8 |10-10-2013|         9 |  10-10-2012| 

我想结束:

| id        |      LabelX|     LabelY|
--------------------------------------
|         A |          1 | 10-10-2011|
|         B |          3 | 10-10-2012|
|         C |          7 | 10-10-2012|
|         D |          8 | 10-10-2013|

补充:这是我试图确定的最大值:

SELECT LTRIM(A) AS A, LTRIM(B) AS B, LTRIM(C) 
    (Select Max(v)
    FROM (VALUES (YYY0), (YYY1), …..(YYY9) AS value(v)) as [MaxDate]
FROM Table
4

3 回答 3

0
SELECT id,
CASE
WHEN YYYY0 > YYY1 AND YYY0 > YYY2 ... AND YYY0 > YYY9 THEN XXX0
WHEN YYY1 > YYY2 ... AND YYY0 > YYY9 THEN XXX1
...
ELSE XXX9 AS LabelX,
CASE
WHEN YYYY0 > YYY1 AND YYY0 > YYY2 ... AND YYY0 > YYY9 THEN YYY0
WHEN YYY1 > YYY2 ... AND YYY0 > YYY9 THEN YYY1
...
ELSE YYY9 AS LabelY,
...

并用 >= 替换 > ,这取决于如果它们相等,您想赢取哪一个。

于 2013-06-14T17:26:54.793 回答
0

如果它是 SQL Server 2005 及更高版本,您可以这样做(它假定特定 id 的每一列中的日期都是唯一的):

;with cte as (
    select id, xxxx0 as LabelX, yyy0 as LabelY from tab union all
    select id, xxxx1, yyy1 from tab union all
    select id, xxxx9, yyy9 from tab
)
select t.id, x.LabelX, t.LabelY from (
    select t1.id,  max(t1.LabelY) as LabelY 
    from cte t1
    group by t1.id
) t
join cte x on t.id = x.id and t.LabelY = x.LabelY

Live SQL Fiddle 示例

于 2013-06-14T18:18:09.910 回答
0

这是为您提供的简化示例。我使用的是 SQL Server 2008,但是这个 SQL 是非常标准的,应该可以在大多数现代实现上正常工作(著名的遗言)。

所以,给定这个表模式:

drop table dbo.foobar
go
create table dbo.foobar
(
  id    char(1) not null primary key ,
  X1 int not null , Y1 date not null ,
  X2 int not null , Y2 date not null ,
  X3 int not null , Y3 date not null ,
)
go

以及一些示例数据:

insert dbo.foobar values ( 'A' , 1 , '1 Jan 2013' , 2 , '1 Feb 2013' , 3 , '1 Mar 2013' )
insert dbo.foobar values ( 'B' , 1 , '1 Mar 2013' , 2 , '1 Jan 2013' , 3 , '1 Feb 2013' )
insert dbo.foobar values ( 'C' , 1 , '1 Feb 2013' , 2 , '1 Mar 2013' , 3 , '1 Jan 2013' )
go

根据数据的性质以及查询和结果的所需语义,这种方法:

--
-- This approach pushes evaluation of the corresponding X to the output column list
--
-- 1. Construct a UNION ALL to normalize the table into a set of id/date pairs
-- 2. Compute max(date) for each id
-- 3. Join back against the original table to recover the source row
-- 4. Use the max(date) value to identify the corresponding X 
--
select t.id ,
       MaxY = t.y ,
       X    = case
              when t.Y = x.Y1 then x.X1
              when t.Y = x.Y2 then x.X2
              when t.Y = x.Y3 then x.X3
              end
from ( select x.id ,
              y = max( x.y )
       from (           select id , y=y1 from dbo.foobar
              union all select id , y=y2 from dbo.foobar
              union all select id , y=y3 from dbo.foobar
            ) x
        group by x.id
      ) t
join dbo.foobar x on x.id = t.id
order by 1,2,3
go

或者这种方法

--
-- This approach looks at each X/Y pair as its own "table" as it were
--
select t.id       ,
       MaxY = t.y ,
       X    = coalesce( t1.X1 , t2.X2 , t3.X3 )
from ( select x.id ,
              y = max( x.y )
       from (           select id , y=Y1 from dbo.foobar
              union all select id , y=Y2 from dbo.foobar
              union all select id , y=Y3 from dbo.foobar
            ) x
        group by x.id
     ) t
left join dbo.foobar t1 on t1.id = t.id and t1.y1 = t.Y
left join dbo.foobar t2 on t2.id = t.id and t2.y2 = t.Y
left join dbo.foobar t3 on t3.id = t.id and t3.y3 = t.Y
order by 1,2,3

应该为你工作。无论哪种情况,两个查询都会产生相同的结果集:

id MaxY       X
-- ---------- -
A  2013-03-01 3
B  2013-03-01 1
C  2013-03-01 2

祝你好运!

[您是否考虑过规范您的数据库设计?第三范式使生活更轻松,通常更有效率。]

于 2013-06-14T18:45:23.313 回答