带条件的T-SQL OVER / PARTITION BY查询

时间:2018-08-30 08:59:21

标签: sql sql-server window-functions

我正在努力处理SQL查询,尽管查看了许多类似的答案,但都没有一个适合我的情况。我有一个数据集,如下所示:

Date1       Amount 1    Index   Date2               Type Supplier
31/03/2018  410000.00   17      16/04/2018 06:27    102  A
31/03/2018  410000.00   17      16/04/2018 06:31    102  B
31/03/2018  400000.00   2       16/04/2018 06:37    102  A
31/03/2018  400000.00   2       16/04/2018 06:38    102  B
30/06/2018  0           20      04/07/2018 08:23    202  A
30/06/2018  0           20      04/07/2018 08:23    202  B
30/06/2018  412000.00   20      06/07/2018 12:46    102  A
30/06/2018  412000.00   20      06/07/2018 12:47    102  B
30/06/2018  442000.00   100     16/07/2018 06:27    102  A
30/06/2018  442000.00   100     16/07/2018 06:31    102  B

对于每个存在多个具有相同类型的行的Date1,我只希望索引与最大Date2的索引匹配的行,所以我需要此输出:

Date1       Amount 1    Index   Date2               Type Supplier
31/03/2018  400000.00   2       16/04/2018 06:37    102  A
31/03/2018  400000.00   2       16/04/2018 06:38    102  B
30/06/2018  0           20      04/07/2018 08:23    202  A
30/06/2018  0           20      04/07/2018 08:23    202  B
30/06/2018  442000.00   100     16/07/2018 06:27    102  A
30/06/2018  442000.00   100     16/07/2018 06:31    102  B

我认为某种形式的条件MAX()OVER(PARTITION BY)应该可以实现,但是对于我一生来说,我不知道该怎么做。

6 个答案:

答案 0 :(得分:2)

LAST_VALUE (Transact-SQL)分析函数与子查询一起使用。

下面的工作示例适用于Oracle(我更喜欢Oracle,因为我总是在SQLServer上转换日期时遇到问题),但是查询的思想是相同的,语法也相同:

演示:http://www.sqlfiddle.com/#!4/004ce7/19

core-js

SELECT * FROM (
   SELECT t.* ,
     last_value( "INDEX" ) OVER 
        ( partition by date1, "TYPE" order by date2
          ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING) xx
   FROM table1 t
) x
WHERE xx = "INDEX"
ORDER BY date1;

答案 1 :(得分:1)

我想这就是你想要的。请注意,使用ROW_NUMBER而不是RANK-RANK将产生多个值1,其中Date2是相同的,ROW_NUMBER将产生唯一的增量值rn

SELECT
    [Date1],
    [Amount 1],
    [Index],
    [Date2],
    [Type],
    [Supplier]
FROM my_table
INNER JOIN (
    SELECT
        [Index],
        [Type],
        ROW_NUMBER() OVER (PARTITION BY [Date1], [Type] ORDER BY [Date2] DESC) AS rn
    FROM my_table
) AS subquery
    ON subquery.rn = 1
    AND subquery.[Index] = my_table.[Index]
    AND subquery.[Type] = my_table.[Type]

答案 2 :(得分:1)

使用row_number()

SELECT [Date1],[Amount 1],[Index],[Date2],[Type],[Supplier]
FROM (
    SELECT *, ROW_NUMBER() OVER (PARTITION BY [Date1] ORDER BY [Date2] DESC) AS rn
    FROM tablename
) a
WHERE a.rn in( 1,2)

答案 3 :(得分:1)

尝试

;WITH CTE
AS
(
    SELECT
        *,
        MxDt =ROW_NUMBER() OVER(PARTITION BY Date1,[Type] ORDER BY Date2 DESC)
        FROM YourTableName
)
SELECT
    *
    FROM CTE C1
        WHERE EXISTS
        (
            SELECT
                1
                FROM CTE C2
                    WHERE [Index] = C1.[Index]
                        AND [Type]= C1.[Type]
                        AND C2.MxDt =1
        )

答案 4 :(得分:0)

子查询对数据进行排序,以使控制索引的行对于日期和类型的每种分组始终为row_no。外部查询返回具有相同date1,type,index组合的所有行,但忽略其他行

Select *
From Data D
    inner join SortedData S on S.Date1 = A.Date1 
          and S.Type = A.Type 
          and S.RowNo = 1 
          and D.Index = A.index

(Select Date1, Type, Index, row_Number() over (Partition by Date1, Type ORDER BY Date2 DESC) as RowNo From Data) SortedData

答案 5 :(得分:0)

您可以使用相关子查询:

select t.*
from table t
where Index = (select top (1) t1.Index
               from table t1
               where t1.Date1 = t.Date1 and t1.type = t.type
               order by t1.date2 desc
              );