我有一個使用舊式 Oracle 連接語法的查詢。我正在嘗試將其轉換為 T-SQL 以反映資料存盤更改。
我的查詢是:
SELECT
client_name1,
personal_business_ind,
birth_date,
sex
FROM
client ce,
exclusions_endorsements ee
WHERE
ce.client_reference = :clientid
AND ee.fourth_insert( ) = ce.client_reference
AND ee.identification_code( ) = 'OCCP1'
ORDER BY
ee.run_date_last_trans DESC;
有人可以幫忙嗎?
我嘗試了以下查詢,但它似乎沒有在 SQL Server 上產生正確的輸出:
SELECT
client_name1,
personal_business_ind,
birth_date,
sex
FROM
client ce,
RIGHT OUTER JOIN
[CLOAS].[EE_ExclusionsEndorsements] ee ON ee.fourth_insert = ce.client_reference AND ee.identification_code = 'OCCP1'
WHERE
ce.client_reference = @clientid
ORDER BY
ee.run_date_last_trans DESC;
uj5u.com熱心網友回復:
那是“舊”的 Oracle 外連接運算子。例如:
SQL> select d.deptno, max(e.ename) ename
2 from dept d, emp e
3 where e.deptno ( ) = d.deptno
4 group by d.deptno
5 order by d.deptno;
DEPTNO ENAME
---------- ----------
10 MILLER
20 SMITH
30 WARD
40
你 - 相反 - 使用
SQL> select d.deptno, max(e.ename) ename
2 from dept d left join emp e on e.deptno = d.deptno
3 group by d.deptno
4 order by d.deptno;
DEPTNO ENAME
---------- ----------
10 MILLER
20 SMITH
30 WARD
40
SQL>
或者,就你而言,
SELECT
client_name1,
personal_business_ind,
birth_date,
sex
FROM exclusions_endorsements ee left join client ce on ee.fourth_insert = ce.client_reference
AND ee.identification_code = 'OCCP1'
WHERE
ce.client_reference = :clientid
ORDER BY
ee.run_date_last_trans DESC;
uj5u.com熱心網友回復:
你很近。但是外部連接表是exclusions_endorsements,不是client,所以你需要一個LEFT OUTER JOIN. 并且FROM client ce在您的查詢之后有一個逗號太多。
還有一點:Oracle 在降序時先對空值進行排序,SQL Server 將它們排在最后。并且 SQL Server 不支持標準 SQL 語法NULLS FIRST。因此,我們需要一個 case 運算式。
SELECT
client_name1,
personal_business_ind,
birth_date,
sex
FROM client ce
LEFT OUTER JOIN exclusions_endorsements ee
ON ee.fourth_insert = ce.client_reference
AND ee.identification_code = 'OCCP1'
WHERE
ce.client_reference = @clientid
ORDER BY
CASE WHEN ee.run_date_last_trans IS NULL THEN 1 ELSE 2 END,
ee.run_date_last_trans DESC;
我應該補充一點,查詢看起來會更好,您是否對所有列進行了限定。例如ce.client_name1。按您未選擇的列排序會使結果看起來完全無序。好吧,也許它是一個不可為空的列,在這種情況下,它只會在結果中將外部連接的行與內部連接的行分開。
轉載請註明出處,本文鏈接:https://www.uj5u.com/qiye/328789.html
標籤:sql sql-server 查询语句
上一篇:僅過濾沒有任何先前交易的資料
