teratail header banner
teratail header banner
質問するログイン新規登録

回答編集履歴

6

追記

2020/09/27 03:49

投稿

sazi
sazi

スコア25430

answer CHANGED
@@ -20,7 +20,7 @@
20
20
  and estimate.deleted_at is null
21
21
  where invoice.host_id = 1203507743
22
22
  union all
23
- select estimate.estimate_id, invoice.invoice_id, estimate.customer_id
23
+ select estimate.estimate_id, invoice.invoice_id, estimate.customer_id
24
24
  from estimate
25
25
  left join invoice
26
26
  on estimate.customer_id = invoice.customer_id
@@ -29,4 +29,27 @@
29
29
  and invoice.deleted_at is null
30
30
  where estimate.host_id = 1203507743
31
31
  and invoice.invoice_id is null
32
+ ```
33
+ 追記
34
+ --
35
+ 削除フラグを取得後に判断する
36
+ ```SQL
37
+ select invoice.estimate_id, invoice.invoice_id, invoice.customer_id
38
+ , invoice.deleted_at as invoice_deleted_at
39
+ , estimate.deleted_at as estimate_deleted_at
40
+ from invoice
41
+ left join estimate
42
+ on invoice.customer_id = estimate.customer_id
43
+ and invoice.estimate_id = estimate.estimate_id
44
+ where invoice.host_id = 1203507743
45
+ union all
46
+ select estimate.estimate_id, invoice.invoice_id, estimate.customer_id
47
+ , invoice.deleted_at as invoice_deleted_at
48
+ , estimate.deleted_at as estimate_deleted_at
49
+ from estimate
50
+ left join invoice
51
+ on estimate.customer_id = invoice.customer_id
52
+ and estimate.estimate_id = invoice.estimate_id
53
+ where estimate.host_id = 1203507743
54
+ and invoice.invoice_id is null
32
55
  ```

5

推敲

2020/09/27 03:49

投稿

sazi
sazi

スコア25430

answer CHANGED
@@ -5,7 +5,6 @@
5
5
 
6
6
  質問のSQLでは、Full joinが使用できない場合の、全外部結合の組み立てができていませんね。
7
7
 
8
- 請求書→見積書の参照しかできないみたいですので、
9
8
  A.請求書+関連する見積書:請求書→(lefto join)→見積書
10
9
  B.見積書のみ:見積書→(lefto join)→請求書で請求書が無いもの
11
10
  全体に対してそれぞれが補集合です。

4

推敲

2020/09/26 08:05

投稿

sazi
sazi

スコア25430

answer CHANGED
@@ -8,7 +8,7 @@
8
8
  請求書→見積書の参照しかできないみたいですので、
9
9
  A.請求書+関連する見積書:請求書→(lefto join)→見積書
10
10
  B.見積書のみ:見積書→(lefto join)→請求書で請求書が無いもの
11
- それぞれが全体の補集合です。
11
+ 全体に対してそれぞれが補集合です。
12
12
  Full Joinを使わない場合は、補集合をunion ALL します。
13
13
  ※補集合なので重なりませんからunion allします。
14
14
  ```SQL

3

推敲

2020/09/26 07:50

投稿

sazi
sazi

スコア25430

answer CHANGED
@@ -6,8 +6,9 @@
6
6
  質問のSQLでは、Full joinが使用できない場合の、全外部結合の組み立てができていませんね。
7
7
 
8
8
  請求書→見積書の参照しかできないみたいですので、
9
- 請求書+関連する見積書:請求書→(lefto join)→見積書
9
+ A.請求書+関連する見積書:請求書→(lefto join)→見積書
10
- 見積書のみ:見積書→(lefto join)→請求書で請求書が無いもの
10
+ B.見積書のみ:見積書→(lefto join)→請求書で請求書が無いもの
11
+ それぞれが全体の補集合です。
11
12
  Full Joinを使わない場合は、補集合をunion ALL します。
12
13
  ※補集合なので重なりませんからunion allします。
13
14
  ```SQL

2

推敲

2020/09/26 07:46

投稿

sazi
sazi

スコア25430

answer CHANGED
@@ -8,9 +8,8 @@
8
8
  請求書→見積書の参照しかできないみたいですので、
9
9
  ・請求書+関連する見積書:請求書→(lefto join)→見積書
10
10
  ・見積書のみ:見積書→(lefto join)→請求書で請求書が無いもの
11
- ※上記は背反ので重なりせん
11
+ Full Joinを使わい場合は、補集合をunion ALL し
12
-
13
- 上記をunion allします。
12
+ ※補集合なので重なりませんからunion allします。
14
13
  ```SQL
15
14
  select invoice.estimate_id, invoice.invoice_id, invoice.customer_id
16
15
  from invoice

1

推敲

2020/09/26 07:43

投稿

sazi
sazi

スコア25430

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