-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathGROUP_BY.html
1012 lines (616 loc) · 48.2 KB
/
GROUP_BY.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>GROUP BY | 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="./JOIN.html" />
<link rel="prev" href="./sort.html" />
</head>
<body>
<div class="book"
data-level="9.2"
data-chapter-title="GROUP BY"
data-filepath="GROUP_BY.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 active" 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 " 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="group-by">GROUP BY</h1>
<p>array index 를 활용하면 group by 연산을 할 수 있습니다.
왜 그렇게 되는지 살펴보면 array <code>a</code> 의 index 값 $1 이 BBB 일 경우 a[BBB] 가 되는데
이때는 index 값으로 BBB 를 가지는 array 원소가 하나 생기는 것입니다.
value 값을 대입하지 않았기 때문에 value 는 null 이 되구요.
다음에 $1 값으로 BBB 가 다시 나타나면 a[BBB] 가 되어
index 값이 중복되므로 결과적으로 하나만 남게 됩니다.</p>
<p>두 번째 예제에서처럼 <code>a[$1]++</code> 을 하게 되면 이때는 a[BBB]++ 가되어
처음에 a[BBB] 원소가 생성됨과 동시에 value 가 1 이 됩니다.
다음에 $1 값으로 BBB 가 중복되어 나타나면 다시 a[BBB]++ 가 되므로
a[BBB] 의 value 가 +1 되어 2 가 됩니다.</p>
<pre><code class="lang-bash">$ cat file
AAA
EEE
BBB
CCC
DDD
DDD
BBB
BBB
...................................................
<span class="hljs-comment"># select $1 from file group by $1 </span>
$ awk <span class="hljs-string">'{ a[$1] } END { for (i in a) print i }'</span> file
AAA
CCC
EEE
BBB
DDD
<span class="hljs-comment"># select $1, count($1) from file group by $1 </span>
$ awk <span class="hljs-string">'{ a[$1]++ } END { for (i in a) print i " : " a[i] }'</span> file
AAA : <span class="hljs-number">1</span>
CCC : <span class="hljs-number">1</span>
EEE : <span class="hljs-number">1</span>
BBB : <span class="hljs-number">3</span>
DDD : <span class="hljs-number">2</span>
<span class="hljs-comment"># select distinct($1) from file</span>
$ awk <span class="hljs-string">'!a[$1]++'</span> file
AAA
EEE
BBB
CCC
DDD
<span class="hljs-comment"># select $1, count($1) as c from file group by $1 having c >= 2</span>
$ awk <span class="hljs-string">'++a[$1] == 2'</span> file <span class="hljs-comment"># 중복라인을 가지고 있는 라인</span>
DDD
BBB
</code></pre>
<p>낱말 출현 빈도 계산 후 정렬하기</p>
<pre><code class="lang-bash"><span class="hljs-comment"># '.' 와 ',' 문자가 낱말에 포함되는 것을 방지하기 위해 RS 로 설정</span>
$ awk <span class="hljs-operator">-f</span> - <<\EOF RS=<span class="hljs-string">'\\.|,'</span> song.txt
{
<span class="hljs-keyword">for</span> (i=<span class="hljs-number">1</span>; i<=NF; i++)
words[<span class="hljs-variable">$i</span>]++
}
END {
PROCINFO[<span class="hljs-string">"sorted_in"</span>] = <span class="hljs-string">"compare"</span>
<span class="hljs-keyword">for</span> (i <span class="hljs-keyword">in</span> words)
<span class="hljs-built_in">printf</span> <span class="hljs-string">"%s %s\n"</span>, i, words[i]
}
<span class="hljs-comment"># 이 비교 함수는 awk 명령 출력을 "| LC_ALL=C sort -k2,2r -k1,1" 한 것과 같습니다.</span>
<span class="hljs-comment"># ORDER BY count DESC, word ASC</span>
<span class="hljs-keyword">function</span> compare(i1, v1, i2, v2)
{
<span class="hljs-keyword">if</span> (v1 > v2) <span class="hljs-built_in">return</span> -<span class="hljs-number">1</span> <span class="hljs-comment"># 출현 빈도를 DESC 정렬하는 부분</span>
<span class="hljs-keyword">else</span> <span class="hljs-keyword">if</span> (v1 == v2)
{ <span class="hljs-comment"># { ... } 부분이 낱말을 ASC 정렬하는 부분</span>
<span class="hljs-keyword">if</span> ( i1 < i2 ) <span class="hljs-built_in">return</span> -<span class="hljs-number">1</span>
<span class="hljs-keyword">else</span> <span class="hljs-keyword">if</span> ( i1 == i2 ) <span class="hljs-built_in">return</span> <span class="hljs-number">0</span>
<span class="hljs-keyword">else</span> <span class="hljs-built_in">return</span> <span class="hljs-number">1</span>
}
<span class="hljs-keyword">else</span> <span class="hljs-built_in">return</span> <span class="hljs-number">1</span>
}
EOF
< song.txt 파일 내용 >
길이 <span class="hljs-number">4</span>
대한 <span class="hljs-number">4</span> 동해물과 백두산이 마르고 닳도록
대한으로 <span class="hljs-number">4</span> 하느님이 보우하사 우리나라 만세.
무궁화 <span class="hljs-number">4</span> 무궁화 삼천리 화려강산
보전하세 <span class="hljs-number">4</span> 대한 사람, 대한으로 길이 보전하세.
사람 <span class="hljs-number">4</span>
삼천리 <span class="hljs-number">4</span> 남산 위에 저 소나무, 철갑을 두른 듯
화려강산 <span class="hljs-number">4</span> 바람서리 불변함은 우리 기상일세.
우리 <span class="hljs-number">2</span> 무궁화 삼천리 화려강산
이 <span class="hljs-number">2</span> 대한 사람, 대한으로 길이 보전하세.
가슴 <span class="hljs-number">1</span>
가을 <span class="hljs-number">1</span> 가을 하늘 공활한데 높고 구름 없이
공활한데 <span class="hljs-number">1</span> 밝은 달은 우리 가슴 일편단심일세.
괴로우나 <span class="hljs-number">1</span> 무궁화 삼천리 화려강산
구름 <span class="hljs-number">1</span> 대한 사람, 대한으로 길이 보전하세.
기상과 <span class="hljs-number">1</span>
기상일세 <span class="hljs-number">1</span> 이 기상과 이 맘으로 충성을 다하여
나라 <span class="hljs-number">1</span> 괴로우나 즐거우나 나라 사랑하세.
..... 무궁화 삼천리 화려강산
..... 대한 사람, 대한으로 길이 보전하세.
</code></pre>
<p>현재 각 cpu core 에 바운드된 스레드 개수 출력</p>
<pre><code class="lang-bash"><span class="hljs-comment"># ps 명령에서 psr 은 현재 스레드가 바운드된 cpu core 를 말하고</span>
<span class="hljs-comment"># 뒤에 '=' 는 ps 명령이 타이틀을 표시하지 않게 합니다.</span>
<span class="hljs-comment"># 출력 결과가 실제 코어 수 보다 많게 나오는 이유는 Hyper-Threading 때문입니다.</span>
$ watch -td -n1 <span class="hljs-string">"<span class="hljs-variable">$( cat <<\EOF
ps axH -o psr= |
awk '{a[$1]++} END {
for (i in a)</span> { printf "</span>cpu%-<span class="hljs-number">6</span>d<span class="hljs-string">",i }; print "</span><span class="hljs-string">";
for (i in a) { printf "</span>%-<span class="hljs-number">9</span>d<span class="hljs-string">", a[i] }; print "</span><span class="hljs-string">" }'
EOF
)"</span>
cpu0 cpu1 cpu2 cpu3 cpu4 cpu5 cpu6 cpu7
<span class="hljs-number">289</span> <span class="hljs-number">232</span> <span class="hljs-number">269</span> <span class="hljs-number">198</span> <span class="hljs-number">260</span> <span class="hljs-number">214</span> <span class="hljs-number">279</span> <span class="hljs-number">215</span>
</code></pre>
<h3 id="group-by-로-두-개-이상의-값을-사용">group by 로 두 개 이상의 값을 사용</h3>
<p>SUBSEP 을 사용하는 <code>arr[$1,$3,$5]</code> 형태를 이용하면 group by 로 두 개 이상의 값을 사용할 수 있습니다.
아래의 경우는 A, B 두 값을 group by 하기 위해 <code>a[$1,$2]</code> 를 사용하였습니다.</p>
<p>select 절에 <code>count(C)</code> 가 사용되었다면 입력 때는 <code>a[$1,$2]["C"]++</code>, 출력 때는 <code>a[$1,$2]["C"]</code>
을 사용하면 되는데요.
<code>count(distinct C)</code> 가 사용되었기 때문에 distinct 를 구현하기 위해 입력 때는 <code>a[$1,$2]["C"][$3]</code>, 출력 때는 <code>length(a[$1,$2]["C"])</code> 가 사용되었습니다.
만약에 <code>count(distinct C,D)</code> 를 한다고 하면 입력 때 <code>a[$1,$2]["C"][$3,$4]</code> 로 하면 되겠죠</p>
<pre><code class="lang-bash"><span class="hljs-comment"># awk 에서 데이터 파일을 사용할 때는 헤더 부분은 제외하세요</span>
<span class="hljs-comment">###################### 데이터 파일 내용 #######################</span>
A B C D E
---------------------- <span class="hljs-comment"># x x 와 z o 는 2번씩 중복되고 x x 는 C 값이 같고 z o 는 다르다</span>
x x e <span class="hljs-number">2</span> <span class="hljs-number">10</span> <span class="hljs-comment"># x x e</span>
y y g <span class="hljs-number">1</span> <span class="hljs-number">8</span>
z o e <span class="hljs-number">2</span> <span class="hljs-number">9</span> <span class="hljs-comment"># z o e</span>
o o q <span class="hljs-number">1</span> <span class="hljs-number">10</span>
p z e <span class="hljs-number">3</span> <span class="hljs-number">22</span>
x x e <span class="hljs-number">1</span> <span class="hljs-number">11</span> <span class="hljs-comment"># x x e</span>
z o a <span class="hljs-number">1</span> <span class="hljs-number">24</span> <span class="hljs-comment"># z o a</span>
y z b <span class="hljs-number">1</span> <span class="hljs-number">25</span>
<span class="hljs-comment">#################### SQL 문으로 나타낸 결과 ####################</span>
select A,
B,
count(distinct C),
sum(D),
sum(<span class="hljs-keyword">case</span> when E><span class="hljs-number">20</span> <span class="hljs-keyword">then</span> E <span class="hljs-keyword">else</span> <span class="hljs-number">0</span> END)
from <span class="hljs-built_in">test</span>
group by A,B
A B count(distinct C) sum(D) sum(<span class="hljs-keyword">case</span> when E><span class="hljs-number">20</span> <span class="hljs-keyword">then</span> E <span class="hljs-keyword">else</span> <span class="hljs-number">0</span> END)
-------------------------------------------------------
o o <span class="hljs-number">1</span> <span class="hljs-number">1</span> <span class="hljs-number">0</span>
p z <span class="hljs-number">1</span> <span class="hljs-number">3</span> <span class="hljs-number">22</span>
x x <span class="hljs-number">1</span> <span class="hljs-number">3</span> <span class="hljs-number">0</span>
y y <span class="hljs-number">1</span> <span class="hljs-number">1</span> <span class="hljs-number">0</span>
y z <span class="hljs-number">1</span> <span class="hljs-number">1</span> <span class="hljs-number">25</span>
z o <span class="hljs-number">2</span> <span class="hljs-number">3</span> <span class="hljs-number">24</span>
<span class="hljs-comment">################################ AWK ####################################</span>
$ awk <span class="hljs-operator">-f</span> - <<\EOF OFS=<span class="hljs-string">'\t'</span> file
{ a[<span class="hljs-variable">$1</span>,<span class="hljs-variable">$2</span>][<span class="hljs-string">"C"</span>][<span class="hljs-variable">$3</span>]; a[<span class="hljs-variable">$1</span>,<span class="hljs-variable">$2</span>][<span class="hljs-string">"D"</span>] += <span class="hljs-variable">$4</span>; a[<span class="hljs-variable">$1</span>,<span class="hljs-variable">$2</span>][<span class="hljs-string">"E"</span>] += (<span class="hljs-variable">$5</span>><span class="hljs-number">20</span> ? <span class="hljs-variable">$5</span>:<span class="hljs-number">0</span>) }
END {
PROCINFO[<span class="hljs-string">"sorted_in"</span>] = <span class="hljs-string">"@ind_str_asc"</span>
<span class="hljs-keyword">for</span> ( i <span class="hljs-keyword">in</span> a ) {
split(i, b, SUBSEP)
<span class="hljs-built_in">print</span> b[<span class="hljs-number">1</span>], b[<span class="hljs-number">2</span>], length(a[i][<span class="hljs-string">"C"</span>]), a[i][<span class="hljs-string">"D"</span>], a[i][<span class="hljs-string">"E"</span>]
}
}
EOF
o o <span class="hljs-number">1</span> <span class="hljs-number">1</span> <span class="hljs-number">0</span>
p z <span class="hljs-number">1</span> <span class="hljs-number">3</span> <span class="hljs-number">22</span>
x x <span class="hljs-number">1</span> <span class="hljs-number">3</span> <span class="hljs-number">0</span>
y y <span class="hljs-number">1</span> <span class="hljs-number">1</span> <span class="hljs-number">0</span>
y z <span class="hljs-number">1</span> <span class="hljs-number">1</span> <span class="hljs-number">25</span>
z o <span class="hljs-number">2</span> <span class="hljs-number">3</span> <span class="hljs-number">24</span>
</code></pre>
<p><a name="ORDER_BY"></a></p>
<h3 id="order-by">ORDER BY</h3>
<p>array index 로 <code>a[$1,$2]</code> 가 사용되었기 때문에 <code>order by A, B</code> 를 하기 위해서는
간단히 awk 에서 기본적으로 제공하는 <code>@ind_str_asc</code> 을 사용하면 되었는데요.
만약에 <code>order by E</code> 를 하려면 어떻게 해야 될까요?
이렇게 다차원 배열이 사용되는 환경에서는 value 에 적용되는 <code>@val_num_asc</code> 을 사용할 수 없습니다.
이때는 사용자 정의 비교 함수를 이용해 order by 절을 구현해야 합니다.</p>
<p>출력에 <code>for ( i in a )</code> 문을 사용하므로 비교 함수에 전달되는 인수 값은
index 에 해당하는 i1, i2 값이 <code>i</code> 와 같게 되고 value 에 해당하는 v1, v2 값은 <code>a[i]</code> 와 같게됩니다.</p>
<pre><code class="lang-bash">select A,
B,
count(distinct C),
sum(D),
sum(<span class="hljs-keyword">case</span> when E><span class="hljs-number">20</span> <span class="hljs-keyword">then</span> E <span class="hljs-keyword">else</span> <span class="hljs-number">0</span> END) as E
from <span class="hljs-built_in">test</span>
group by A,B
order by E
.................................................
$ awk <span class="hljs-operator">-f</span> - <<\EOF OFS=<span class="hljs-string">'\t'</span> file
{ a[<span class="hljs-variable">$1</span>,<span class="hljs-variable">$2</span>][<span class="hljs-string">"C"</span>][<span class="hljs-variable">$3</span>]; a[<span class="hljs-variable">$1</span>,<span class="hljs-variable">$2</span>][<span class="hljs-string">"D"</span>] += <span class="hljs-variable">$4</span>; a[<span class="hljs-variable">$1</span>,<span class="hljs-variable">$2</span>][<span class="hljs-string">"E"</span>] += (<span class="hljs-variable">$5</span>><span class="hljs-number">20</span> ? <span class="hljs-variable">$5</span>:<span class="hljs-number">0</span>) }
END {
PROCINFO[<span class="hljs-string">"sorted_in"</span>] = <span class="hljs-string">"compare"</span>
<span class="hljs-keyword">for</span> ( i <span class="hljs-keyword">in</span> a ) {
split(i, b, SUBSEP)
<span class="hljs-built_in">print</span> b[<span class="hljs-number">1</span>], b[<span class="hljs-number">2</span>], length(a[i][<span class="hljs-string">"C"</span>]), a[i][<span class="hljs-string">"D"</span>], a[i][<span class="hljs-string">"E"</span>]
}
}
<span class="hljs-keyword">function</span> compare(i1, v1, i2, v2)
{
<span class="hljs-keyword">if</span> ( v1[<span class="hljs-string">"E"</span>] < v2[<span class="hljs-string">"E"</span>] ) <span class="hljs-built_in">return</span> -<span class="hljs-number">1</span>
<span class="hljs-keyword">else</span> <span class="hljs-keyword">if</span> ( v1[<span class="hljs-string">"E"</span>] == v2[<span class="hljs-string">"E"</span>] ) <span class="hljs-built_in">return</span> <span class="hljs-number">0</span>
<span class="hljs-keyword">else</span> <span class="hljs-built_in">return</span> <span class="hljs-number">1</span>
}
EOF
y y <span class="hljs-number">1</span> <span class="hljs-number">1</span> <span class="hljs-number">0</span>
o o <span class="hljs-number">1</span> <span class="hljs-number">1</span> <span class="hljs-number">0</span>
x x <span class="hljs-number">1</span> <span class="hljs-number">3</span> <span class="hljs-number">0</span>
p z <span class="hljs-number">1</span> <span class="hljs-number">3</span> <span class="hljs-number">22</span>
z o <span class="hljs-number">2</span> <span class="hljs-number">3</span> <span class="hljs-number">24</span>
y z <span class="hljs-number">1</span> <span class="hljs-number">1</span> <span class="hljs-number">25</span>
</code></pre>
<h3 id="having">HAVING</h3>
<p><code>having</code> 절을 구현하는 것은 비교적 간단합니다.
아래와 같이 if 문을 하나 추가하면 됩니다.</p>
<pre><code class="lang-bash">select A,
B,
count(distinct C),
sum(D) as D,
sum(<span class="hljs-keyword">case</span> when E><span class="hljs-number">20</span> <span class="hljs-keyword">then</span> E <span class="hljs-keyword">else</span> <span class="hljs-number">0</span> END) as E
from <span class="hljs-built_in">test</span>
group by A,B
having D > <span class="hljs-number">1</span>
order by E
.................................................
$ awk <span class="hljs-operator">-f</span> - <<\EOF OFS=<span class="hljs-string">'\t'</span> file
{ a[<span class="hljs-variable">$1</span>,<span class="hljs-variable">$2</span>][<span class="hljs-string">"C"</span>][<span class="hljs-variable">$3</span>]; a[<span class="hljs-variable">$1</span>,<span class="hljs-variable">$2</span>][<span class="hljs-string">"D"</span>] += <span class="hljs-variable">$4</span>; a[<span class="hljs-variable">$1</span>,<span class="hljs-variable">$2</span>][<span class="hljs-string">"E"</span>] += (<span class="hljs-variable">$5</span>><span class="hljs-number">20</span> ? <span class="hljs-variable">$5</span>:<span class="hljs-number">0</span>) }
END {
PROCINFO[<span class="hljs-string">"sorted_in"</span>] = <span class="hljs-string">"compare"</span>
<span class="hljs-keyword">for</span> ( i <span class="hljs-keyword">in</span> a ) {
<span class="hljs-keyword">if</span> ( a[i][<span class="hljs-string">"D"</span>] > <span class="hljs-number">1</span> ) { <span class="hljs-comment"># having 절을 위해 추가된 if 문</span>
split(i, b, SUBSEP)
<span class="hljs-built_in">print</span> b[<span class="hljs-number">1</span>], b[<span class="hljs-number">2</span>], length(a[i][<span class="hljs-string">"C"</span>]), a[i][<span class="hljs-string">"D"</span>], a[i][<span class="hljs-string">"E"</span>]
}
}
}
<span class="hljs-keyword">function</span> compare(i1, v1, i2, v2)
{
<span class="hljs-keyword">if</span> ( v1[<span class="hljs-string">"E"</span>] < v2[<span class="hljs-string">"E"</span>] ) <span class="hljs-built_in">return</span> -<span class="hljs-number">1</span>
<span class="hljs-keyword">else</span> <span class="hljs-keyword">if</span> ( v1[<span class="hljs-string">"E"</span>] == v2[<span class="hljs-string">"E"</span>] ) <span class="hljs-built_in">return</span> <span class="hljs-number">0</span>
<span class="hljs-keyword">else</span> <span class="hljs-built_in">return</span> <span class="hljs-number">1</span>
}
EOF
x x <span class="hljs-number">1</span> <span class="hljs-number">3</span> <span class="hljs-number">0</span>
p z <span class="hljs-number">1</span> <span class="hljs-number">3</span> <span class="hljs-number">22</span>
z o <span class="hljs-number">2</span> <span class="hljs-number">3</span> <span class="hljs-number">24</span>
</code></pre>
<h3 id="union">UNION</h3>
<p>UNION 은 order1 과 order2 의 항목들을 하나로 합칩니다.
이때 공통으로 존재하는 banana 와 strawberry 는 중복이 제거됩니다.
다음은 union 결과에 대해 order1 과 order2 에 존재 여부를 <code>"O"</code>, <code>"X"</code> 로 표시합니다.</p>
<pre><code class="lang-bash">$ cat order1 $ cat order2
apple melon
banana cherry
grape banana
strawberry strawberry
orange mango
tomato
-------------------------------------------------
$ awk <span class="hljs-operator">-f</span> - <<\EOF order1 order2
BEGIN {
<span class="hljs-keyword">while</span> (getline < ARGV[<span class="hljs-number">1</span>] > <span class="hljs-number">0</span>) { u[<span class="hljs-variable">$1</span>]; o1[<span class="hljs-variable">$1</span>]=<span class="hljs-number">1</span> } <span class="hljs-comment"># u[$1] 는 union 을 위한 array</span>
<span class="hljs-keyword">while</span> (getline < ARGV[<span class="hljs-number">2</span>] > <span class="hljs-number">0</span>) { u[<span class="hljs-variable">$1</span>]; o2[<span class="hljs-variable">$1</span>]=<span class="hljs-number">1</span> }
} END {
<span class="hljs-built_in">printf</span> <span class="hljs-string">"%12s | %s | %s |\n"</span>, <span class="hljs-string">" "</span>, ARGV[<span class="hljs-number">1</span>], ARGV[<span class="hljs-number">2</span>]
<span class="hljs-built_in">print</span> <span class="hljs-string">"--------------------------------"</span>
PROCINFO[<span class="hljs-string">"sorted_in"</span>] = <span class="hljs-string">"@ind_str_asc"</span>
<span class="hljs-keyword">for</span> ( i <span class="hljs-keyword">in</span> u )
<span class="hljs-built_in">printf</span> <span class="hljs-string">"%12s | %s | %s |\n"</span> ,
i , ( o1[i] ? <span class="hljs-string">"O"</span> : <span class="hljs-string">"X"</span> ) , ( o2[i] ? <span class="hljs-string">"O"</span> : <span class="hljs-string">"X"</span> )
}
EOF
| order1 | order2 |
--------------------------------
apple | O | X |
banana | O | O |
cherry | X | O |
grape | O | X |
mango | X | O |
melon | X | O |
orange | O | X |
strawberry | O | O |
tomato | X | O |
</code></pre>
</section>
</div>
</div>
</div>
<a href="./sort.html" class="navigation navigation-prev " aria-label="Previous page: Sort"><i class="fa fa-angle-left"></i></a>
<a href="./JOIN.html" class="navigation navigation-next " aria-label="Next page: JOIN"><i class="fa fa-angle-right"></i></a>
</div>
</div>
<script src="gitbook/app.js"></script>
<script src="gitbook/plugins/gitbook-plugin-search/lunr.min.js"></script>
<script src="gitbook/plugins/gitbook-plugin-search/search.js"></script>
<script src="gitbook/plugins/gitbook-plugin-sharing/buttons.js"></script>
<script src="gitbook/plugins/gitbook-plugin-fontsettings/buttons.js"></script>