回答編集履歴

6

追記

2020/09/27 03:49

投稿

sazi
sazi

スコア25195

test CHANGED
@@ -42,7 +42,7 @@
42
42
 
43
43
  union all
44
44
 
45
- select estimate.estimate_id, invoice.invoice_id, estimate.customer_id
45
+ select estimate.estimate_id, invoice.invoice_id, estimate.customer_id
46
46
 
47
47
  from estimate
48
48
 
@@ -61,3 +61,49 @@
61
61
  and invoice.invoice_id is null
62
62
 
63
63
  ```
64
+
65
+ 追記
66
+
67
+ --
68
+
69
+ 削除フラグを取得後に判断する
70
+
71
+ ```SQL
72
+
73
+ select invoice.estimate_id, invoice.invoice_id, invoice.customer_id
74
+
75
+ , invoice.deleted_at as invoice_deleted_at
76
+
77
+ , estimate.deleted_at as estimate_deleted_at
78
+
79
+ from invoice
80
+
81
+ left join estimate
82
+
83
+ on invoice.customer_id = estimate.customer_id
84
+
85
+ and invoice.estimate_id = estimate.estimate_id
86
+
87
+ where invoice.host_id = 1203507743
88
+
89
+ union all
90
+
91
+ select estimate.estimate_id, invoice.invoice_id, estimate.customer_id
92
+
93
+ , invoice.deleted_at as invoice_deleted_at
94
+
95
+ , estimate.deleted_at as estimate_deleted_at
96
+
97
+ from estimate
98
+
99
+ left join invoice
100
+
101
+ on estimate.customer_id = invoice.customer_id
102
+
103
+ and estimate.estimate_id = invoice.estimate_id
104
+
105
+ where estimate.host_id = 1203507743
106
+
107
+ and invoice.invoice_id is null
108
+
109
+ ```

5

推敲

2020/09/27 03:49

投稿

sazi
sazi

スコア25195

test CHANGED
@@ -11,8 +11,6 @@
11
11
  質問のSQLでは、Full joinが使用できない場合の、全外部結合の組み立てができていませんね。
12
12
 
13
13
 
14
-
15
- 請求書→見積書の参照しかできないみたいですので、
16
14
 
17
15
  A.請求書+関連する見積書:請求書→(lefto join)→見積書
18
16
 

4

推敲

2020/09/26 08:05

投稿

sazi
sazi

スコア25195

test CHANGED
@@ -18,7 +18,7 @@
18
18
 
19
19
  B.見積書のみ:見積書→(lefto join)→請求書で請求書が無いもの
20
20
 
21
- それぞれが全体の補集合です。
21
+ 全体に対してそれぞれが補集合です。
22
22
 
23
23
  Full Joinを使わない場合は、補集合をunion ALL します。
24
24
 

3

推敲

2020/09/26 07:50

投稿

sazi
sazi

スコア25195

test CHANGED
@@ -14,9 +14,11 @@
14
14
 
15
15
  請求書→見積書の参照しかできないみたいですので、
16
16
 
17
- 請求書+関連する見積書:請求書→(lefto join)→見積書
17
+ A.請求書+関連する見積書:請求書→(lefto join)→見積書
18
18
 
19
- 見積書のみ:見積書→(lefto join)→請求書で請求書が無いもの
19
+ B.見積書のみ:見積書→(lefto join)→請求書で請求書が無いもの
20
+
21
+ それぞれが全体の補集合です。
20
22
 
21
23
  Full Joinを使わない場合は、補集合をunion ALL します。
22
24
 

2

推敲

2020/09/26 07:46

投稿

sazi
sazi

スコア25195

test CHANGED
@@ -18,11 +18,9 @@
18
18
 
19
19
  ・見積書のみ:見積書→(lefto join)→請求書で請求書が無いもの
20
20
 
21
- ※上記背反なので重なりせん
21
+ Full Joinを使わない場合、補集合をunion ALL し
22
22
 
23
-
24
-
25
- 上記をunion allします。
23
+ ※補集合なので重なりませんからunion allします。
26
24
 
27
25
  ```SQL
28
26
 

1

推敲

2020/09/26 07:43

投稿

sazi
sazi

スコア25195

test CHANGED
@@ -5,6 +5,8 @@
5
5
  見積書・請求書の顧客を使用するとして結合するかどうかは後回し。
6
6
 
7
7
  > 見積書・請求書、どちらか一方のデータがあれば出力したいです
8
+
9
+
8
10
 
9
11
  質問のSQLでは、Full joinが使用できない場合の、全外部結合の組み立てができていませんね。
10
12