WITH
irelevant as (
SELECT [Entity_Number] from [HFA_M].[dbo].[IB_Entity_Events]
where Event_Code in ('1552', '1567')) -- כל הבקשות שקיבלו קוד 1552,1567 לסינון בהמשך השאילתה
SELECT
ROW_NUMBER() OVER(ORDER BY [IB_Bakasha].[Tik_Binian] ASC) AS OID
,[IB_Bakasha].[Tik_Binian]
,[IB_Bakasha].[Bakasha_Number]
,[IB_Bakasha].[Bakasha_Description]
,[IB_Bakasha].[Ikari_Area]
,[IB_Bakasha].[Y_Diur]
,[IB_Bakasha].[Floors_Requested_Number]
,[IB_Bakasha].[Basement_Floors]
,[IB_Entity_Events].[Entity_Number]
,[IB_Entity_Events].[Event_Code]
,[IB_Entity_Events].[Event_Date]
FROM [HFA_M].[dbo].[IB_Bakasha]
join [HFA_M].[dbo].[IB_Entity_Events] on [Bakasha_Number]= [IB_Entity_Events.[Entity_Number]
where
[IB_Bakasha].[Bakasha_Number] not in (select * from irelevant)
and ([IB_Entity_Events].[Event_Code] = '1100' or [IB_Entity_Events].[Event_Code] = '1101')
and [Table_Number] = '2'
and [Maslul_Code] = '1'
查询从存储用户请求的表中检索记录。
每个请求由 [Bakasha_Number] 字段标识。
一个请求可以出现在多个具有不同[Event_Code]的记录中,这意味着该请求在处理过程中经历了多个阶段。
我想要 [Event_Code] 为 1100 或 1101 的记录。
但是,我需要帮助:
当一个请求出现两次时,[Event_Code] 为 1100,一次为 1101,我只想选择 [Event_Code] 为 1100 的记录。
换句话说:
如果一个请求 ID ([Bakasha_Number]) 有一个事件代码 - 选择它
如果一个请求 ID ([Bakasha_Number]) 具有两个事件代码 - 仅选择 1100
有什么建议我该怎么做吗?
我尝试创建子选择,但它太复杂且不可读
我将基于 IB_Entity_Events 创建第二个 CTE,以按照您希望的优先顺序对 Event_Codes 进行优先级排序;然后加入其中。像这样:
WITH
irelevant as (
SELECT [Entity_Number] from [HFA_M].[dbo].[IB_Entity_Events]
where Event_Code in ('1552', '1567')), -- כל הבקשות שקיבלו קוד 1552,1567 לסינון בהמשך השאילתה
ee as
(
select
Entity_Number,
Event_Code,
Event_Date,
row_number() over
(
partition by
Entity_Number
order by
Event_Code asc
) as Priority_Number
from
[HFA_M].[dbo].[IB_Entity_Events]
where
[Event_Code] in ('1100', '1101')
)
SELECT
ROW_NUMBER() OVER(ORDER BY [IB_Bakasha].[Tik_Binian] ASC) AS OID
,[IB_Bakasha].[Tik_Binian]
,[IB_Bakasha].[Bakasha_Number]
,[IB_Bakasha].[Bakasha_Description]
,[IB_Bakasha].[Ikari_Area]
,[IB_Bakasha].[Y_Diur]
,[IB_Bakasha].[Floors_Requested_Number]
,[IB_Bakasha].[Basement_Floors]
,ee.Entity_Number
,ee.Event_Code
,ee.Event_Date
FROM [HFA_M].[dbo].[IB_Bakasha]
join ee on [Bakasha_Number]= ee.Entity_Number and ee.Priority_Number = 1
where
[IB_Bakasha].[Bakasha_Number] not in (select * from irelevant)
and [Table_Number] = '2'
and [Maslul_Code] = '1'