以下是我的SELECT陈述,它很好地支持了我的数据.
我的数据看起来像这样:
col_a | col_b | col_c | col_d | Score
-------------------------------------
stuff | stuff | stuff | null | 5
stuff | stuff | stuff | title_a | 3
stuff | stuff | stuff | title_x | 4
Run Code Online (Sandbox Code Playgroud)
我目前的Pivot语句如下所示:
SELECT `col_a`, `col_b`, `col_c`,
MAX(CASE `col_d` WHEN 'title_a' THEN `col_d` end) AS 'Title',
MAX(CASE `col_d` WHEN 'title_a' THEN `score` end) AS 'Score'
MAX(CASE `col_d` WHEN 'title_x' THEN `col_d` end) AS 'Title',
MAX(CASE `col_d` WHEN 'title_x' THEN `score` end) AS 'Score'
.....
Run Code Online (Sandbox Code Playgroud)
这给了我以下结果:
col_a | col_b | col_c | Title | Score | Title | Score
---------------------------------------------------------
stuff | stuff | stuff | title_a | 3 | title_x | 4
Run Code Online (Sandbox Code Playgroud)
我想做的是检查更多标题,但我只想在数据透视表中有四列.最多只有2行需要转动到上面的记录.但col_d可以包含任何标题.
例如,我尝试了以下内容:
我的数据现在看起来像这样:
col_a | col_b | col_c | col_d | Score
-------------------------------------
stuff | stuff | stuff | null | 5
stuff | stuff | stuff | title_a | 3
stuff | stuff | stuff | title_x | 4
stuff | stuff | stuff | null | 5
stuff | stuff | stuff | title_a | 3
stuff | stuff | stuff | title_bx | 4
Run Code Online (Sandbox Code Playgroud)
我的Pivot语句现在看起来像这样:
SELECT `col_a`, `col_b`, `col_c`,
MAX(CASE `col_d` WHEN 'title_a' THEN `col_d` end) AS 'Title',
MAX(CASE `col_d` WHEN 'title_a' THEN `score` end) AS 'Score'
MAX(CASE `col_d` WHEN 'title_x' THEN `col_d` end) AS 'Title',
MAX(CASE `col_d` WHEN 'title_x' THEN `score` end) AS 'Score'
MAX(CASE `col_d` WHEN 'title_bx' THEN `col_d` end) AS 'Second Title',
MAX(CASE `col_d` WHEN 'title_bx' THEN `score` end) AS 'Score'
.....
Run Code Online (Sandbox Code Playgroud)
所以你可以看到我试图检查另一个标题,但只是给了我六列,其中2个为空,因为在这种情况下,两行包含title_a和title_bx,所以中间两列,其中充满null.
我想从上面的数据输出:
col_a | col_b | col_c | Title | Score | Title | Score
---------------------------------------------------------
stuff | stuff | stuff | title_a | 3 | title_x | 4
stuff | stuff | stuff | title_a | 3 | title_bx | 4
Run Code Online (Sandbox Code Playgroud)
所以我的问题是如何检查多个可能的标题col_d,并且只有4列.
这有点乱,因为MySQL没有窗口函数,你想在第一组Title/ Score列中包含非常具体的值.您可以通过使用一些用户变量为那些col_d不等于的行创建行号来获得最终结果title_a,然后将其连接回您的表.
语法类似于以下内容:
select a.col_a, a.col_b, a.col_c,
max(case when a.col_d = 'title_a' then a.col_d end) title1,
max(case when a.col_d = 'title_a' then a.score end) score1,
max(case when na.col_d <> 'title_a' then na.col_d end) title2,
max(case when na.col_d <> 'title_a' then na.score end) score2
from yourtable a
left join
(
-- need to generate a row number value for the col_d rows
-- that aren't equal to title_a
select n.col_a, n.col_b, n.col_c, n.col_d,
n.score,
@num:=@num+1 rownum
from yourtable n
cross join
(
select @num:=0
) d
where n.col_d <> 'title_a'
order by n.col_a, n.col_b, n.col_c, n.col_d
) na
on a.col_a = na.col_a
and a.col_b = na.col_b
and a.col_c = na.col_c
-- in the event you have more than 2 row only return 2
and na.rownum <= 2
where a.col_d = 'title_a'
group by a.col_a, a.col_b, a.col_c, na.rownum;
Run Code Online (Sandbox Code Playgroud)
请参阅SQL Fiddle with Demo.这得到一个结果:
| COL_A | COL_B | COL_C | TITLE1 | SCORE1 | TITLE2 | SCORE2 |
|-------|-------|-------|---------|--------|----------|--------|
| stuff | stuff | stuff | title_a | 3 | title_bx | 4 |
| stuff | stuff | stuff | title_a | 3 | title_x | 4 |
Run Code Online (Sandbox Code Playgroud)
有人指出,如果你只有2个其他值,那么你可以简单地加入数据而不使用用户变量:
select distinct a.col_a, a.col_b, a.col_c,
a.col_d title1,
a.score score1,
na.col_d title2,
na.score score2
from yourtable a
left join
(
select n.col_a, n.col_b, n.col_c, n.col_d,
n.score
from yourtable n
where n.col_d <> 'title_a'
) na
on a.col_a = na.col_a
and a.col_b = na.col_b
and a.col_c = na.col_c
where a.col_d = 'title_a';
Run Code Online (Sandbox Code Playgroud)
请参阅SQL Fiddle with Demo.这给出了相同的结果:
| COL_A | COL_B | COL_C | TITLE1 | SCORE1 | TITLE2 | SCORE2 |
|-------|-------|-------|---------|--------|----------|--------|
| stuff | stuff | stuff | title_a | 3 | title_x | 4 |
| stuff | stuff | stuff | title_a | 3 | title_bx | 4 |
Run Code Online (Sandbox Code Playgroud)
根据您实际拥有的数据col_a,col_b以及col_c你可能必须改变这一点,但它应该得到你所需要的结果.
更新:根据您的注释,您将不知道col_d列中的值,但您只需要将数据拆分为两个透视列,过程变得复杂,因为MySQL没有窗口函数.如果有NTILE功能,这将非常容易.该NTILE函数将行分配到特定数量的组中.在这种情况下,您的数据将分为两组.
我已经修改了SO User,Quassnoi在这个博客中使用用户变量复制函数的代码.变量用于创建两个东西,一个行号(在旋转期间使用)和ntile值. NTILE
代码将被修改为:
select
x.col_a,
x.col_b,
x.col_c,
max(case when x.splitgroup = 1 then x.col_d end) as Title1,
max(case when x.splitgroup = 1 then x.Score end) as Score1,
max(case when x.splitgroup = 2 then x.col_d end) as Title2,
max(case when x.splitgroup = 2 then x.Score end) as Score2
from
(
select src.col_a, src.col_b, src.col_c, src.col_d, src.score,
src.splitGroup,
@row:=case when @prev=src.splitGroup then @row else 0 end +1 rownum,
@prev:=src.splitGroup
from
(
-- mimic NTILE function by splitting the total count of rows
-- over the number of columns we want (2)
select d.col_a, d.col_b, d.col_c, d.col_d, d.score,
FLOOR((@r * @n) / cnt) + 1 AS splitGroup
from
(
select a.col_a, a.col_b, a.col_c, a.col_d, a.score, grp.cnt
from yourtable a
inner join
(
select col_a, col_b, col_c, count(*) as cnt
from yourtable
where col_d is not null
group by col_a, col_b, col_c
) grp
on a.col_a = grp.col_a
and a.col_b = grp.col_b
and a.col_c = grp.col_c
where a.col_d is not null
order by a.col_a, a.col_b, a.col_c
) d
cross join
(
-- @n is equal to the number of new pivoted columns we want
select @n:=2, @group1:='N', @group2:='N', @group3:='N'
) v
WHERE
CASE
WHEN @group1 <> col_a AND @group2<> col_b AND @group3 <> col_c
THEN @r := -1
ELSE 0 END IS NOT NULL
AND (@r := @r + 1) IS NOT NULL
) src
cross join
(
-- these vars are used to get the row number once the data is split
-- this will be needed for the aggregate/group by on the final select
select @row:=0, @prev:=1
) v2
order by src.splitGroup
) x
group by x.col_a, x.col_b, x.col_c, x.rowNum;
Run Code Online (Sandbox Code Playgroud)
请参阅SQL Fiddle with Demo.这给出了结果:
| COL_A | COL_B | COL_C | TITLE1 | SCORE1 | TITLE2 | SCORE2 |
|-------|-------|-------|----------|--------|----------|--------|
| stuff | stuff | stuff | title_a | 3 | title_tt | 1 |
| stuff | stuff | stuff | title_bx | 0 | title_qq | 1 |
| stuff | stuff | stuff | title_x | 4 | title_a | 8 |
| stuff | stuff | stuff | title_yy | 3 | title_h | 4 |
| stuff | stuff | stuff | title_a | 2 | title_o | 6 |
Run Code Online (Sandbox Code Playgroud)