将列转换为oracle中的行

时间:2016-02-03 11:50:49

标签: sql oracle pivot

我有如下表结构,需要输出如下所述

表:

    C:\Users\ADMIN>pip --version
Traceback (most recent call last):
  File "c:\dev\python27\lib\runpy.py", line 162, in _run_module_as_main
    "__main__", fname, loader, pkg_name)
  File "c:\dev\python27\lib\runpy.py", line 72, in _run_code
    exec code in run_globals
  File "C:\dev\Python27\Scripts\pip.exe\__main__.py", line 5, in <module>
  File "c:\dev\python27\lib\site-packages\pip\__init__.py", line 15, in <module>
    from pip.vcs import git, mercurial, subversion, bazaar  # noqa
  File "c:\dev\python27\lib\site-packages\pip\vcs\mercurial.py", line 10, in <module>
    from pip.download import path_to_url
  File "c:\dev\python27\lib\site-packages\pip\download.py", line 35, in <module>
    from pip.utils.ui import DownloadProgressBar, DownloadProgressSpinner
  File "c:\dev\python27\lib\site-packages\pip\utils\ui.py", line 51, in <module>
    _BaseBar = _select_progress_class(IncrementalBar, Bar)
  File "c:\dev\python27\lib\site-packages\pip\utils\ui.py", line 44, in _select_progress_class
    six.text_type().join(characters).encode(encoding)
LookupError: unknown encoding: cp65001

答案:

A                   B
CUSTOMER_TYPE_ID    4
CUSTOMER_TYPE_ID    3
CUSTOMER_TYPE_ID    2
CUSTOMER_TYPE_ID    1
CUSTOMER_TYPE_ID    0

怎么做?

1 个答案:

答案 0 :(得分:2)

如上所述,这不是一个枢轴而是聚合字符串连接。 Oracle函数是LISTAGG(): 选择组内的listagg(b,&#39;,&#39;)(按b desc排序)为b 从T 分组; 编辑: 如果您想要围绕值的单引号: 选择listagg(&#39;&#39;&#39; || b ||&#39;&#39;&#39;&#39;,&#39;,&#39;在组内(由b desc命令)为b 从T 分组;