Sql - 如何根据 mssql 中的列生成唯一记录
Sql - How to generate unique record based on column from mssql
我有一个 mssql table,格式如下:
ID |Code |Name |Date
1 |01 |A |2011-01-01 00:01:23
2 |02 |B |2011-07-01 00:01:23
3 |01 |A |2011-05-01 00:01:23
4 |01 |A |2011-07-01 00:01:23
5 |03 |C |2011-02-01 00:01:23
6 |04 |D |2011-01-01 00:01:23
7 |03 |C |2011-01-01 00:01:23
8 |02 |B |2011-01-01 00:01:23
I need to select unique code with with latest date as follows:
ID |Code |Name |Date
1 |01 |A |2011-07-01 00:01:23
2 |02 |B |2011-07-01 00:01:23
3 |03 |C |2011-02-01 00:01:23
4 |04 |D |2011-01-01 00:01:23
如何编写 sql 查询来实现此目的?
很简单就是用了Group by Clause..
select ID,Code,Name,MAX(Date) as Date from TableName Group by Id,Code,Name
另一种方式是 CTE 函数
With CTE AS
(
SELECT row_number() over(partition by Code order by Code) rn
ID,Code,Name,MAX(Date) as Date
FROM TableName
)
select ID,Code,Name,Date from CTE where rn = 1
使用 Group BY
并获取 MAX
日期。
SELECT ROW_NUMBER() OVER (ORDER BY Code), Code, Name, MAX(Date)
FROM Table
GROUP BY Code, Name
请将 table 名称放在 "YourTable" 的位置,然后尝试下面的查询。
SELECT ID,CODE,NAME,MAX(DATE)
FROM YourTable
GROUP BY ID,CODE,NAME
ORDER BY CODE,NAME DESC
使用相关子查询:
select t.*
from t
where t.date = (select max(t2.date) from t t2 where t2.code = t.code);
与NOT EXISTS
:
select t.* from tablename t
where not exists (
select 1 from tablename
where code = t.code abd date > t.date
);
或 ROW_NUMBER()
:
select t.id, t.code, t.name, t.date
from (
select *, row_number() over (partition by code order by date desc) rn
from tablename
) t
where t.rn = 1;
要保持相同的 ID:
SELECT Id, Code, Name, [Date]
FROM
(
SELECT *
, ROW_NUMBER() OVER (PARTITION BY Code, Name ORDER BY [Date] DESC, Id) AS Rn
FROM yourtable
) q
WHERE Rn = 1
ORDER BY Id;
获取新 ID:
SELECT
ROW_NUMBER() OVER (ORDER BY MAX([Date]), Code, Name) AS Id,
Code,
Name,
MAX([Date]) AS [Date]
FROM yourtable
GROUP BY Code, Name;
试试这个:
SELECT tbl1.* FROM tbl1 WHERE tbl1.date = (SELECT max(tbl2.date) FROM tbl1 tbl2 WHERE tbl2.code = tbl1.code);
我有一个 mssql table,格式如下:
ID |Code |Name |Date 1 |01 |A |2011-01-01 00:01:23 2 |02 |B |2011-07-01 00:01:23 3 |01 |A |2011-05-01 00:01:23 4 |01 |A |2011-07-01 00:01:23 5 |03 |C |2011-02-01 00:01:23 6 |04 |D |2011-01-01 00:01:23 7 |03 |C |2011-01-01 00:01:23 8 |02 |B |2011-01-01 00:01:23 I need to select unique code with with latest date as follows: ID |Code |Name |Date 1 |01 |A |2011-07-01 00:01:23 2 |02 |B |2011-07-01 00:01:23 3 |03 |C |2011-02-01 00:01:23 4 |04 |D |2011-01-01 00:01:23
如何编写 sql 查询来实现此目的?
很简单就是用了Group by Clause..
select ID,Code,Name,MAX(Date) as Date from TableName Group by Id,Code,Name
另一种方式是 CTE 函数
With CTE AS
(
SELECT row_number() over(partition by Code order by Code) rn
ID,Code,Name,MAX(Date) as Date
FROM TableName
)
select ID,Code,Name,Date from CTE where rn = 1
使用 Group BY
并获取 MAX
日期。
SELECT ROW_NUMBER() OVER (ORDER BY Code), Code, Name, MAX(Date)
FROM Table
GROUP BY Code, Name
请将 table 名称放在 "YourTable" 的位置,然后尝试下面的查询。
SELECT ID,CODE,NAME,MAX(DATE)
FROM YourTable
GROUP BY ID,CODE,NAME
ORDER BY CODE,NAME DESC
使用相关子查询:
select t.*
from t
where t.date = (select max(t2.date) from t t2 where t2.code = t.code);
与NOT EXISTS
:
select t.* from tablename t
where not exists (
select 1 from tablename
where code = t.code abd date > t.date
);
或 ROW_NUMBER()
:
select t.id, t.code, t.name, t.date
from (
select *, row_number() over (partition by code order by date desc) rn
from tablename
) t
where t.rn = 1;
要保持相同的 ID:
SELECT Id, Code, Name, [Date]
FROM
(
SELECT *
, ROW_NUMBER() OVER (PARTITION BY Code, Name ORDER BY [Date] DESC, Id) AS Rn
FROM yourtable
) q
WHERE Rn = 1
ORDER BY Id;
获取新 ID:
SELECT
ROW_NUMBER() OVER (ORDER BY MAX([Date]), Code, Name) AS Id,
Code,
Name,
MAX([Date]) AS [Date]
FROM yourtable
GROUP BY Code, Name;
试试这个:
SELECT tbl1.* FROM tbl1 WHERE tbl1.date = (SELECT max(tbl2.date) FROM tbl1 tbl2 WHERE tbl2.code = tbl1.code);