SQL:WHERE 检查多列(其中只有一列必须不为空)
SQL: WHERE check on multiple columns (Where only one has to be not null)
好的,
所以我有一个报告如下:
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 我真的不能 post 结果集)但实际上我需要做的就是省略报告中的任何记录在所有课程选项中都有 'null'(基本上有些人已经撤回或完成了他们的 FS 部分而不是他们的 WBL 课程,因此作为本报告的一部分出现,我尝试使用完成状态列省略记录, 但有些人被认为仍然活跃并且很久以前就离开了 - 所以那是不行的,所以我唯一的选择是 duuuurty
我正在整理的小视图
合并?
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
好的,
所以我有一个报告如下:
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 我真的不能 post 结果集)但实际上我需要做的就是省略报告中的任何记录在所有课程选项中都有 'null'(基本上有些人已经撤回或完成了他们的 FS 部分而不是他们的 WBL 课程,因此作为本报告的一部分出现,我尝试使用完成状态列省略记录, 但有些人被认为仍然活跃并且很久以前就离开了 - 所以那是不行的,所以我唯一的选择是 duuuurty
我正在整理的小视图
合并?
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