tsql - 基于表中的两列的数据透视
问题描述
我有一张这样的桌子
PCode Milestone DeliveryDate Status
------------------------------------------------------
P1234 Start 14/5/2019 Complete
P1234 End 17/6/2019 Complete
P2345 Start 8/6/2019 Progress
P2345 End 19/6/2019 Progress
P7335 Start 18/8/2019 Provisional
P7335 End 19/9/2019 Provisional
-----------------------------------------------------------
根据上面的数据我需要得到一个这样的表
PCode Start End Status
---------------------------------------------
P1234 14/05/2019 17/06/2019 Completed
P2345 8/6/2019 19/06/2019 Progress
P7335 18/08/2019 19/09/2019 Provisional
----------------------------------------------
我不知道如何使用一些 PIVOT 来实现这一点
解决方案
一个简单的条件聚合应该可以解决问题
这假设[Status]
跨PCode
记录是一致的
Selet PCode
,[Start] = min(case when Milestone='Start' then DeliveryDate end)
,[Ebd] = max(case when Milestone='End' then DeliveryDate end)
,[Status] = max([Status])
From YourTable
Group By PCode
编辑 - 查找开始和结束状态
Select PCode
,[Start] = min(case when Milestone='Start' then DeliveryDate end)
,[End] = max(case when Milestone='End' then DeliveryDate end)
,[StatusStart] = min(case when Milestone='Start' then [Status] end)
,[StatusEnd] = min(case when Milestone='End' then [Status] end)
From YourTable
Group By PCode
推荐阅读
- html - 如何使 Tweenmax 动画仅适用于特定视口?
- git - Git 推送:缺少或无效的凭据。致命:“https://github.com/username/repo.git”的身份验证失败
- html - 更改特定按钮的离子按钮颜色而不是所有按钮
- wordpress - 如何在 WordPress 中为帖子创建第二个特色图片框
- c# - 在默认注入的 IHttpClientFactory 中禁用 SSL 证书验证
- python - 如何去除图像中导致图像特征丢失的额外发光?
- javascript - 如何停止无限循环?
- reactjs - 在反应中使用recoil.js,在类组件中不使用钩子
- spring - 我想知道下面的代码是否正确
- c++ - 如何将 I->getType() 作为参数传递给 llvm 中的 CreateCall?