我有3个人口的以下数据(可能是' n')。每个群体包含38个不同的记录,我按行存储所有数据,我想为所有群体明智地显示数据列。以下是存储在临时表中的数据:
pid s carrier sc c w
1 1 Ufone 331 20 3.289474
1 2 Ufone 332 24 3.947368
1 3 Ufone 333 93 15.29605
1 4 Ufone 334 38 6.25
1 5 Ufone 335 10 1.644737
1 6 Ufone 336 32 5.263158
1 7 Mobilink 300 43 7.072369
1 8 Mobilink 301 9 1.480263
1 9 Mobilink 302 10 1.644737
1 10 Mobilink 303 4 0.6578947
1 11 Mobilink 304 0 0
1 12 Mobilink 305 3 0.493421
1 13 Mobilink 306 6 0.9868421
1 14 Mobilink 307 13 2.138158
1 15 Mobilink 320 5 0.8223684
1 16 Mobilink 321 17 2.796053
1 17 Mobilink 322 11 1.809211
1 18 Mobilink 323 3 0.493421
1 19 Telenor 340 4 0.6578947
1 20 Telenor 341 14 2.302632
1 21 Telenor 342 7 1.151316
1 22 Telenor 343 5 0.8223684
1 23 Telenor 344 14 2.302632
1 24 Telenor 345 39 6.414474
1 25 Telenor 346 0 0
1 26 Telenor 347 0 0
1 27 Telenor 348 0 0
1 28 Telenor 349 1 0.1644737
1 29 Zong 310 8 1.315789
1 30 Zong 311 13 2.138158
1 31 Zong 312 25 4.111842
1 32 Zong 313 61 10.0329
1 33 Zong 314 17 2.796053
1 34 Zong 315 11 1.809211
1 35 Zong 316 23 3.782895
1 36 Zong 317 11 1.809211
1 37 Zong 318 5 0.8223684
1 38 Zong 319 9 1.480263
2 1 Ufone 331 19 3.869654
2 2 Ufone 332 19 3.869654
2 3 Ufone 333 71 14.46029
2 4 Ufone 334 35 7.12831
2 5 Ufone 335 8 1.629328
2 6 Ufone 336 19 3.869654
2 7 Mobilink 300 32 6.517312
2 8 Mobilink 301 14 2.851324
2 9 Mobilink 302 9 1.832994
2 10 Mobilink 303 3 0.610998
2 11 Mobilink 304 5 1.01833
2 12 Mobilink 305 2 0.407332
2 13 Mobilink 306 5 1.01833
2 14 Mobilink 307 1 0.203666
2 15 Mobilink 320 2 0.407332
2 16 Mobilink 321 16 3.258656
2 17 Mobilink 322 10 2.03666
2 18 Mobilink 323 3 0.610998
2 19 Telenor 340 3 0.610998
2 20 Telenor 341 7 1.425662
2 21 Telenor 342 4 0.8146639
2 22 Telenor 343 4 0.8146639
2 23 Telenor 344 10 2.03666
2 24 Telenor 345 31 6.313646
2 25 Telenor 346 3 0.610998
2 26 Telenor 347 1 0.203666
2 27 Telenor 348 1 0.203666
2 28 Telenor 349 0 0
2 29 Zong 310 14 2.851324
2 30 Zong 311 6 1.221996
2 31 Zong 312 23 4.684318
2 32 Zong 313 52 10.59063
2 33 Zong 314 16 3.258656
2 34 Zong 315 6 1.221996
2 35 Zong 316 15 3.05499
2 36 Zong 317 8 1.629328
2 37 Zong 318 1 0.203666
2 38 Zong 319 13 2.647658
3 1 Ufone 331 4 4.494382
3 2 Ufone 332 2 2.247191
3 3 Ufone 333 20 22.47191
3 4 Ufone 334 9 10.11236
3 5 Ufone 335 0 0
3 6 Ufone 336 3 3.370786
3 7 Mobilink 300 4 4.494382
3 8 Mobilink 301 6 6.741573
3 9 Mobilink 302 1 1.123595
3 10 Mobilink 303 0 0
3 11 Mobilink 304 1 1.123595
3 12 Mobilink 305 0 0
3 13 Mobilink 306 0 0
3 14 Mobilink 307 1 1.123595
3 15 Mobilink 320 0 0
3 16 Mobilink 321 4 4.494382
3 17 Mobilink 322 2 2.247191
3 18 Mobilink 323 1 1.123595
3 19 Telenor 340 2 2.247191
3 20 Telenor 341 1 1.123595
3 21 Telenor 342 0 0
3 22 Telenor 343 1 1.123595
3 23 Telenor 344 2 2.247191
3 24 Telenor 345 1 1.123595
3 25 Telenor 346 0 0
3 26 Telenor 347 0 0
3 27 Telenor 348 0 0
3 28 Telenor 349 1 1.123595
3 29 Zong 310 3 3.370786
3 30 Zong 311 4 4.494382
3 31 Zong 312 1 1.123595
3 32 Zong 313 9 10.11236
3 33 Zong 314 0 0
3 34 Zong 315 0 0
3 35 Zong 316 3 3.370786
3 36 Zong 317 1 1.123595
3 37 Zong 318 0 0
3 38 Zong 319 2 2.247191
我想将结果记录设为:
pid1_s pid1_carrier pid1_sc pid1_c pid1_w pid2_s pid2_carrier pid2_sc pid2_c pid2_w pid3_s pid3_carrier pid3_sc pid3_c pid3_w
1 Ufone 331 20 3.289474 1 Ufone 331 19 3.869654 1 Ufone 331 4 4.494382
2 Ufone 332 24 3.947368 2 Ufone 332 19 3.869654 2 Ufone 332 2 2.247191
3 Ufone 333 93 15.29605 3 Ufone 333 71 14.46029 3 Ufone 333 20 22.47191
4 Ufone 334 38 6.25 4 Ufone 334 35 7.12831 4 Ufone 334 9 10.11236
5 Ufone 335 10 1.644737 5 Ufone 335 8 1.629328 5 Ufone 335 0 0
6 Ufone 336 32 5.263158 6 Ufone 336 19 3.869654 6 Ufone 336 3 3.370786
7 Mobilink 300 43 7.072369 7 Mobilink 300 32 6.517312 7 Mobilink 300 4 4.494382
8 Mobilink 301 9 1.480263 8 Mobilink 301 14 2.851324 8 Mobilink 301 6 6.741573
9 Mobilink 302 10 1.644737 9 Mobilink 302 9 1.832994 9 Mobilink 302 1 1.123595
10 Mobilink 303 4 0.6578947 10 Mobilink 303 3 0.610998 10 Mobilink 303 0 0
11 Mobilink 304 0 0 11 Mobilink 304 5 1.01833 11 Mobilink 304 1 1.123595
12 Mobilink 305 3 0.493421 12 Mobilink 305 2 0.407332 12 Mobilink 305 0 0
13 Mobilink 306 6 0.9868421 13 Mobilink 306 5 1.01833 13 Mobilink 306 0 0
14 Mobilink 307 13 2.138158 14 Mobilink 307 1 0.203666 14 Mobilink 307 1 1.123595
15 Mobilink 320 5 0.8223684 15 Mobilink 320 2 0.407332 15 Mobilink 320 0 0
16 Mobilink 321 17 2.796053 16 Mobilink 321 16 3.258656 16 Mobilink 321 4 4.494382
17 Mobilink 322 11 1.809211 17 Mobilink 322 10 2.03666 17 Mobilink 322 2 2.247191
18 Mobilink 323 3 0.493421 18 Mobilink 323 3 0.610998 18 Mobilink 323 1 1.123595
19 Telenor 340 4 0.6578947 19 Telenor 340 3 0.610998 19 Telenor 340 2 2.247191
20 Telenor 341 14 2.302632 20 Telenor 341 7 1.425662 20 Telenor 341 1 1.123595
21 Telenor 342 7 1.151316 21 Telenor 342 4 0.8146639 21 Telenor 342 0 0
22 Telenor 343 5 0.8223684 22 Telenor 343 4 0.8146639 22 Telenor 343 1 1.123595
23 Telenor 344 14 2.302632 23 Telenor 344 10 2.03666 23 Telenor 344 2 2.247191
24 Telenor 345 39 6.414474 24 Telenor 345 31 6.313646 24 Telenor 345 1 1.123595
25 Telenor 346 0 0 25 Telenor 346 3 0.610998 25 Telenor 346 0 0
26 Telenor 347 0 0 26 Telenor 347 1 0.203666 26 Telenor 347 0 0
27 Telenor 348 0 0 27 Telenor 348 1 0.203666 27 Telenor 348 0 0
28 Telenor 349 1 0.1644737 28 Telenor 349 0 0 28 Telenor 349 1 1.123595
29 Zong 310 8 1.315789 29 Zong 310 14 2.851324 29 Zong 310 3 3.370786
30 Zong 311 13 2.138158 30 Zong 311 6 1.221996 30 Zong 311 4 4.494382
31 Zong 312 25 4.111842 31 Zong 312 23 4.684318 31 Zong 312 1 1.123595
32 Zong 313 61 10.0329 32 Zong 313 52 10.59063 32 Zong 313 9 10.11236
33 Zong 314 17 2.796053 33 Zong 314 16 3.258656 33 Zong 314 0 0
34 Zong 315 11 1.809211 34 Zong 315 6 1.221996 34 Zong 315 0 0
35 Zong 316 23 3.782895 35 Zong 316 15 3.05499 35 Zong 316 3 3.370786
36 Zong 317 11 1.809211 36 Zong 317 8 1.629328 36 Zong 317 1 1.123595
37 Zong 318 5 0.8223684 37 Zong 318 1 0.203666 37 Zong 318 0 0
38 Zong 319 9 1.480263 38 Zong 319 13 2.647658 38 Zong 319 2 2.247191
如何应用此类加入或交叉表?
答案 0 :(得分:0)
考虑以下问题,这是您想要的:
SELECT
s,
MAX(CASE WHEN pid = 1 THEN carrier END) AS pid1_carrier,
MAX(CASE WHEN pid = 1 THEN sc END) AS pid1_sc,
MAX(CASE WHEN pid = 1 THEN c END) AS pid1_c,
MAX(CASE WHEN pid = 1 THEN w END) AS pid1_w
-- do the same for the other two pid values
FROM yourTable
GROUP BY
s;
在这种情况下,枢轴技巧使用MAX
,因为对于每个s
值,CASE
表达式只保留来自特定{NULL
的非pid
值1}}人口。