我有以下查询,它运行良好但我被告知使用CTE
是一个比巨型子查询更好的路线。我对这个概念很陌生,并尝试通过从CASE
语句中的CONVERT
语句到SELECT
列表中的最后CTE
来提取所有内容,但我没有运气。对于试图使用CASE
而不是子查询的人,是否有任何提示?我需要使用子查询的原因是我需要将WHERE
语句中的日期与其他别名日期进行比较,而不能在USE PDX_SAP_USER
GO
SELECT *
FROM (
SELECT E.team_member_name [EMPLOYEE], K.business_segment_desc [BUSINESS SEGMENT], G.order_status [GPS ORDER STATUS], H.po_type [PO TYPE], G.order_no [GPS ORDER NO], I.po_number [SAP PO NUMBER], I.shipping_instruct [SAP SHIP MODE], G.shipping_type [GPS SHIP MODE], CASE
WHEN I.shipping_instruct LIKE 'A%'
THEN CONVERT(VARCHAR(12), (I.revised_ex_factory + 10), 101)
WHEN I.shipping_instruct LIKE 'C%'
THEN CONVERT(VARCHAR(12), (I.revised_ex_factory + 5), 101)
END [DESIRED TRANSIT DATE], CONVERT(VARCHAR(12), I.revised_ex_factory, 101) [LAST CONFIRMED DATE ], CONVERT(VARCHAR(12), S.po_estimated_deliv_date, 101) [CURRENT DELIV DATE],
-- 'days_diff'
I.material [MATERIAL], M.description [DESCRIPTION], I.stock_category [STOCK CATEGORY], I.po_ordered_quantity [PO ORDERED QUANTITY], I.po_recvd_quantity [PO RECVD QUANTITY], I.po_balance_quantity [PO BALANCE QUANTITY], I.po_intransit_quantity [PO INTRANSIT QUANTITY], I.plant_code [PLANT], I.direct_ship_code [DS CODE], I.comment [COMMENT]
FROM (
SELECT order_no, order_status, shipping_type
FROM asagdwpdx_prod.dbo.SimoxOrder1
UNION ALL
SELECT order_no, order_status, shipping_type
FROM asagdwpdx_prod.dbo.SimoxOrder2
UNION ALL
SELECT order_no, order_status, shipping_type
FROM asagdwpdx_prod.dbo.SimoxOrder3
) G
INNER JOIN pdx_sap_user..vw_po_header H ON G.order_no = H.ahag_number
INNER JOIN pdx_sap_user..vw_po_item I ON H.po_number = I.po_number
INNER JOIN pdx_sap_user..vw_po_size S ON I.po_number = S.po_number
AND I.po_item_number = S.po_item_number
INNER JOIN pdx_sap_user..vw_mm_material M ON I.material = M.material
INNER JOIN pdx_sap_user..vw_kd_business_segment K ON M.business_segment_code = K.business_segment_code
INNER JOIN adi_user_maintained..scm_po_employee_name E ON I.po_number = E.po_number
WHERE I.po_balance_quantity > 0
AND I.del_indicator NOT IN ('L', 'S')
AND H.PO_TYPE NOT IN ('01', 'UB')
AND I.shipping_instruct IN ('A1', 'A2', 'A5', 'C1', 'C2', 'C3')
AND G.order_status = '40'
) q1
WHERE q1.[DESIRED TRANSIT DATE] <> q1.[CURRENT DELIV DATE]
ORDER BY q1.[SAP PO NUMBER]
感谢。
SQL:
function chapa() {
console.log('altered');
TouchID.isSupported()
.then(authenticate)
.catch(error => {
AlertIOS.alert('TouchID not supported');
});
}
答案 0 :(得分:2)
派生表和CTE(当然不是语法)之间的唯一区别是CTE允许您编写自引用的递归查询(也称为递归CTE)。提前将事物抽象到CTE中有时更清晰,但这只是一个偏好问题,而不是一个表现问题。就个人而言,我倾向于成为使用CTE来抽象丑陋查询的粉丝,但对于其他人而言,这并不是一个强有力的论据。
通过一个简单的例子,这两个陈述在功能上是相同的
--- using a CTE
;with numbers as
(
select top 10 num = row_number() over (order by (select null))
from sys.all_objects
)
select *
from numbers
-- Using a derived table
select *
from
(
select top 10 num = row_number() over (order by (select null))
from sys.all_objects
) a