简体   繁体   English

SQL比较一列中的最小值与另一列中的最大日期

[英]SQL compare min value in one column with max date in different column

I would like to find all records in Column A where, after sorting Column C in ascending order, Column D begins with a value other than the earliest date. 我想在A列中找到所有记录,其中C列按升序排序后,D列的起始日期不是最早的日期。

From the below example, I would want it to return records for Ex2 and Ex3 and not return records for Ex1. 从下面的示例中,我希望它返回Ex2和Ex3的记录,而不返回Ex1的记录。

REVISED UPDATES/REQUIREMENTS: 修订的更新/要求:

1) I would like it grouped by Column A and ordered by Column C. 1)我想按A列分组并按C列排序。
2) I would like to find all records in Column A where the first value in Column D is not the lowest date. 2)我想在A列中找到所有记录,其中D列中的第一个值不是最低日期。

Column A  Column B  Column C  Column D
--------  --------  --------  --------
  Ex1     Title A      1      2003/1/1
  Ex1     Title B      2      2003/2/2

  Ex2     Title C      3      2004/4/4
  Ex2     Title D      4      2004/3/3

  Ex3     Title E      5      2005/6/6
  Ex3     Title F      6      2005/5/5

Any Ideas? 有任何想法吗?

You appear to assume an ordering by [Column B], which you can make explicit in a self join: 您似乎假设按[B列]进行排序,可以在自连接中使之明确:

SELECT t1.[Column A], t1.[Column B], t1.[Column C], t1.[Column D]
FROM YourTableName t1
JOIN YourTableName t2
ON t2.[Column A] = t1.[Column A]
AND t2.[Column B] > t1.[Column B]
WHERE t2.[Column C] > t1.[Column C]
AND t2.[Column D] < t1.[Column D]

This assumes there are exactly two rows where [Column A] = 'Ex1', etc. If you have more than two rows with the same value in [Column A] you will probably find the results to be unexpected. 假设[Column A] ='Ex1'等正好有两行。如果[Column A]中有多于两行的值相同,则可能会发现结果出乎意料。

Note that the first two comparisons are part of the join condition, and the third and fourth comparisons are part of the WHERE clause. 请注意,前两个比较是连接条件的一部分,而第三和第四个比较是WHERE子句的一部分。

UPDATE: 更新:

Working to changed requirements: There may be twenty rows with [Column A] = 'Ex1'. 努力改变需求:可能有二十行[Column A] ='Ex1'。 Return distinct values of [Column A] for which the lowest value of [Column C] is not in the same row as the lowest value of [Column D]. 返回[Column A]的最低值与[Column D]的最低值不在同一行的[Column A]的不同值。 [Column B] is not relevant. [B列]不相关。

SELECT DISTINCT t1.[Column A]
FROM YourTableName t1
JOIN 
(SELECT [Column A], MIN([Column C]) AS MinC, MIN([Column D]) AS MinD
 FROM YourTableName
 GROUP BY [Column A]) t2
ON t2.[Column A] = t1.[Column A]
WHERE t1.[Column C] = t2.MinC
AND t1.[Column D] <> t2.MinD

This returns: 返回:

Column A
Ex1

For the following test table: 对于以下测试表:

CREATE TABLE YourTableName
([Column A] VARCHAR(50),
[Column B] VARCHAR(50),
[Column C] INT,
[Column D] INT)

And test data: 并测试数据:

INSERT INTO YourTableName
([Column A], [Column B], [Column C], [Column D])
VALUES ('Ex1', 'Title A', 1, 2)

INSERT INTO YourTableName
([Column A], [Column B], [Column C], [Column D])
VALUES ('Ex1', 'Title B', 2, 1)

INSERT INTO YourTableName
([Column A], [Column B], [Column C], [Column D])
VALUES ('Ex1', 'Title C', 3, 1)

New answer specifically addressing the following specifications (which seem to comprise the problem now): 新的答案专门针对以下规范(现在似乎构成了问题):

UPDATE: I would like it grouped by Column A and ordered by Column C. 更新:我希望它按列A分组并按列C排序。

UPDATE #2: I want to return all examples from Column A where the "first" value in Column D is not the lowest value. 更新2:我想从A列返回所有示例,其中D列中的“第一个”值不是最低值。

SELECT a.*
FROM tableName a
WHERE a.columnD <> (
    SELECT min(columnD)
    FROM tableName b
    WHERE a.columnA=b.columnA
    )
ORDER BY a.columnC    

This technique is called a "self join." 此技术称为“自连接”。

SELECT a.*
FROM tableName a, tableName b
WHERE a.column_a = b.column_a
AND a.column_c < b.column_c
AND a.column_d > b.column_d

This assumes that the values in column_a are filled down and there aren't actually nulls as you showed in your example. 假定column_a中的值已填满,并且如示例中所示,实际上没有空值。

Using ranking functions, it can be done something like this: 使用排名功能,可以完成以下操作:

SELECT * FROM 
(
    SELECT 
        *
        , ROW_NUMBER() OVER (PARTITION BY ColumnA ORDER BY ColumnC) AS RankC
        , ROW_NUMBER() OVER (PARTITION BY ColumnA ORDER BY ColumnD) AS RankD
    FROM MyTable
) x
WHERE RankC <> RankD

SQLFiddle DEMO SQLFiddle演示

select * from table where C = 
(select MIN(D) from table) 
AND 
(select * from table where D = (select MAX(D) from table))

null means false else true null表示错误,否则为true

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM