我有下表,比如包含这些记录的订阅
apply plugin: 'com.android.application'
android {
// ...
}
dependencies {
// ...
compile 'com.google.firebase:firebase-core:11.8.0'
// Getting a "Could not find" error? Make sure you have
// added the Google maven respository to your root build.gradle
}
// ADD THIS AT THE BOTTOM
apply plugin: 'com.google.gms.google-services'
我的输出逻辑是:
1)对于每个app_id,我们需要选择提交状态的日期。
2)如果app_id有2条状态为已提交的记录,我们需要选择最早提交日期的记录。
3)如果没有提交日期,则选择具有"已批准"的日期。或"拒绝"状态。
上表的我的输出应该如下所示
app_id Status Date
1 Submitted d1
1 Open d2
1 Approved d3
2 Submitted d4
2 Submitted d5(d4<d5)
3 Open d6
3 Approved d7
4 Open d8
4 Declined d9
你能告诉我如何编写sql查询吗?
答案 0 :(得分:2)
基于已删除的样本数据(为什么......)?
CREATE TABLE #Record (App_id int,
[Status] varchar(12),
[date] char(2));
INSERT INTO #Record
VALUES (1,'Submitted','d1'),
(1,'Open ','d2'),
(1,'Approved ','d3'),
(2,'Submitted','d4'),
(2,'Submitted','d5'),
(3,'Open ','d6'),
(3,'Approved ','d7'),
(4,'Open ','d8'),
(4,'Declined ','d9');
GO
WITH CTE AS(
SELECT *,
ROW_NUMBER() OVER (PARTITION BY App_Id
ORDER BY CASE [Status] WHEN 'Submitted' THEN 0 ELSE 1 END ASC, [date] ASC) AS RN
FROM #Record)
SELECT App_id,
[Status],
[date]
FROM CTE
WHERE RN = 1;
GO
DROP TABLE #Record;
答案 1 :(得分:1)
试试这个
select app_id, min(thedate)
from #tryout
where (
thestatus='Submitted' OR (thestatus = 'Approved' or thestatus='Declined')
)
group by app_id