我正在尝试查找EMPLID,Z_SERV_DT_TYPE和SERVICE_DT字段。大多数EMPLID都有'SAD'和'TED'Z_SERV_DT_TYPE。我想将它们组合在一起,并能够看到SERVICE_DT字段,以便更容易对数据进行排序。当我运行下面虽然它说,'不是一个表达的组'有谁知道我做错了什么?
select EMPLID, Z_SERV_DT_TYPE, SERVICE_DT
from PS_Z_ADD_EMP_SRVDT
where Z_SERV_DT_TYPE IN ('TED', 'SAD')
GROUP BY EMPLID
答案 0 :(得分:0)
您不需要一个小组来列出所有在TED和SAD中输入类型的员工
select EMPLID, Z_SERV_DT_TYPE, SERVICE_DT
from PS_Z_ADD_EMP_SRVDT
where Z_SERV_DT_TYPE IN ('TED', 'SAD')
order by Z_SERV_DT_TYPE
select应该具有Group by
中的所有列如果你想获得计数,那么你可以使用GROUP BY
select EMPLID, Z_SERV_DT_TYPE, COUNT(*)
from PS_Z_ADD_EMP_SRVDT
where Z_SERV_DT_TYPE IN ('TED', 'SAD')
group by EMPLID, Z_SERV_DT_TYPE
答案 1 :(得分:0)
如果您想要最大日期,那么您需要使用包含所有不使用聚合函数的列的GROUP BY:
SELECT EMPLID
,Z_SERV_DT_TYPE
,MAX(SERVICE_DT) AS MAX_SERVICE_DT
FROM PS_Z_ADD_EMP_SRVDT
WHERE Z_SERV_DT_TYPE IN ('TED','SAD')
GROUP BY EMPLID
,Z_SERV_DT_TYPE
这应该为您提供每EMPLID
和Z_SERV_DT_TYPE
组合的最长日期。如果你想要MAX日期是否是任何一种类型,那么你可以删除类型:
SELECT EMPLID
,MAX(SERVICE_DT) AS MAX_SERVICE_DT
FROM PS_Z_ADD_EMP_SRVDT
WHERE Z_SERV_DT_TYPE IN ('TED','SAD')
GROUP BY EMPLID
或者使用子查询:
SELECT EMPLID
,Z_SERV_DT_TYPE
,SERVICE_DT
FROM PS_Z_ADD_EMP_SRVDT
WHERE Z_SERV_DT_TYPE IN ('TED','SAD')
AND (EMPLID,SERVICE_DT) IN (
SELECT EMPLID
,MAX(SERVICE_DT)
FROM PS_Z_ADD_EMP_SRVDT
WHERE Z_SERV_DT_TYPE IN ('TED','SAD')
)
或者,如果您想获得幻想,请使用OLAP功能:
SELECT EMPLID
,Z_SERV_DT_TYPE
,SERVICE_DT
FROM PS_Z_ADD_EMP_SRVDT
WHERE Z_SERV_DT_TYPE IN ('TED','SAD')
QUALIFY (
ROW_NUMBER() OVER (
PARTITION BY EMPLID
ORDER BY SERVICE_DT DESC
) = 1
)
这些都是动态的,所以可能需要稍微调整,但应该让你知道如何处理它。