考虑以下查询:
DECLARE @t1 TABLE(
CompanyId INT,
DirectorName VARCHAR(100));
INSERT INTO @t1
VALUES
(1,'D11'),
(1,'D12'),
(1,'D13'),
(1,'D14'),
(1,'D15'),
(1,'D16'),
(2,'D21'),
(2,'D22'),
(2,'D23'),
(2,'D24'),
(2,'D25'),
(2,'D26'),
(2,'D27'),
(2,'D28'),
(2,'D29'),
(2,'D210'),
(3,'D31'),
(3,'D32'),
(3,'D33');
SELECT * FROM @t1
它仅返回该ID的companyId和一组董事。如何生成以下输出:
CompanyId | Director1| Director2| Director3|Director4| Director5| Director6| Director7| Director8| Director9| Director10|
-----------------------------------------------------------------------------------------------------------------------------
1 D11 D12 D13 D14 D15 D16 NULL NULL NULL NULL
2 D21 D22 D23 D24 D25 D26 D27 D28 D29 D210
3 D31 D32 D33 NULL NULL NULL NULL NULL NULL NULL
上述查询技巧的出处是每个公司的董事人数都不相同。在上面的示例中,第2行具有最高的控制器数目,这意味着最终输出内其他ID的相关列将为空。如何创建以上输出。
为了清楚起见,在此示例中,每个id(companyId)的结果不会超过10列(导演)。
答案 0 :(得分:2)
您可以使用PIVOT
使用以下解决方案:
Traceback (most recent call last):
File "manage.py", line 21, in <module>
main()
File "manage.py", line 17, in main
execute_from_command_line(sys.argv)
File "/home/akshay/django/google_login/myvenv/lib/python3.6/site-packages/django/core/management/__init__.py", line 381, in execute_from_command_line
utility.execute()
File "/home/akshay/django/google_login/myvenv/lib/python3.6/site-packages/django/core/management/__init__.py", line 357, in execute
django.setup()
File "/home/akshay/django/google_login/myvenv/lib/python3.6/site-packages/django/__init__.py", line 24, in setup
apps.populate(settings.INSTALLED_APPS)
File "/home/akshay/django/google_login/myvenv/lib/python3.6/site-packages/django/apps/registry.py", line 114, in populate
app_config.import_models()
File "/home/akshay/django/google_login/myvenv/lib/python3.6/site-packages/django/apps/config.py", line 211, in import_models
self.models_module = import_module(models_module_name)
File "/usr/lib/python3.6/importlib/__init__.py", line 126, in import_module
return _bootstrap._gcd_import(name[level:], package, level)
File "<frozen importlib._bootstrap>", line 994, in _gcd_import
File "<frozen importlib._bootstrap>", line 971, in _find_and_load
File "<frozen importlib._bootstrap>", line 955, in _find_and_load_unlocked
File "<frozen importlib._bootstrap>", line 665, in _load_unlocked
File "<frozen importlib._bootstrap_external>", line 678, in exec_module
File "<frozen importlib._bootstrap>", line 219, in _call_with_frames_removed
File "/home/akshay/django/google_login/login/models.py", line 3, in <module>
from oauth2client.contrib.django_util.models import CredentialsField
File "/home/akshay/django/google_login/myvenv/lib/python3.6/site-packages/oauth2client/contrib/django_util/__init__.py", line 365, in <module>
oauth2_settings = OAuth2Settings(django.conf.settings)
File "/home/akshay/django/google_login/myvenv/lib/python3.6/site-packages/oauth2client/contrib/django_util/__init__.py", line 341, in __init__
info = _get_oauth2_client_id_and_secret(settings_instance)
File "/home/akshay/django/google_login/myvenv/lib/python3.6/site-packages/oauth2client/contrib/django_util/__init__.py", line 277, in _get_oauth2_client_id_and_secret
return _load_client_secrets(secret_json)
File "/home/akshay/django/google_login/myvenv/lib/python3.6/site-packages/oauth2client/contrib/django_util/__init__.py", line 255, in _load_client_secrets
client_type, client_info = clientsecrets.loadfile(filename)
File "/home/akshay/django/google_login/myvenv/lib/python3.6/site-packages/oauth2client/clientsecrets.py", line 165, in loadfile
return _loadfile(filename)
File "/home/akshay/django/google_login/myvenv/lib/python3.6/site-packages/oauth2client/clientsecrets.py", line 122, in _loadfile
obj = json.load(fp)
File "/usr/lib/python3.6/json/__init__.py", line 299, in load
parse_constant=parse_constant, object_pairs_hook=object_pairs_hook, **kw)
File "/usr/lib/python3.6/json/__init__.py", line 354, in loads
return _default_decoder.decode(s)
File "/usr/lib/python3.6/json/decoder.py", line 339, in decode
obj, end = self.raw_decode(s, idx=_w(s, 0).end())
File "/usr/lib/python3.6/json/decoder.py", line 355, in raw_decode
obj, end = self.scan_once(s, idx)
json.decoder.JSONDecodeError: Invalid control character at: line 1 column 451 (char 450)
答案 1 :(得分:1)
使用CTE,ROW_NUMBER和条件聚合,您可以做到这一点
每家公司的董事人数不超过10名
;with cteGetDirectorNum
AS(
select
T.CompanyId
,T.DirectorName
,DirectorNum ='Director'+CONVERT(VARCHAR(10), Row_Number()over( partition by T.CompanyId order by T.CompanyId, T.DirectorName))
from
@t1 T
)
SELECT
N.CompanyId
,Director1 = MAX(CASE WHEN N.DirectorNum = 'Director1' THEN n.DirectorName ELSE NULL END)
,Director2 = MAX(CASE WHEN N.DirectorNum = 'Director2' THEN n.DirectorName ELSE NULL END)
,Director3 = MAX(CASE WHEN N.DirectorNum = 'Director3' THEN n.DirectorName ELSE NULL END)
,Director4 = MAX(CASE WHEN N.DirectorNum = 'Director4' THEN n.DirectorName ELSE NULL END)
,Director5 = MAX(CASE WHEN N.DirectorNum = 'Director5' THEN n.DirectorName ELSE NULL END)
,Director6 = MAX(CASE WHEN N.DirectorNum = 'Director6' THEN n.DirectorName ELSE NULL END)
,Director7 = MAX(CASE WHEN N.DirectorNum = 'Director7' THEN n.DirectorName ELSE NULL END)
,Director8 = MAX(CASE WHEN N.DirectorNum = 'Director8' THEN n.DirectorName ELSE NULL END)
,Director9 = MAX(CASE WHEN N.DirectorNum = 'Director9' THEN n.DirectorName ELSE NULL END)
,Director10 = MAX(CASE WHEN N.DirectorNum = 'Director10' THEN n.DirectorName ELSE NULL END)
FROM
cteGetDirectorNum N
GROUP BY N.CompanyId