-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathJOIN.html
1111 lines (699 loc) · 46.4 KB
/
JOIN.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<!DOCTYPE HTML>
<html lang="ko" >
<head>
<!-- Global site tag (gtag.js) - Google Analytics -->
<script async src="https://www.googletagmanager.com/gtag/js?id=UA-161001174-3"></script>
<script>
window.dataLayer = window.dataLayer || [];
function gtag(){dataLayer.push(arguments);}
gtag('js', new Date());
gtag('config', 'UA-161001174-3');
</script>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
<title>JOIN | Introduction</title>
<meta content="text/html; charset=utf-8" http-equiv="Content-Type">
<meta name="description" content="">
<meta name="generator" content="GitBook 2.6.7">
<meta name="HandheldFriendly" content="true"/>
<meta name="viewport" content="width=device-width, initial-scale=1, user-scalable=no">
<meta name="apple-mobile-web-app-capable" content="yes">
<meta name="apple-mobile-web-app-status-bar-style" content="black">
<link rel="apple-touch-icon-precomposed" sizes="152x152" href="gitbook/images/apple-touch-icon-precomposed-152.png">
<link rel="shortcut icon" href="gitbook/images/favicon.ico" type="image/x-icon">
<link rel="stylesheet" href="gitbook/style.css">
<link rel="stylesheet" href="gitbook/plugins/gitbook-plugin-highlight/website.css">
<link rel="stylesheet" href="gitbook/plugins/gitbook-plugin-search/search.css">
<link rel="stylesheet" href="gitbook/plugins/gitbook-plugin-fontsettings/website.css">
<link rel="next" href="./subquery.html" />
<link rel="prev" href="./GROUP_BY.html" />
</head>
<body>
<div class="book"
data-level="9.3"
data-chapter-title="JOIN"
data-filepath="JOIN.md"
data-basepath="."
data-innerlanguage="">
<div class="book-summary">
<nav role="navigation">
<ul class="summary">
<li class="chapter " data-level="0" data-path="index.html">
<a href="./index.html">
<i class="fa fa-check"></i>
Introduction
</a>
</li>
<li class="chapter " data-level="1" data-path="bug_reports.html">
<a href="./bug_reports.html">
<i class="fa fa-check"></i>
<b>1.</b>
Bug reports
</a>
</li>
<li class="chapter " data-level="2" data-path="basics.html">
<a href="./basics.html">
<i class="fa fa-check"></i>
<b>2.</b>
Basics
</a>
<ul class="articles">
<li class="chapter " data-level="2.1" data-path="data_types.html">
<a href="./data_types.html">
<i class="fa fa-check"></i>
<b>2.1.</b>
Data Types
</a>
</li>
<li class="chapter " data-level="2.2" data-path="variables.html">
<a href="./variables.html">
<i class="fa fa-check"></i>
<b>2.2.</b>
Variables
</a>
</li>
<li class="chapter " data-level="2.3" data-path="regex.html">
<a href="./regex.html">
<i class="fa fa-check"></i>
<b>2.3.</b>
Regex
</a>
</li>
<li class="chapter " data-level="2.4" data-path="functions.html">
<a href="./functions.html">
<i class="fa fa-check"></i>
<b>2.4.</b>
Functions
</a>
</li>
<li class="chapter " data-level="2.5" data-path="escape_sequences.html">
<a href="./escape_sequences.html">
<i class="fa fa-check"></i>
<b>2.5.</b>
Escape sequences
</a>
</li>
<li class="chapter " data-level="2.6" data-path="begin_end.html">
<a href="./begin_end.html">
<i class="fa fa-check"></i>
<b>2.6.</b>
BEGIN, END
</a>
</li>
</ul>
</li>
<li class="chapter " data-level="3" data-path="builtin_variables.html">
<a href="./builtin_variables.html">
<i class="fa fa-check"></i>
<b>3.</b>
Built-in Variables
</a>
</li>
<li class="chapter " data-level="4" data-path="record_sep.html">
<a href="./record_sep.html">
<i class="fa fa-check"></i>
<b>4.</b>
Record 분리
</a>
</li>
<li class="chapter " data-level="5" data-path="field_sep.html">
<a href="./field_sep.html">
<i class="fa fa-check"></i>
<b>5.</b>
Field 분리
</a>
</li>
<li class="chapter " data-level="6" data-path="zero_one.html">
<a href="./zero_one.html">
<i class="fa fa-check"></i>
<b>6.</b>
$0=$0, $1=$1
</a>
</li>
<li class="chapter " data-level="7" data-path="redirection.html">
<a href="./redirection.html">
<i class="fa fa-check"></i>
<b>7.</b>
Redirection
</a>
</li>
<li class="chapter " data-level="8" data-path="getline.html">
<a href="./getline.html">
<i class="fa fa-check"></i>
<b>8.</b>
getline
</a>
</li>
<li class="chapter " data-level="9" data-path="arrays.html">
<a href="./arrays.html">
<i class="fa fa-check"></i>
<b>9.</b>
Arrays
</a>
<ul class="articles">
<li class="chapter " data-level="9.1" data-path="sort.html">
<a href="./sort.html">
<i class="fa fa-check"></i>
<b>9.1.</b>
Sort
</a>
</li>
<li class="chapter " data-level="9.2" data-path="GROUP_BY.html">
<a href="./GROUP_BY.html">
<i class="fa fa-check"></i>
<b>9.2.</b>
GROUP BY
</a>
</li>
<li class="chapter active" data-level="9.3" data-path="JOIN.html">
<a href="./JOIN.html">
<i class="fa fa-check"></i>
<b>9.3.</b>
JOIN
</a>
</li>
<li class="chapter " data-level="9.4" data-path="subquery.html">
<a href="./subquery.html">
<i class="fa fa-check"></i>
<b>9.4.</b>
Subquery
</a>
</li>
<li class="chapter " data-level="9.5" data-path="index2.html">
<a href="./index2.html">
<i class="fa fa-check"></i>
<b>9.5.</b>
Index
</a>
</li>
</ul>
</li>
<li class="chapter " data-level="10" data-path="control_statements.html">
<a href="./control_statements.html">
<i class="fa fa-check"></i>
<b>10.</b>
Control Statements
</a>
</li>
<li class="chapter " data-level="11" data-path="operators.html">
<a href="./operators.html">
<i class="fa fa-check"></i>
<b>11.</b>
Operators
</a>
</li>
<li class="chapter " data-level="12" data-path="builtin_functions.html">
<a href="./builtin_functions.html">
<i class="fa fa-check"></i>
<b>12.</b>
Built-in Functions
</a>
<ul class="articles">
<li class="chapter " data-level="12.1" data-path="builtin/numeric.html">
<a href="./builtin/numeric.html">
<i class="fa fa-check"></i>
<b>12.1.</b>
Numeric Functions
</a>
</li>
<li class="chapter " data-level="12.2" data-path="builtin/string.html">
<a href="./builtin/string.html">
<i class="fa fa-check"></i>
<b>12.2.</b>
String Functions
</a>
</li>
<li class="chapter " data-level="12.3" data-path="builtin/io.html">
<a href="./builtin/io.html">
<i class="fa fa-check"></i>
<b>12.3.</b>
I/O Functions
</a>
</li>
<li class="chapter " data-level="12.4" data-path="builtin/time.html">
<a href="./builtin/time.html">
<i class="fa fa-check"></i>
<b>12.4.</b>
Time Functions
</a>
</li>
<li class="chapter " data-level="12.5" data-path="builtin/bitwise.html">
<a href="./builtin/bitwise.html">
<i class="fa fa-check"></i>
<b>12.5.</b>
Bitwise Functions
</a>
</li>
<li class="chapter " data-level="12.6" data-path="builtin/type.html">
<a href="./builtin/type.html">
<i class="fa fa-check"></i>
<b>12.6.</b>
Type Functions
</a>
</li>
</ul>
</li>
<li class="chapter " data-level="13" data-path="extensions.html">
<a href="./extensions.html">
<i class="fa fa-check"></i>
<b>13.</b>
Dynamic Extensions
</a>
<ul class="articles">
<li class="chapter " data-level="13.1" data-path="example.html">
<a href="./example.html">
<i class="fa fa-check"></i>
<b>13.1.</b>
예제
</a>
</li>
</ul>
</li>
<li class="chapter " data-level="14" data-path="tcp_ip.html">
<a href="./tcp_ip.html">
<i class="fa fa-check"></i>
<b>14.</b>
TCP/IP Internetworking
</a>
</li>
<li class="chapter " data-level="15" data-path="command_line_options.html">
<a href="./command_line_options.html">
<i class="fa fa-check"></i>
<b>15.</b>
Command-line options
</a>
</li>
<li class="chapter " data-level="16" data-path="debugging.html">
<a href="./debugging.html">
<i class="fa fa-check"></i>
<b>16.</b>
Debugging
</a>
</li>
<li class="chapter " data-level="17" data-path="tips.html">
<a href="./tips.html">
<i class="fa fa-check"></i>
<b>17.</b>
Tips
</a>
</li>
<li class="divider"></li>
<li>
<a href="https://www.gitbook.com" target="blank" class="gitbook-link">
</a>
</li>
</ul>
</nav>
</div>
<div class="book-body">
<div class="body-inner">
<div class="book-header" role="navigation">
<!-- Actions Left -->
<!-- Title -->
<h1>
<i class="fa fa-circle-o-notch fa-spin"></i>
<a href="./" >Introduction</a>
</h1>
</div>
<div class="page-wrapper" tabindex="-1" role="main">
<div class="page-inner">
<section class="normal" id="section-">
<h1 id="join">JOIN</h1>
<p><code>NR == FNR</code> 는 입력으로 두 개의 파일을 다룰 때 간단히 사용되는 방법입니다.
<code>FNR</code> 값은 입력 파일이 변경될 때마다 <code>0</code> 으로 reset 되어 다시 시작하고
<code>NR</code> 값은 계속해서 누적되므로 결과적으로 <code>NR == FNR</code> 가 되는 경우는 처음 파일에만 해당됩니다.
따라서 <code>NR == FNR { a[$0]; next }</code> 는 첫 번째 파일을 처리하기 위한 부분으로
레코드 값을 index 로 하는 array 를 생성합니다.
뒤에 next 가 있으므로 이후의 코드는 실행되지 않고 계속해서 다음 레코드로 이동하겠죠.
<code>$0 in a</code> 는 <code>a</code> 라는 array 에 index 값으로 <code>$0</code> 가 있는지 조회하는 방법이므로
결국 file1, file2 두 파일의 공통 라인에 대해서만 참이 되게 됩니다.</p>
<pre><code class="lang-bash"><span class="hljs-comment"># 라인 111, 222, 333 은 file1, file2 공통</span>
$ cat file1 $ cat file2
AAA XXX
<span class="hljs-number">111</span> <span class="hljs-number">111</span>
<span class="hljs-number">222</span> <span class="hljs-number">222</span>
BBB <span class="hljs-number">333</span>
CCC YYY
<span class="hljs-number">333</span> ZZZ
....................................................
<span class="hljs-comment"># file1 와 file2 공통 라인 프린트 (intersection)</span>
$ awk <span class="hljs-string">'NR == FNR{ a[$1]; next } $1 in a'</span> file1 file2
<span class="hljs-number">111</span>
<span class="hljs-number">222</span>
<span class="hljs-number">333</span>
<span class="hljs-comment"># file1 에만 있는 라인 프린트 (file2 file1 순서 주의)</span>
$ awk <span class="hljs-string">'NR == FNR{ a[$1]; next } !($1 in a)'</span> file2 file1
AAA
BBB
CCC
<span class="hljs-comment"># file2 에만 있는 라인 프린트 (file1 file2 순서 주의)</span>
$ awk <span class="hljs-string">'NR == FNR{ a[$1]; next } !($1 in a)'</span> file1 file2
XXX
YYY
ZZZ
</code></pre>
<p>JOIN 은 data 파일과 map 파일에 공통적으로 존재하는 키값을 이용해 일종의 치환 작업을 합니다.
map 파일에 존재하는 키값에 한에서 data 파일에서 키가 사용되면 상관없는데
그렇지 않고 map 파일에 존재하지 않는 키가 data 파일에서 사용되면
여기서 LEFT JOIN 과 INNER JOIN 의 차이가 생깁니다.
LEFT JOIN 은 map 파일에 해당 키값이 없을 경우 해당 컬럼 값을 공백으로 두고 나머지 정보들을 표시하는데 반해
INNER JOIN 은 아무런 정보도 표시하지 않습니다.</p>
<pre><code class="lang-bash">$ cat data
<span class="hljs-number">20081010</span> <span class="hljs-number">123</span> xxx
<span class="hljs-number">20081011</span> <span class="hljs-number">234</span> def
<span class="hljs-number">20081012</span> <span class="hljs-number">512</span> xyz
<span class="hljs-number">20081013</span> <span class="hljs-number">512</span> abc
<span class="hljs-number">20081014</span> <span class="hljs-number">717</span> def
<span class="hljs-number">20081015</span> <span class="hljs-number">999</span> zzz <--- map 파일에는 없는 키
$ cat map
abc withdrawal
def payment
xyz deposit
xxx balance
........................................
<span class="hljs-comment"># SELECT d.$1, d.$2, m.$2 FROM data d</span>
<span class="hljs-comment"># LEFT JOIN map m ON d.$3 = m.$1</span>
$ awk <span class="hljs-string">'NR == FNR{ a[$1]=$2; next } { print $1,$2,a[$3] }'</span> map data
<span class="hljs-number">20081010</span> <span class="hljs-number">123</span> balance
<span class="hljs-number">20081011</span> <span class="hljs-number">234</span> payment
<span class="hljs-number">20081012</span> <span class="hljs-number">512</span> deposit
<span class="hljs-number">20081013</span> <span class="hljs-number">512</span> withdrawal
<span class="hljs-number">20081014</span> <span class="hljs-number">717</span> payment
<span class="hljs-number">20081015</span> <span class="hljs-number">999</span> <---- LEFT JOIN 결과
<span class="hljs-comment"># SELECT d.$1, d.$2, m.$2 FROM data d</span>
<span class="hljs-comment"># INNER JOIN map m ON d.$3 = m.$1</span>
$ awk <span class="hljs-string">'NR == FNR{ a[$1]=$2; next }
$3 in a { print $1,$2,a[$3] } # INNER JOIN 을 위한 키값 체크
'</span> map data
<span class="hljs-number">20081010</span> <span class="hljs-number">123</span> balance
<span class="hljs-number">20081011</span> <span class="hljs-number">234</span> payment
<span class="hljs-number">20081012</span> <span class="hljs-number">512</span> deposit
<span class="hljs-number">20081013</span> <span class="hljs-number">512</span> withdrawal
<span class="hljs-number">20081014</span> <span class="hljs-number">717</span> payment
</code></pre>
<p>앞서 사용한 <code>data</code>, <code>map</code> 파일에 이어 <code>map2</code> 파일을 추가하여 두 개의 파일을 JOIN 합니다.
먼저 map, map2 두 개의 파일을 array 로 읽어들여야 하는데 <code>NR == FNR</code> 는 첫 번째 파일에만
적용되므로 여기서는 사용할 수 없고 BEGIN 블록에서 getline 을 이용해 직접 읽어들입니다.</p>
<pre><code class="lang-bash">$ cat map2
<span class="hljs-number">123</span> spring
<span class="hljs-number">234</span> summer
<span class="hljs-number">512</span> autumn
<span class="hljs-number">717</span> winter
...............................................
<span class="hljs-comment"># SELECT d.$1, m2.$2, m.$2 FROM data d</span>
<span class="hljs-comment"># LEFT JOIN map m ON d.$3 = m.$1</span>
<span class="hljs-comment"># LEFT JOIN map2 m2 ON d.$2 = m2.$1</span>
$ awk <span class="hljs-string">'BEGIN {
while (getline < ARGV[1] > 0) m[$1] = $2
while (getline < ARGV[2] > 0) m2[$1] = $2
while (getline < ARGV[3] > 0) {
print $1,m2[$2],m[$3]
}
}'</span> map map2 data
<span class="hljs-number">20081010</span> spring balance
<span class="hljs-number">20081011</span> summer payment
<span class="hljs-number">20081012</span> autumn deposit
<span class="hljs-number">20081013</span> autumn withdrawal
<span class="hljs-number">20081014</span> winter payment
<span class="hljs-number">20081015</span> <---- LEFT JOIN 결과
<span class="hljs-comment"># SELECT d.$1, m2.$2, m.$2 FROM data d</span>
<span class="hljs-comment"># INNER JOIN map m ON d.$3 = m.$1</span>
<span class="hljs-comment"># INNER JOIN map2 m2 ON d.$2 = m2.$1</span>
$ awk <span class="hljs-string">'BEGIN {
while (getline < ARGV[1] > 0) m[$1] = $2
while (getline < ARGV[2] > 0) m2[$1] = $2
while (getline < ARGV[3] > 0) {
if (( $2 in m2 ) && ( $3 in m )) { # INNER JOIN 을 위한 키값 체크
print $1,m2[$2],m[$3]
}
}
}'</span> map map2 data
<span class="hljs-number">20081010</span> spring balance
<span class="hljs-number">20081011</span> summer payment
<span class="hljs-number">20081012</span> autumn deposit
<span class="hljs-number">20081013</span> autumn withdrawal
<span class="hljs-number">20081014</span> winter payment
</code></pre>
<h4 id="group-by-와-join-을-함께-사용">GROUP BY 와 JOIN 을 함께 사용</h4>
<pre><code class="lang-bash"><span class="hljs-comment"># 사용할땐 데이터 이외의 헤더 부분은 삭제해야 합니다.</span>
$ cat Shippers
ShipperID ShipperName Phone
----------+-----------------+---------------
<span class="hljs-number">1</span> , Speedy Express , (<span class="hljs-number">503</span>) <span class="hljs-number">555</span>-<span class="hljs-number">9831</span>
<span class="hljs-number">2</span> , United Package , (<span class="hljs-number">503</span>) <span class="hljs-number">555</span>-<span class="hljs-number">3199</span>
<span class="hljs-number">3</span> , Federal Shipping , (<span class="hljs-number">503</span>) <span class="hljs-number">555</span>-<span class="hljs-number">9931</span>
$ cat Orders
OrderID CustomerID EmployeeID OrderDate ShipperID
--------+-----------+----------+-----------+----------
<span class="hljs-number">10248</span> <span class="hljs-number">90</span> <span class="hljs-number">5</span> <span class="hljs-number">1996</span>-<span class="hljs-number">07</span>-<span class="hljs-number">04</span> <span class="hljs-number">3</span>
<span class="hljs-number">10249</span> <span class="hljs-number">81</span> <span class="hljs-number">6</span> <span class="hljs-number">1996</span>-<span class="hljs-number">07</span>-<span class="hljs-number">05</span> <span class="hljs-number">1</span>
<span class="hljs-number">10250</span> <span class="hljs-number">34</span> <span class="hljs-number">4</span> <span class="hljs-number">1996</span>-<span class="hljs-number">07</span>-<span class="hljs-number">08</span> <span class="hljs-number">2</span>
<span class="hljs-number">10251</span> <span class="hljs-number">84</span> <span class="hljs-number">3</span> <span class="hljs-number">1996</span>-<span class="hljs-number">07</span>-<span class="hljs-number">08</span> <span class="hljs-number">1</span>
<span class="hljs-number">10252</span> <span class="hljs-number">76</span> <span class="hljs-number">4</span> <span class="hljs-number">1996</span>-<span class="hljs-number">07</span>-<span class="hljs-number">09</span> <span class="hljs-number">2</span>
<span class="hljs-number">10253</span> <span class="hljs-number">34</span> <span class="hljs-number">3</span> <span class="hljs-number">1996</span>-<span class="hljs-number">07</span>-<span class="hljs-number">10</span> <span class="hljs-number">2</span>
<span class="hljs-number">10254</span> <span class="hljs-number">14</span> <span class="hljs-number">5</span> <span class="hljs-number">1996</span>-<span class="hljs-number">07</span>-<span class="hljs-number">11</span> <span class="hljs-number">2</span>
<span class="hljs-comment">##################### SQL 문 #######################</span>
SELECT S.ShipperName, COUNT(O.OrderID) AS NumberOfOrders
FROM Orders O
LEFT JOIN Shippers S ON O.ShipperID = S.ShipperID
GROUP BY ShipperName
ORDER BY NumberOfOrders
ShipperName NumberOfOrders
---------------- --------------
Federal Shipping <span class="hljs-number">1</span>
Speedy Express <span class="hljs-number">2</span>
United Package <span class="hljs-number">4</span>
<span class="hljs-comment">###################### AWK ##########################</span>
<span class="hljs-comment"># $1 : Shippers.ShipperID</span>
<span class="hljs-comment"># $2 : Shippers.ShipperName</span>
<span class="hljs-comment"># $5 : Orders.ShipperID</span>
$ awk <span class="hljs-string">'
NR == FNR{ s[$1]=$2; next }
{ o[$5]++ }
END {
PROCINFO["sorted_in"] = "@val_num_asc"
for ( i in o ) { printf "%-20s %d\n", s[i], o[i] }
}
'</span> FS=<span class="hljs-string">' *, *'</span> Shippers FS=<span class="hljs-string">' '</span> Orders
Federal Shipping <span class="hljs-number">1</span>
Speedy Express <span class="hljs-number">2</span>
United Package <span class="hljs-number">4</span>
</code></pre>
<h3 id="cross-join">CROSS JOIN</h3>
<p>CROSS JOIN 은 <a href="https://en.wikipedia.org/wiki/Cartesian_product" target="_blank">Cartesian product</a> 라고도 하는데
두 집합 A, B에 대해 a∈A이고 b∈B인 모든 순서쌍(a, b)의 집합을 말합니다.
그러니까 만약에 테이블 A 에 10 개의 row 가 있고 테이블 B 에 10 개의 row 가 있으면
CROSS JOIN 을 하게 되면 모두 10 X 10 = 100 개의 row 가 생기게 됩니다.
joining everything to everything 이기 때문에 따로 ON 절이 없고 만약에
테이블 하나가 empty 면 result set 은 0 이 됩니다.</p>
<p>이것이 어디에 필요한지 예제를 통해 알아보면 먼저 아래와 같은 테이블이 있을 경우
"각 Store 에서 판매된 Product 별로 Amount 를 SUM 해서 보여달라"
라고 하면 간단히 GROUP BY 를 하면 됩니다.</p>
<pre><code class="lang-bash">$ cat sales $ cat stores $ cat products
Store Product Amount Store Product
-------+---------+------- ----- -------
lg24 snack <span class="hljs-number">300</span> lg24 milk
lg24 snack <span class="hljs-number">200</span> du bread
lg24 soda <span class="hljs-number">150</span> <span class="hljs-number">7</span>ele soda
lg24 soda <span class="hljs-number">250</span> snack
du milk <span class="hljs-number">150</span>
du bread <span class="hljs-number">100</span>
du bread <span class="hljs-number">200</span>
<span class="hljs-number">7</span>ele snack <span class="hljs-number">100</span>
<span class="hljs-number">7</span>ele milk <span class="hljs-number">150</span>
<span class="hljs-number">7</span>ele bread <span class="hljs-number">200</span>
<span class="hljs-number">7</span>ele bread <span class="hljs-number">150</span>
.......................................................................
SELECT Store, Product, SUM(Amount) as Sum
FROM sales
GROUP BY Store, Product
.........................................
$ awk <span class="hljs-string">'
{ a[$1,$2] += $3 }
END {
PROCINFO["sorted_in"] = "@ind_str_asc"
for ( i in a ) {
split(i, b, SUBSEP)
printf "%-10s %-10s %5d\n", b[1], b[2], a[i]
}
}'</span> sales
<span class="hljs-number">7</span>ele bread <span class="hljs-number">350</span>
<span class="hljs-number">7</span>ele milk <span class="hljs-number">150</span>
<span class="hljs-number">7</span>ele snack <span class="hljs-number">100</span>
du bread <span class="hljs-number">300</span>
du milk <span class="hljs-number">150</span>
lg24 snack <span class="hljs-number">500</span>
lg24 soda <span class="hljs-number">400</span>
</code></pre>
<p>그런데 "어떤 상점에서 어떤 품목이 판매가 안됐는지 알 수 없으니
그런 품목에 대해서는 0 으로 표시해달라"
라고 할 경우엔 모든 상점에서 모든 품목을 표시해야 하므로
이때는 CROSS JOIN 을 해야 합니다.</p>
<pre><code class="lang-bash">SELECT S.Store, P.Product
FROM stores S
CROSS JOIN products P
..........................
SELECT S.Store, P.Product, ISNULL(C.Sum,<span class="hljs-number">0</span>) as Sum
FROM stores S
CROSS JOIN products P
LEFT JOIN
(SELECT Store, Product, SUM(Amount) as Sum
FROM sales
GROUP BY Store, Product) C
ON
S.Store = C.Store AND
P.Product = C.Product
................................................................
$ awk <span class="hljs-string">'BEGIN {
while (getline < ARGV[1] > 0) s[$1]
while (getline < ARGV[2] > 0) p[$1]
while (getline < ARGV[3] > 0) a[$1,$2] += $3
PROCINFO["sorted_in"] = "@ind_str_asc"
for ( i in s ) { # CROSS JOIN 을 위한 이중 for 문
for ( j in p ) {
printf "%-10s %-10s %5d\n", i, j, ( a[i,j] ? a[i,j] : 0 )
}
}
}'</span> stores products sales
<span class="hljs-number">7</span>ele bread <span class="hljs-number">350</span>
<span class="hljs-number">7</span>ele milk <span class="hljs-number">150</span>
<span class="hljs-number">7</span>ele snack <span class="hljs-number">100</span>
<span class="hljs-number">7</span>ele soda <span class="hljs-number">0</span>
du bread <span class="hljs-number">300</span>
du milk <span class="hljs-number">150</span>
du snack <span class="hljs-number">0</span>
du soda <span class="hljs-number">0</span>
lg24 bread <span class="hljs-number">0</span>
lg24 milk <span class="hljs-number">0</span>
lg24 snack <span class="hljs-number">500</span>
lg24 soda <span class="hljs-number">400</span>
</code></pre>
<p><a name="full-join"></a></p>
<h3 id="full-join-exclusive">FULL JOIN (exclusive)</h3>
<p>다음은 file1, file2 의 공통부분을 제외하고 각 파일에만 존재하는 데이터를 출력합니다.</p>
<pre><code class="lang-bash">SELECT *
FROM file1 f1 FULL JOIN file f2
ON f1.value = f2.value
WHERE f1.value IS NULL OR f2.value IS NULL
..........................................
$ cat file1 $ cat file2
AAA XXX
<span class="hljs-number">111</span> <span class="hljs-number">111</span>
<span class="hljs-number">222</span> <span class="hljs-number">222</span>
BBB <span class="hljs-number">333</span>
CCC YYY
<span class="hljs-number">333</span>
............................
$ awk <span class="hljs-string">'BEGIN {
while (getline < ARGV[1] > 0) a[$1]=$1
while (getline < ARGV[2] > 0) b[$1]=$1
for (i in a) if ( !b[i] ) print i
for (i in b) if ( !a[i] ) print i
}'</span> file1 file2
AAA
CCC
BBB
XXX
YYY
</code></pre>
<p>다음은 FULL JOIN 을 이용해 local directory 와 remote directory 를 비교하는 예입니다.
remote 파일을 비교하는 것이므로 파일을 다운로드하지 않고 md5sum 을 이용해 비교합니다.
local 과 remote 파일의 md5sum 값이 다르다는 것은 파일이 수정되었다는 의미고
아래 libtlpi.a 파일은 remote 에만 존재하는 파일이 됩니다.</p>
<blockquote>
<p>이 방법은 md5sum 값만을 이용해 비교하는 것으로 동일한 내용의 파일이 있을 경우
( 예를 들면 <code>cp</code> 에 의해 생성된 ) 는 비교되지 않습니다.
또한 hash collision 이 있을 수 있습니다. ( md5sum 을 sha1sum 이나 이상으로 변경해보세요 )</p>
</blockquote>
<pre><code class="lang-bash">$ awk <span class="hljs-string">'BEGIN {
while (getline < ARGV[1] > 0) a[$1]=$0
while (getline < ARGV[2] > 0) b[$1]=$0
for (i in a) if ( !b[i] ) print a[i]
for (i in b) if ( !a[i] ) print b[i]
}'</span> <( find BUILD -type f -exec md5sum {} + ) \
<( ssh user@remote <span class="hljs-string">'find path/to/remote/BUILD -type f -exec md5sum {} +'</span> )
aeabb93eddf837d8dd45cbf94d0ef7d2 BUILD/pty_master_open.d
e507a57525fabe57695683442ecde7e3 BUILD/pty_fork.o