如何在针对SQL中的键获取多个值时仅获取一个值

如何在针对SQL中的键获取多个值时仅获取一个值,sql,sql-server-2012,Sql,Sql Server 2012,我有一个SQL查询。下面是查询 select ID, replace(replace(replace(replace([Name],',',''),'"',''),':',''),'?','') [Name] , replace(replace([Description],',',''),'"','') [Description], [GUID], Bussinesskey from course where COURSESTA

我有一个SQL查询。下面是查询

select 
    ID, 
    replace(replace(replace(replace([Name],',',''),'"',''),':',''),'?','') [Name] ,
    replace(replace([Description],',',''),'"','') [Description],
    [GUID],
    Bussinesskey 
from course 
where COURSESTATUS = 'Published'  and RETIREDTF = 0  
and 
    bussinesskey in 
    (
    ...
    )
and id in (
    select c.id from course c
    inner join COURSE_CUSTOMERENTITLEMENT cce on cce.COURSE_ID = c.ID
    inner join CUSTOMERENTITLEMENT ce on ce.id = cce.CUSTOMERENTITLEMENT_ID
    where 
        ce.ENROLLMENTTYPE = 'Course' 
    and ce.customer_id =  23753 
    and c.COURSESTATUS = 'Published'  and c.RETIREDTF = 0
    UNION 
    select c.id from course c
    inner join COURSE_COURSEGROUP cg on cg.course_id = c.id
    inner join COURSEGROUP_CUSTOMERENTITLEMENT cgce on cgce.COURSEGROUP_ID = cg.COURSEGROUP_ID
    inner join CUSTOMERENTITLEMENT ce on ce.id = cgce.CUSTOMERENTITLEMENT_ID
    where 
        ce.ENROLLMENTTYPE = 'CourseGroup' 
    and ce.customer_id = 23753  
    and c.COURSESTATUS = 'Published'  and c.RETIREDTF = 0
)
order by name, id asc 
当这个查询运行时,我会得到如下快照所示的输出

你可以在屏幕截图中看到,我得到了8个相同类型合同的名称。合同的最后一个id是780697,这是添加到数据库中的最新记录。现在我希望当我的查询运行时,它只得到最新的记录。表示不显示8个Contarcts名称。这只是每个课程名称的最新版本。仅显示ID为780697的合同平均值记录。如果其他课程有相同的结果,则仅显示最新的Id记录。我怎样才能做到这一点


谢谢

为了获得最新的ID,您可以尝试以下操作:-

select MAX(ID), 
replace(replace(replace(replace([Name],',',''),'"',''),':',''),'?','') [Name] ,
replace(replace([Description],',',''),'"','') [Description],
[GUID],
Bussinesskey 
from course 
where COURSESTATUS = 'Published'  and RETIREDTF = 0  
and 
bussinesskey in 
(
...
)
and id in (
select c.id from course c
inner join COURSE_CUSTOMERENTITLEMENT cce on cce.COURSE_ID = c.ID
inner join CUSTOMERENTITLEMENT ce on ce.id = cce.CUSTOMERENTITLEMENT_ID
where 
    ce.ENROLLMENTTYPE = 'Course' 
and ce.customer_id =  23753 
and c.COURSESTATUS = 'Published'  and c.RETIREDTF = 0
UNION 
select c.id from course c
inner join COURSE_COURSEGROUP cg on cg.course_id = c.id
inner join COURSEGROUP_CUSTOMERENTITLEMENT cgce on cgce.COURSEGROUP_ID = cg.COURSEGROUP_ID
inner join CUSTOMERENTITLEMENT ce on ce.id = cgce.CUSTOMERENTITLEMENT_ID
where 
    ce.ENROLLMENTTYPE = 'CourseGroup' 
and ce.customer_id = 23753  
and c.COURSESTATUS = 'Published'  and c.RETIREDTF = 0
)
group by [Name], [Description], [GUID], Bussinesskey 
order by name, id asc 
这将产生: ID名称说明GUID业务密钥 90000份合同ARB5 40000份非合同ARB1


请告知我的数据假设在哪里不成立。

这里是我如何做到的。我不知道这有多有效,但它给了我想要的东西

select ID, [Name], [Description], [GUID], Bussinesskey from (
    select row_number() over (partition by [Name] order by id desc) as row, t1.* from (
        select 
            ID, 
            replace(replace(replace(replace([Name],',',''),'"',''),':',''),'?','') [Name] ,
            replace(replace([Description],',',''),'"','') [Description],
            [GUID],
            Bussinesskey 
        from course 
        where COURSESTATUS = 'Published'  and RETIREDTF = 0  
        and bussinesskey in  (
            'PSTOAS0314001',
             ...
            'RECEAL0510019'
        )
        and id in (
            select c.id from course c
            inner join COURSE_CUSTOMERENTITLEMENT cce on cce.COURSE_ID = c.ID
            inner join CUSTOMERENTITLEMENT ce on ce.id = cce.CUSTOMERENTITLEMENT_ID
            where ce.ENROLLMENTTYPE = 'Course' 
            and ce.customer_id =  23753 
            and c.COURSESTATUS = 'Published'  and c.RETIREDTF = 0
            UNION 
            select c.id from course c
            inner join COURSE_COURSEGROUP cg on cg.course_id = c.id
            inner join COURSEGROUP_CUSTOMERENTITLEMENT cgce on cgce.COURSEGROUP_ID = cg.COURSEGROUP_ID
            inner join CUSTOMERENTITLEMENT ce on ce.id = cgce.CUSTOMERENTITLEMENT_ID
            where ce.ENROLLMENTTYPE = 'CourseGroup' 
            and ce.customer_id = 23753  
            and c.COURSESTATUS = 'Published'  and c.RETIREDTF = 0
        )
    )t1
) t 
where t.row=1 
order by t.name, t.id asc

感谢

按GUID和业务分组不会导致OPYup描述的合同的单行运行此查询生成错误,即列course.ID在ORDER by子句中无效,因为它既不包含在聚合函数中,也不包含在GROUP by子句中。您的DB设计会造成这种混乱。你最好进行一次回顾和重建。然而您的问题是:如何获得每个字段的MaxKey值。因此,如果您在一个CTE中进行查询,并创建了第二个CTE,每个名称获得最大ID,然后加入两个CTE,您将获得每个课程名称的最大ID作为筛选器,以及第一个CTE中该最大ID的所有数据…嗨,谢谢。实际上,这个数据库设计不是我自己做的,所以我不能说关于这个数据库或表设计的任何事情:。不管怎样,我运行你的查询。但它并没有起到观望的作用。如果按原样运行此查询,则会得到一个错误,即列名称“ID”不明确。列名称“name”不明确。。如果我输入c.ID、c.[Name]、c.[GUID]、c.Bussinesskey,然后运行查询,那么我得到的行太多,课程名称也重复。
select ID, [Name], [Description], [GUID], Bussinesskey from (
    select row_number() over (partition by [Name] order by id desc) as row, t1.* from (
        select 
            ID, 
            replace(replace(replace(replace([Name],',',''),'"',''),':',''),'?','') [Name] ,
            replace(replace([Description],',',''),'"','') [Description],
            [GUID],
            Bussinesskey 
        from course 
        where COURSESTATUS = 'Published'  and RETIREDTF = 0  
        and bussinesskey in  (
            'PSTOAS0314001',
             ...
            'RECEAL0510019'
        )
        and id in (
            select c.id from course c
            inner join COURSE_CUSTOMERENTITLEMENT cce on cce.COURSE_ID = c.ID
            inner join CUSTOMERENTITLEMENT ce on ce.id = cce.CUSTOMERENTITLEMENT_ID
            where ce.ENROLLMENTTYPE = 'Course' 
            and ce.customer_id =  23753 
            and c.COURSESTATUS = 'Published'  and c.RETIREDTF = 0
            UNION 
            select c.id from course c
            inner join COURSE_COURSEGROUP cg on cg.course_id = c.id
            inner join COURSEGROUP_CUSTOMERENTITLEMENT cgce on cgce.COURSEGROUP_ID = cg.COURSEGROUP_ID
            inner join CUSTOMERENTITLEMENT ce on ce.id = cgce.CUSTOMERENTITLEMENT_ID
            where ce.ENROLLMENTTYPE = 'CourseGroup' 
            and ce.customer_id = 23753  
            and c.COURSESTATUS = 'Published'  and c.RETIREDTF = 0
        )
    )t1
) t 
where t.row=1 
order by t.name, t.id asc