如何 SELECT 区分 SQL 中的另一列,按具有 MAX(列值)的行
How to SELECT DISTINCT another column in SQL, by rows with MAX(Column value)
如何Select 将另一个列引用与具有 MAX(Column) 值的行区分开来。
这是我的 table:
Category | Desc | Total1 | Total2 | MaxDate | Topic
--------------------------------------------------------
A | A Desc | 1 | 3 | 2017-04-01 | Topic1
A | A Desc | 1 | 1 | 2017-05-10 | Topic2
B | B Desc | 1 | 1 | 2017-03-25 | Topic3
我想在 select Topic
列中获取 MaxDate
列中的参考最大值。我希望 table 像:
Category | Desc | Total1 | Total2 | MaxDate | Topic
--------------------------------------------------------
A | A Desc | 2 | 4 | 2017-05-10 | Topic2
B | B Desc | 1 | 1 | 2017-03-25 | Topic3
然后 Total1
和 Total2
是来自 SUM 行的值具有不同的
根据你的数据我们也可以这样实现
Declare @Table1 TABLE
(Category varchar(1), Descp varchar(6), Total1 int, Total2 int, MaxDate datetime, Topic varchar(6))
;
INSERT INTO @Table1
(Category, Descp, Total1, Total2, MaxDate, Topic)
VALUES
('A', 'A Desc', 1, 3, '2017-04-01 00:00:00', 'Topic1'),
('A', 'A Desc', 1, 1, '2017-05-10 00:00:00', 'Topic2'),
('B', 'B Desc', 1, 1, '2017-03-25 00:00:00', 'Topic3')
;
脚本
Select TT.Category,
TT.Descp,
TT.Total1,
TT.Total2,
TT.MaxDate,
T.Topic
From @Table1 T
INNER JOIN (
select
Category,Descp,
SUM(Total1)Total1,
SUM(Total2)Total2,
Max(MaxDate)MaxDate
from @Table1
GROUP BY Category,Descp )TT
ON T.MaxDate = TT.MAXDATE
用户可以这样使用row_number and sum() over()
DECLARE @SampleData AS TABLE
(
Category varchar(10), [Desc] varchar(20), Total1 int, Total2 int, MaxDate datetime, Topic varchar(10)
)
INSERT INTO @SampleData VALUES
('A','A Desc', 1, 3, '2017-04-01', 'Topic1'),
('A','A Desc', 1, 1, '2017-05-10', 'Topic2'),
('B','B Desc', 1, 1, '2017-03-25', 'Topic3')
;WITH temp AS
(
SELECT sd.Category, sd.MaxDate, sd.Topic, sd.[Desc],
row_number() OVER(PARTITION BY sd.Category ORDER BY sd.MaxDate desc) AS Rn,
Sum(sd.Total1) OVER(PARTITION BY sd.Category ) AS Total1,
Sum(sd.Total2) OVER(PARTITION BY sd.Category ) AS Total2
FROM @SampleData sd
)
SELECT t.Category, t.[Desc], t.Total1,t.Total2, t.MaxDate, t.ToPic
FROM temp t
WHERE t.Rn = 1
演示 link:http://rextester.com/NQI25173
如何Select 将另一个列引用与具有 MAX(Column) 值的行区分开来。 这是我的 table:
Category | Desc | Total1 | Total2 | MaxDate | Topic
--------------------------------------------------------
A | A Desc | 1 | 3 | 2017-04-01 | Topic1
A | A Desc | 1 | 1 | 2017-05-10 | Topic2
B | B Desc | 1 | 1 | 2017-03-25 | Topic3
我想在 select Topic
列中获取 MaxDate
列中的参考最大值。我希望 table 像:
Category | Desc | Total1 | Total2 | MaxDate | Topic
--------------------------------------------------------
A | A Desc | 2 | 4 | 2017-05-10 | Topic2
B | B Desc | 1 | 1 | 2017-03-25 | Topic3
然后 Total1
和 Total2
是来自 SUM 行的值具有不同的
根据你的数据我们也可以这样实现
Declare @Table1 TABLE
(Category varchar(1), Descp varchar(6), Total1 int, Total2 int, MaxDate datetime, Topic varchar(6))
;
INSERT INTO @Table1
(Category, Descp, Total1, Total2, MaxDate, Topic)
VALUES
('A', 'A Desc', 1, 3, '2017-04-01 00:00:00', 'Topic1'),
('A', 'A Desc', 1, 1, '2017-05-10 00:00:00', 'Topic2'),
('B', 'B Desc', 1, 1, '2017-03-25 00:00:00', 'Topic3')
;
脚本
Select TT.Category,
TT.Descp,
TT.Total1,
TT.Total2,
TT.MaxDate,
T.Topic
From @Table1 T
INNER JOIN (
select
Category,Descp,
SUM(Total1)Total1,
SUM(Total2)Total2,
Max(MaxDate)MaxDate
from @Table1
GROUP BY Category,Descp )TT
ON T.MaxDate = TT.MAXDATE
用户可以这样使用row_number and sum() over()
DECLARE @SampleData AS TABLE
(
Category varchar(10), [Desc] varchar(20), Total1 int, Total2 int, MaxDate datetime, Topic varchar(10)
)
INSERT INTO @SampleData VALUES
('A','A Desc', 1, 3, '2017-04-01', 'Topic1'),
('A','A Desc', 1, 1, '2017-05-10', 'Topic2'),
('B','B Desc', 1, 1, '2017-03-25', 'Topic3')
;WITH temp AS
(
SELECT sd.Category, sd.MaxDate, sd.Topic, sd.[Desc],
row_number() OVER(PARTITION BY sd.Category ORDER BY sd.MaxDate desc) AS Rn,
Sum(sd.Total1) OVER(PARTITION BY sd.Category ) AS Total1,
Sum(sd.Total2) OVER(PARTITION BY sd.Category ) AS Total2
FROM @SampleData sd
)
SELECT t.Category, t.[Desc], t.Total1,t.Total2, t.MaxDate, t.ToPic
FROM temp t
WHERE t.Rn = 1
演示 link:http://rextester.com/NQI25173