如何比较来自同一列和不同行但相同 table 的值?
How do I compare values from same column and different row but same table?
我正在尝试获取特定帐户名称的开始日期不同的记录。我有以下示例数据。
示例数据脚本如下
create table Meter (AccountNumer varchar(50), MeterNumber varchar(50), StartDate date)
Insert into Meter Values('0142628117','123470203','4/22/2020')
Insert into Meter Values('0142628117','123470205','4/22/2020')
Insert into Meter Values('0160059948','100094717','4/24/2020')
Insert into Meter Values('0160059948','328144931','4/24/2020')
Insert into Meter Values('0500000178767001363445','TCA105238304','10/2/2018')
Insert into Meter Values('0500000178767001363445','TCA130359929','8/12/2019')
Insert into Meter Values('0500071816677001432356','1ND386803501','8/20/2019')
Insert into Meter Values('0500071816677001432356','99D024666064','7/18/2019')
RowNo AccountNumber MeterNumber StartDate
1 0500000178767001363445 TCA105238304 10/2/2018
2 0500000178767001363445 TCA130359929 8/12/2019
3 0160059948 100094717 4/24/2020
4 0160059948 328144931 4/24/2020
5 0500071816677001432356 1ND386803501 8/20/2019
6 0500071816677001432356 99D024666064 7/18/2019
7 0142628117 123470203 4/22/2020
8 0142628117 123470205 4/22/2020
我试图只获取帐号开始日期不同的记录(两行)。所以我期望的结果如下 table.
预期结果
RowNo AccountNumber MeterNumber StartDate
1 0500000178767001363445 TCA105238304 10/2/2018
2 0500000178767001363445 TCA130359929 8/12/2019
5 0500071816677001432356 1ND386803501 8/20/2019
6 0500071816677001432356 99D024666064 7/18/2019
任何帮助将不胜感激。
您可以为此使用 window 函数。
我们只是按 AccountNumber
对结果进行分区,并检查每个分区的最大值和最小值 StartDate
是否不同。
SELECT *
FROM (
SELECT *,
MinDate = MIN(m.StartDate) OVER (PARTITION BY m.AccountNumber),
MaxDate = MAX(m.StartDate) OVER (PARTITION BY m.AccountNumber)
FROM Meter m
) m
WHERE MinDate <> MaxDate;
你可以试试下面的查询
select ROW_NUMBER() OVER(ORDER BY m.AccountNumer)RowNo , m.AccountNumer,m.MeterNumber,m.StartDate
from meter m
INNER JOIN meter n ON m.AccountNumer=n.AccountNumer
and m.StartDate <> n.StartDate
如果您遇到任何问题或需要进行任何更改,请告诉我。
我正在尝试获取特定帐户名称的开始日期不同的记录。我有以下示例数据。
示例数据脚本如下
create table Meter (AccountNumer varchar(50), MeterNumber varchar(50), StartDate date)
Insert into Meter Values('0142628117','123470203','4/22/2020')
Insert into Meter Values('0142628117','123470205','4/22/2020')
Insert into Meter Values('0160059948','100094717','4/24/2020')
Insert into Meter Values('0160059948','328144931','4/24/2020')
Insert into Meter Values('0500000178767001363445','TCA105238304','10/2/2018')
Insert into Meter Values('0500000178767001363445','TCA130359929','8/12/2019')
Insert into Meter Values('0500071816677001432356','1ND386803501','8/20/2019')
Insert into Meter Values('0500071816677001432356','99D024666064','7/18/2019')
RowNo AccountNumber MeterNumber StartDate
1 0500000178767001363445 TCA105238304 10/2/2018
2 0500000178767001363445 TCA130359929 8/12/2019
3 0160059948 100094717 4/24/2020
4 0160059948 328144931 4/24/2020
5 0500071816677001432356 1ND386803501 8/20/2019
6 0500071816677001432356 99D024666064 7/18/2019
7 0142628117 123470203 4/22/2020
8 0142628117 123470205 4/22/2020
我试图只获取帐号开始日期不同的记录(两行)。所以我期望的结果如下 table.
预期结果
RowNo AccountNumber MeterNumber StartDate
1 0500000178767001363445 TCA105238304 10/2/2018
2 0500000178767001363445 TCA130359929 8/12/2019
5 0500071816677001432356 1ND386803501 8/20/2019
6 0500071816677001432356 99D024666064 7/18/2019
任何帮助将不胜感激。
您可以为此使用 window 函数。
我们只是按 AccountNumber
对结果进行分区,并检查每个分区的最大值和最小值 StartDate
是否不同。
SELECT *
FROM (
SELECT *,
MinDate = MIN(m.StartDate) OVER (PARTITION BY m.AccountNumber),
MaxDate = MAX(m.StartDate) OVER (PARTITION BY m.AccountNumber)
FROM Meter m
) m
WHERE MinDate <> MaxDate;
你可以试试下面的查询
select ROW_NUMBER() OVER(ORDER BY m.AccountNumer)RowNo , m.AccountNumer,m.MeterNumber,m.StartDate
from meter m
INNER JOIN meter n ON m.AccountNumer=n.AccountNumer
and m.StartDate <> n.StartDate
如果您遇到任何问题或需要进行任何更改,请告诉我。