简体   繁体   English

使用group_concat

[英]Use of group_concat

I have following a table called LOPList: 我有一个称为LOPList的表:

忙碌的猫

I want results to be : 我希望结果是:

LOPID | EMP ID | TIME START (LOPStatus = 'Y') | TIME END (LOPStatus = 'N')

2     | 6      | 2016-03-24T20:05:27+00:00    | 2016-03-24T20:14:41+00:00

5     | 6      | 2016-03-24T20:07:59+00:00    | 2016-03-24T20:13:11+00:00

Try: 尝试:

select LOPID,EmpId,
max(case when LOPStatus='Y' then TimeUpdated end) as time_start,
max(case when LOPStatus='N' then TimeUpdated end) as time_end  
from LOPList where JobID= 22 group by LOPID;

SELECT list.LOPID,list.EmpID as "EMP ID", listY.TimeUpdated as "TIME START (LOPStatus = 'Y')",listN.TimeUpdated as "TIME END (LOPStatus = 'N')" from (SELECT DISTINCT LOPID,EmpID FROM [TestDB].[dbo].[LOPList]) AS list , (Select LOPID,LOPStatus,TimeUpdated from [TestDB].[dbo].[LOPList] where LOPStatus = 'Y') listY, (Select LOPID,LOPStatus,TimeUpdated from [TestDB].[dbo].[LOPList] where LOPStatus = 'N') listN WHERE listY.LOPID = list.LOPID and listN.LOPID = list.LOPID; 从(SELECT DISTINCT LOPID)中选择list.LOPID,list.EmpID为“ EMP ID”,listY.Time更新为“ TIME START(LOPStatus ='Y')”,listN.TimeUpdate为“ TIME END(LOPStatus ='N')” ,EmpID from [TestDB]。[dbo]。[LOPList])AS列表,(选择LOPID,LOPStatus,TimeUpdated从[TestDB]。[dbo]。[LOPList]其中LOPStatus ='Y')listY,(选择LOPID, LOPStatus,Time从[TestDB]。[dbo]。[LOPList]更新,其中LOPStatus ='N')listN WHERE listY.LOPID = list.LOPID和listN.LOPID = list.LOPID;

You should know some sql language besic knowledge,here you should use 'DISTINCT ' statement, thanks, hope can take you some help 您应该了解一些sql语言基本知识,在这里应该使用'DISTINCT'语句,谢谢,希望能对您有所帮助

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM