如何在具有左连接的查询中过滤年份?

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

我试图在查询中过滤 2022 年,但它不仅仅过滤 2022 年。我目前在查询的底部有年份过滤器,我认为 where 子句需要位于连接之前。我还尝试将 where 子句移到“from tp.load l”之后,但这也不起作用。我知道我正在尝试过滤那一年的表格。

select 
l.truck_id,
l.CREATED_DATE,
st.location_name,
st.location_address_1,
st.location_city,
st.location_state_code,
st.location_postal_code,
l.mode_type,
li.weight,
li.hazmat,
li.PRODUCT_DESCRIPTION


from tp.table l

LEFT OUTER JOIN
(Select m.* from
(SELECT truck_ID,hazmat,sum(weight) as 'Weight', product_description,
Row_number() OVER(PARTITION BY truck_ID ORDER BY freight_ID DESC) AS R_NO
FROM [TP].[table2] where status='active'
GROUP BY truck_ID,HAZMAT, PRODUCT_DESCRIPTION,FREIGHT_ID)
m where R_NO=1)li
ON L.truck_ID=li.truck_id

LEFT OUTER JOIN
(Select n.* from
(SELECT truck_ID, location_name, location_address_1, location_city, location_state_code,location_postal_code, 
Row_number() OVER(PARTITION BY truck_ID ORDER BY stop_ID DESC) AS R_NO
FROM [TP].[table3] where status='active' and STOP_SEQUENCE_NUMBER = '1') n where R_NO=1) st
ON L.truck_ID=st.truck_id


where year(created_datetime)= '2022'
and LOCATION_address_1 LIKE '%6210 GLENWAY%'
or LOCATION_address_1  like '%480 WILEY%'



sql where-clause
1个回答
1
投票
where year(created_datetime)= '2022'
and LOCATION_address_1 LIKE '%6210 GLENWAY%'
or LOCATION_address_1  like '%480 WILEY%'

被评价为

where (year(created_datetime)= '2022' and LOCATION_address_1 LIKE '%6210 GLENWAY%')
    or LOCATION_address_1  like '%480 WILEY%'

即2022 年 GLENWAY,或 WILEY(无论哪一年。)

但你想要

where year(created_datetime)= '2022'
  and (LOCATION_address_1 LIKE '%6210 GLENWAY%'
    or LOCATION_address_1  like '%480 WILEY%')

即年份必须是 2022 年,并且地址类似于其中之一GLENWAY 或 WILEY 等

© www.soinside.com 2019 - 2024. All rights reserved.