我知道这可能听起来有些混乱,但基本上我有一个查询返回一组"群集名称"和" ID"。我希望查询只为每个ID选择一行,基于"群集名称",按字母顺序排序。
例如,我们当前正在让查询返回以下内容:
Cluster Name ID
Construction Technologies 239378769
Construction Technologies 239378942
Construction Technologies 239510698
Law and Public Safety 239510698
Health Science 239510698
Health Science 240236166
Health Science 240236203
Construction Technologies 240236209
Health Science 240236236
Education and Training 240236303
如果您注意到ID 239510698出现三次,建筑技术,法律和公共安全以及健康科学。我们想要的不是将这行返回三次,而是只返回一次,这将是按字母顺序排列的第一行,即Construction Technologies。
以下是查询:
SELECT c.clustername AS ClusterName, MAX(cc.monsterid) AS CountOfClusters
FROM tblCareerCluster cc
INNER JOIN tblClusters c ON c.clusterid = cc.clusterid
LEFT JOIN tblStudentPersonal sp ON sp.monsterid = cc.monsterid
INNER JOIN tblStudentSchool ss ON ss.monsterid = cc.monsterid
INNER JOIN tblSchools s ON s.schoolid = ss.schoolid
INNER JOIN tblSchoolDistricts sd ON sd.schoolid = s.schoolid
INNER JOIN tblDistricts d ON d.districtid = sd.districtid
INNER JOIN tblDistrictUserDistrictGroups rurg ON rurg.schoolid = ss.schoolid
INNER JOIN tblGroups g ON g.groupid = rurg.groupid
INNER JOIN tblUserGroups ug ON ug.groupid = g.groupid
WHERE cc.ranking = (SELECT MAX(cc2.ranking) from tblCareerCluster cc2 INNER JOIN tblCareerCluster cc ON cc.monsterid = cc2.monsterid)
AND ss.graduationyear IN (SELECT Items FROM FN_Split('2015,2016,2017,2018,2019,2020,2021,2022,2023', ',')) AND sp.optin = 'Yes'
AND g.groupname = 'My Districts' AND ug.userid = 14
GROUP BY c.clustername, cc.ranking, d.district, cc.monsterid, cc.clusterid, d.IRN, d.districtid
如果您需要任何其他信息,请告诉我们,我们将非常感谢您的帮助。
答案 0 :(得分:1)
select MIN(A.ClusterName), A.CountOfClusters
FROM
(
SELECT c.clustername AS ClusterName, MAX(cc.monsterid) AS CountOfClusters
FROM tblCareerCluster cc
INNER JOIN tblClusters c ON c.clusterid = cc.clusterid
LEFT JOIN tblStudentPersonal sp ON sp.monsterid = cc.monsterid
INNER JOIN tblStudentSchool ss ON ss.monsterid = cc.monsterid
INNER JOIN tblSchools s ON s.schoolid = ss.schoolid
INNER JOIN tblSchoolDistricts sd ON sd.schoolid = s.schoolid
INNER JOIN tblDistricts d ON d.districtid = sd.districtid
INNER JOIN tblDistrictUserDistrictGroups rurg ON rurg.schoolid = ss.schoolid
INNER JOIN tblGroups g ON g.groupid = rurg.groupid
INNER JOIN tblUserGroups ug ON ug.groupid = g.groupid
WHERE cc.ranking = (SELECT MAX(cc2.ranking) from tblCareerCluster cc2 INNER JOIN tblCareerCluster cc ON cc.monsterid = cc2.monsterid)
AND ss.graduationyear IN (SELECT Items FROM FN_Split('2015,2016,2017,2018,2019,2020,2021,2022,2023', ',')) AND sp.optin = 'Yes'
AND g.groupname = 'My Districts' AND ug.userid = 14
GROUP BY c.clustername, cc.ranking, d.district, cc.monsterid, cc.clusterid, d.IRN, d.districtid
) A
GROUP BY A.CountOfClusters
答案 1 :(得分:1)
我认为这个可能更简单
使用返回Cluster Name
和ID
SELECT MIN(Cluster_Name) as ClusterName, ID
FROM <your query>
GROUP BY ID
答案 2 :(得分:0)
使用Row_Number
为ClusterName排序的每个ID选择第一个结果:
With CTE as (select *, Row_Number() over (partition by ID order by ClusterName) as RN from [Long sequence of tables and joins])
Select ClusterName, ID from CTE where RN = 1
如果您愿意,可以将CTE转换为临时表或子查询。
答案 3 :(得分:0)
您可以使用row_number
窗口函数对ID的每个分区中的行进行编号,然后选择行号= 1.如果无法将函数直接添加到查询中,则可以使用公用表表达式并将原始查询用作派生表:
with cte as (
select *, rn = row_number() over (partition by id order by clustername)
from ( << your_original_query here >> ) t
)
select * from cte
where rn = 1
order by ClusterName