SQL:在多列上检查(其中只有一列必须不为空)

问题描述 投票:0回答:1

好的,

所以我有如下报告:

WITH   StudentCTEm ([Person Code], [Course Instance Maths], [Course Occurrence Maths], [Course Title], [ULN], [Main Aim])
AS     (SELECT [Person Code],
               [Course Instance],
               [Course Occurrence],
               [Course Title],
               [ULN],
               [Main Aim]
        FROM   EBSWarehouse.dbo.StudentEnrolment cte
        WHERE  cte.[Progress Status] = 'A' AND cte.[Course Instance] LIKE '%REGM%' AND cte.[Course Occurrence] LIKE '1920%'),
StudentCTEe ([Person Code], [Course Instance English], [Course Occurrence English], [Course Title], [ULN], [Main Aim])
AS     (SELECT [Person Code],
               [Course Instance],
               [Course Occurrence],
               [Course Title],
               [ULN],
               [Main Aim]
        FROM   EBSWarehouse.dbo.StudentEnrolment cte
        WHERE  cte.[Progress Status] = 'A' AND cte.[Course Instance] LIKE '%REGE%' AND cte.[Course Occurrence] LIKE '1920%'),
StudentCTEIT ([Person Code], [Course Instance IT], [Course Occurrence IT], [Course Title], [ULN], [Main Aim])
AS     (SELECT [Person Code],
               [Course Instance],
               [Course Occurrence],
               [Course Title],
               [ULN],
               [Main Aim]
        FROM   EBSWarehouse.dbo.StudentEnrolment cte
        WHERE  cte.[Progress Status] = 'A' AND cte.[Course Instance] LIKE '%REGIT%' AND cte.[Course Occurrence] LIKE '1920%'),
pcsCTEfsM ([Learning Aim Ref Maths], [Learning Aim Desc Maths], [PICS ID], [Funding End])
AS     (SELECT [Learning Aim Ref],
               [Learning Aim Desc],
               [PICS ID],
               [Funding End]
        FROM   dbo.ILRComponentAim cte
        WHERE [Learning Aim Desc] like '%Functional Skills qualification in Mathematics%'),
pcsCTEfsE ([Learning Aim Ref English], [Learning Aim Desc English], [PICS ID], [Funding End])
AS     (SELECT [Learning Aim Ref],
               [Learning Aim Desc],
               [PICS ID],
               [Funding End]
        FROM   dbo.ILRComponentAim cte
        WHERE [Learning Aim Desc] like '%Functional Skills qualification in English%'),
pcsCTEfsIT ([Learning Aim Ref IT], [Learning Aim Desc IT], [PICS ID], [Funding End])
AS     (SELECT [Learning Aim Ref],
               [Learning Aim Desc],
               [PICS ID],
               [Funding End]
        FROM   dbo.ILRComponentAim cte
        WHERE [Learning Aim Desc] like '%Functional Skills qualification in Information%')


SELECT DISTINCT
s.[Person Code],
s.ULN,
s.Forenames,
s.Surname,
s.[Episode Start],
s.[Episode Exp End],
s.[Person Code],
icm.[Learning Aim Ref Maths],
ICm.[Learning Aim Desc Maths],
Sem.[Course Instance Maths],
Sem.[Course Occurrence Maths],
SEm.[Course Title],
ice.[Learning Aim Ref English],
ICe.[Learning Aim Desc English],
See.[Course Instance English],
See.[Course Occurrence English],
SEe.[Course Title],
icit.[Learning Aim Ref IT],
icit.[Learning Aim Desc IT],
Seit.[Course Instance IT],
Seit.[Course Occurrence IT],
SEIT.[Course Title]






FROM dbo.Student s
LEFT JOIN           dbo.ILRProgAim AS p ON p.[PICS ID] = S.[PICS ID]
LEFT JOIN       pcsCTEfsM icm ON icm.[PICS ID] = s.[PICS ID]
LEFT JOIN       pcsCTEfsE ice ON ice.[PICS ID] = s.[PICS ID]
LEFT JOIN       pcsCTEfsIT icit ON icit.[PICS ID] = s.[PICS ID]
LEFT JOIN       StudentCTEe see ON see.[ULN] = s.[ULN]
LEFT JOIN       StudentCTEm sem ON sem.[ULN] = s.[ULN]
LEFT JOIN       StudentCTEIT seit ON seit.[ULN] = s.[ULN]
INNER JOIN EBSWarehouse.dbo.StudentEnrolment sen ON sen.[ULN] = s.ULN  

WHERE s.[Episode End] is null AND s.[Status] = 'L' AND [Learning Aim End] is null AND icm.[Funding End] is null AND sen.[Main Aim] <> 1 
ORDER BY s.[Person Code] ASC

某些记录显示空为:

    SELECT 
    icm.[Learning Aim Ref Maths],
    ICm.[Learning Aim Desc Maths],
    Sem.[Course Instance Maths],
    Sem.[Course Occurrence Maths],
    SEm.[Course Title],
    ice.[Learning Aim Ref English],
    ICe.[Learning Aim Desc English],
    See.[Course Instance English],
    See.[Course Occurrence English],
    SEe.[Course Title],
    icit.[Learning Aim Ref IT],
    icit.[Learning Aim Desc IT],
    Seit.[Course Instance IT],
    Seit.[Course Occurrence IT],
    SEIT.[Course Title]
    ...

哪个很好。

如何在我的WHERE子句中仅显示上面至少一列不为空的记录

感谢

我知道这是肮脏的报告写作,但这是一项需要做的快速工作

告诉我添加更多详细信息:

如果您想要上下文是的,因此,这基本上是在各个表和另一个数据库之间进行映射,以准确计算谁正在使用其PICS(内部软件)学徒课程进行功能技能课程,以及我们在内部学生管理系统上拥有的信息。

我应该添加预期结果与实际结果相同,对于GDPR,我无法真正发布结果集),但实际上我需要做的就是忽略报告中所有记录均为'null'的记录课程选项(基本上有些人撤回或完成了他们的FS部分,但没有撤回他们的WBL课程,因此出现在此报告中,我尝试使用“完成状态”列省略记录,但有些人被认为仍处于活动状态,并且从很久以前就离开了-所以这是不行的,所以我唯一的选择就是我正在整理的duuuurty小视图

sql sql-server ssms
1个回答
2
投票

COALESCE?

WHERE   COALESCE    (
                        icm.[Learning Aim Ref Maths],
                        ICm.[Learning Aim Desc Maths],
                        Sem.[Course Instance Maths],
                        Sem.[Course Occurrence Maths],
                        SEm.[Course Title],
                        ice.[Learning Aim Ref English],
                        ICe.[Learning Aim Desc English],
                        See.[Course Instance English],
                        See.[Course Occurrence English],
                        SEe.[Course Title],
                        icit.[Learning Aim Ref IT],
                        icit.[Learning Aim Desc IT],
                        Seit.[Course Instance IT],
                        Seit.[Course Occurrence IT],
                        SEIT.[Course Title]
                    ) IS NOT NULL
© www.soinside.com 2019 - 2024. All rights reserved.