[英]How to get the last day for each month in a distinct year based on a field
SELECT [ItemKey]
,[Location]
,[QtyOnHand]
,[UpdatedOnHandQty]
,AffectedDate
FROM [Test].[dbo].[INCos]
where ItemKey = '20406'
order by ItemKey, year(AffectedDate)
结果如下:
ItemKey UpdatedOnHandQty AffectedDate
20406 1594.03 2013-12-27 00:00:00.000
20406 78.975 2014-09-15 00:00:00.000
20406 1401.975 2014-09-26 00:00:00.000
20406 512.261 2014-10-20 00:00:00.000
20406 849.928 2014-01-06 00:00:00.000
20406 842.132 2014-01-09 00:00:00.000
20406 1283.132 2014-02-05 00:00:00.000
20406 539.03 2014-02-11 00:00:00.000
20406 980.03 2014-05-07 00:00:00.000
20406 486.183 2014-05-12 00:00:00.000
20406 927.183 2014-06-03 00:00:00.000
20406 917.86 2014-06-27 00:00:00.000
20406 927.043 2014-07-18 00:00:00.000
20406 432.209 2014-07-18 00:00:00.000
20406 1314.209 2014-07-29 00:00:00.000
我想要的是每年只记录每月最后一天的记录。 我已经看到很多解决方案,我可以根据GetDate()获得每个月的最后一天,但我不需要。 我只需要根据AffectedDate字段提取每年每月的最后一条记录。
一个快速选项是与Row_Number()一起使用WITH TIES子句
例
Select Top 1 with Ties *
From YourTable
where ItemKey = '20406'
Order by Row_Number() over (Partition by ItemKey,Year(AffectedDate),Month(AffectedDate) Order by AffectedDate Desc)
返回
ItemKey UpdatedOnHandQty AffectedDate
20406 1594.03 2013-12-27 00:00:00.000
20406 842.132 2014-01-09 00:00:00.000
20406 539.03 2014-02-11 00:00:00.000
20406 486.183 2014-05-12 00:00:00.000
20406 917.86 2014-06-27 00:00:00.000
20406 1314.209 2014-07-29 00:00:00.000
20406 1401.975 2014-09-26 00:00:00.000
20406 512.261 2014-10-20 00:00:00.000
WITH CTE
AS (SELECT Row_number()
OVER (
partition BY Month(AffectedDate)
ORDER BY AffectedDate DESC) rn,
[ItemKey],
[Location],
[QtyOnHand],
[UpdatedOnHandQty],
AffectedDate
FROM [Test].[dbo].[INCos]
WHERE ItemKey = '20406')
SELECT *
FROM CTE
WHERE rn = 1
这是一个完整示例的另一个选项,它有很多行快速,希望这对你有用
CREATE TABLE #DATA(
ItemKey INT,
UpdatedOnHandQty DECIMAL(16,2),
AffectedDate DATETIME
)
INSERT INTO #DATA
SELECT 20406 ,1594.03 ,'2013-12-27 00:00:00.000' UNION
SELECT 20406 ,78.975 ,'2014-09-15 00:00:00.000' UNION
SELECT 20406 ,1401.975 ,'2014-09-26 00:00:00.000' UNION
SELECT 20406 ,512.261 ,'2014-10-20 00:00:00.000' UNION
SELECT 20406 ,849.928 ,'2014-01-06 00:00:00.000' UNION
SELECT 20406 ,842.132 ,'2014-01-09 00:00:00.000' UNION
SELECT 20406 ,1283.132 ,'2014-02-05 00:00:00.000' UNION
SELECT 20406 ,539.03 ,'2014-02-11 00:00:00.000' UNION
SELECT 20406 ,980.03 ,'2014-05-07 00:00:00.000' UNION
SELECT 20406 ,486.183 ,'2014-05-12 00:00:00.000' UNION
SELECT 20406 ,927.183 ,'2014-06-03 00:00:00.000' UNION
SELECT 20406 ,917.86 ,'2014-06-27 00:00:00.000' UNION
SELECT 20406 ,927.043 ,'2014-07-18 00:00:00.000' UNION
SELECT 20406 ,432.209 ,'2014-07-18 00:00:00.000' UNION
SELECT 20406 ,1314.209 ,'2014-07-29 00:00:00.000'
SELECT *
FROM #DATA AS d
WHERE d.ItemKey = 20406
and EXISTS(
SELECT TOP 1 1 FROM #DATA AS d1
WHERE D1.ItemKey = d.ItemKEy
AND YEAR(d.AffectedDate) = YEAR(d1.AffectedDate)
AND MONTH(d.AffectedDate)= MONTH(d1.AffectedDate)
GROUP BY YEAR(d1.AffectedDate),MONTH(d1.AffectedDate)
HAVING MAX(d1.AffectedDate) = d.AffectedDate
)
ORDER BY d.AffectedDate desc
DROP TABLE #DATA
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.