Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/sql/85.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
sql枢轴字符变量_Sql_Pivot - Fatal编程技术网

sql枢轴字符变量

sql枢轴字符变量,sql,pivot,Sql,Pivot,为了在sql中透视表,假设以下占领表包含3个特性,即ID、FirstName和占领,如下所示: 然后,为了恢复pivot的职业基础,我尝试了以下方法: select actress, Dancer, Photographer, Salesman from Occupation o pivot (max(o.FirstName) for o.Occupation in ([Actress], [Dancer], [Photo

为了在sql中透视表,假设以下占领表包含3个特性,即ID、FirstName和占领,如下所示:

然后,为了恢复pivot的职业基础,我尝试了以下方法:

select actress,
       Dancer,
       Photographer,
       Salesman
       from Occupation o 
       pivot (max(o.FirstName) for o.Occupation in ([Actress], [Dancer], [Photographer], [Salesman])) as pivotable

而结果返回NULL。这里假设实现的是pivot只包含基于职业的人名,而不返回all Null,如下表所示


您只需从占领表中选择所需的列。使用CTE或派生表执行此操作

select actress,
       Dancer,
       Photographer,
       Salesman
from   (
              select FirstName, Occupation 
              from Occupation 
       ) o 
       pivot 
       (
              max(o.FirstName) 
              for o.Occupation in ([Actress], [Dancer], [Photographer], [Salesman])
       ) as pivotable

问题是,在您的数据中,每个人之间没有关系,因此不能显示在同一行上(您想在同一行上看到Juli和Satria的哪些链接?)

如果可以按每个作业生成行号,则可以按每个作业的基本位置将它们混合在一起

IF OBJECT_ID('tempdb..#Jobs') IS NOT NULL
    DROP TABLE #Jobs

CREATE TABLE #Jobs (
    ID INT,
    Name VARCHAR(100),
    Job VARCHAR(100))

INSERT INTO #Jobs (
    ID,
    Name,
    Job)
VALUES
    (1001, 'Satria', 'Dancer'),
    (1002, 'Juli', 'Actress'),
    (1003, 'Mario', 'Actress'),
    (1004, 'Memet', 'Salesman'),
    (1005, 'Alan', 'Photographer'),
    (1006, 'Kiky', 'Photographer'),
    (1007, 'Chacha', 'Photographer'),
    (1008, 'Joko', 'Actress'),
    (1009, 'Juni', 'Dancer'),
    (1010, 'Putra', 'Salesman')



;WITH JobNumbering AS
(
    SELECT
        J.Job,
        J.Name,
        Ranking = ROW_NUMBER() OVER (PARTITION BY J.Job ORDER BY J.ID)
    FROM
        #Jobs AS J
)
SELECT
    P.Actress,
    P.Dancer,
    P.Photographer,
    P.Salesman
FROM
    JobNumbering AS J
    PIVOT (
        MAX(J.Name) FOR J.Job IN ([Actress], [Dancer], [Photographer], [Salesman])
    ) AS P

您应该首先在pivot的源表部分使用row_number函数以所需的方式对它们进行排序


请不要使用图片,而是共享与我想要实现的非常接近的scriptswork表,但我的意思是,不要将所有null返回为表所需的结果。无论如何谢谢你。。。
select 
    actress,
    Dancer,
    Photographer,
    Salesman
from 
(
select 
   FirstName,
   Occupation,-- see no Id here in select list to explicitly avoid ordering by id
   rn= row_number() over (partition by Occupation order by id asc)
from 
Occupation 
)  o 
pivot 
(
    max(o.FirstName) 
    for o.Occupation in 
    (
        [Actress], [Dancer], [Photographer], [Salesman]
    )
) as pivotable
order by rn --explicitly added to get desired results
select 
-- incase u dont want null to be populated as a value in the output
  isnull(dancer,' ') as dancer,
  isnull(actress,' ') as actress,
  isnull(salesman,' ') as salesman,
  isnull(photographer,' ') as photographer
from
(
 select firstname,
 occupation,
 row_number() over (partition by occupation order by occupation) as ranks
 from Occupation
) a
pivot
(
min(firstname)
for occupation in ([Dancer],[actress],[salesman], [photographer])
) as piv
order by ranks