我有下表
Key Year_month Ammount
1 201612 100
1 201701 120
2 201605 1000
2 201608 800
3 201705 500
我需要使用最低的year_month值
为每个键选择一行例如:这应该是查询的结果
Key Year_month Ammount
1 201612 100
2 201605 1000
3 201705 500
问题是我不确定如何做到这一点,我尝试使用
SELECT
KEY,
YEAR_MONTH,
AMMOUNT
FROM TABLE AS T1
WHERE YEAR_MONTH =(SELECT TOP 1 YEAR_MONTH FROM TABLE WHERE KEY = T1.KEY)
但这不起作用
答案 0 :(得分:2)
Select DT.* FROM Data_Table DT
JOIN
( SELECT [KEY], Min(Year_month) AS MinYM FROM Data_Table GROUP BY [KEY] ) DX
ON DX.[KEY]= DT.[KEY] AND DX.MinYM = DT.Year_month ;
答案 1 :(得分:1)
另一种方法
<span th:if="${#arrays.contains({'fr','en','bl'}, entry.value)}">
返回
declare @table table ([key] int, year_month int, amount int)
insert into @table ([key], year_month, amount)
values (1, 201612, 100),
(1, 201701, 120),
(2, 201605, 1000),
(2, 201608, 800),
(3, 201705, 500)
select t.*,
(select t2.amount from @table t2 where t2.[key] = t.[key] and t2.year_month = t.year_month) as amount
from ( select [key], min(year_month) as year_month
from @table
group by [key]
) t
答案 2 :(得分:1)
试试这个
[19:02:48] build dev finished in 13.28 s
> cordova run android
You have been opted out of telemetry. To change this, run: cordova telemetry on.
Android Studio project detected
ANDROID_HOME=G:\dl\Android\sdk
JAVA_HOME=C:\Program Files\Java\jdk1.8.0_162
studio
[19:02:53] lint finished in 4.70 s
Starting a Gradle Daemon (subsequent builds will be faster)
:wrapper
BUILD SUCCESSFUL
in 13s
1 actionable task: 1 executed
Subproject Path: CordovaLib
Subproject Path: app
Exception in thread "main"
java.lang.NullPointerException
at org.gradle.wrapper.PathAssembler.getDistName(PathAssembler.java:84)
at org.gradle.wrapper.PathAssembler.getDistribution(PathAssembler.java:40)
at org.gradle.wrapper.Install.createDist(Install.java:44)
at org.gradle.wrapper.WrapperExecutor.execute(WrapperExecutor.java:107)
at org.gradle.wrapper.GradleWrapperMain.main(GradleWrapperMain.java:61)
(node:2388) UnhandledPromiseRejectionWarning: Unhandled promise rejection (rejection id: 1): Error: cmd: Command failed with exit code 1 Error output:
Exception in thread "main" java.lang.NullPointerException
at org.gradle.wrapper.PathAssembler.getDistName(PathAssembler.java:84)
at org.gradle.wrapper.PathAssembler.getDistribution(PathAssembler.java:40)
at org.gradle.wrapper.Install.createDist(Install.java:44)
at org.gradle.wrapper.WrapperExecutor.execute(WrapperExecutor.java:107)
at org.gradle.wrapper.GradleWrapperMain.main(GradleWrapperMain.java:61)
(node:2388) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code.
[OK] Your app has been deployed.
Did you know you can live-reload changes from your app with --livereload?
答案 3 :(得分:1)
DROP TABLE IF EXISTS #tmp_test01
CREATE TABLE #tmp_test01 ( [KEY] INT
, Year_Month INT
, Amount INT
)
INSERT INTO #tmp_test01
VALUES
(1, 201612, 100)
, (1, 201701, 120)
, (2, 201605, 1000)
, (2, 201608, 800)
, (3, 201705, 500)
WITH CTE AS (
SELECT ROW_NUMBER() OVER (PARTITION BY [KEY] ORDER BY [KEY], [Year_Month]) RN
, [KEY]
, Year_Month
, Amount
FROM #tmp_test01
)
SELECT [KEY]
, Year_Month
, Amount
FROM CTE
WHERE RN = 1