[英]MySQL Query, Pivoting Table
I'm having trouble building up my Query that will pivot my rows to columns. 我在建立将我的行旋转为列的查询时遇到了麻烦。 My Pivot is my DATE. 我的数据透视表是我的DATE。 What the User need is to have a view of a full week data. 用户需要的是查看整周的数据。
My Data is constructed like this : 我的数据是这样构造的:
ID - AssemblyDate
----------------------------------
12345 - 2015-01-01
12346 - 2015-01-01
12347 - 2015-01-01
12348 - 2015-01-02
12349 - 2015-01-02
12350 - 2015-01-02
12351 - 2015-01-03
12352 - 2015-01-03
12353 - 2015-01-03
And the Result I'm expecting would be something like this 我期望的结果将是这样的
DAY1 - DAY2 - DAY3
12345 12348 12351
12346 12349 12352
12347 12350 12352
What I have Tried : 我尝试过的东西:
SELECT CASE WHEN (AssemblyDate = '2015-01-01') THEN ID ELSE NULL END AS DAY1
, CASE WHEN (AssemblyDate = '2015-01-02') THEN ID ELSE NULL END AS DAY2
, CASE WHEN (AssemblyDate = '2015-01-03') THEN ID ELSE NULL END AS DAY3
FROM MyTable
GROUP BY AssemblyDate
This Gives me something like this : 这给了我这样的东西:
DAY1 - DAY2 - DAY3
12345 NULL NULL
12346 NULL NULL
12347 NULL NULL
NULL 12348 NULL
NULL 12349 NULL
NULL 12350 NULL
NULL NULL 12351
NULL NULL 12352
NULL NULL 12352
but I don't want the nulls ... is there a way I could like GROUP them without the nulls ? 但是我不想要空值...有没有一种方法可以让我将它们设为空值?
You need to do a bit of fiddling, as you need to further group those results. 您需要做些摆弄,因为您需要进一步将这些结果分组。 Since there's nothing to group them on, we need to engineer something to use for grouping - in this case, one variable per column that we increment whenever that column is the one that matches the assembly date: 由于没有什么可分组的,因此我们需要设计一些用于分组的东西-在这种情况下,每列一个变量,只要该列与装配日期匹配,我们就会增加该变量:
select if(assemblydate = '2015-01-01', @d1c := @d1c +1,
if(assemblydate = '2015-01-02', @d2c := @d2c + 1, @d3c := @d3c +1)) id,
case when assemblydate = '2015-01-01' then id end d1,
case when assemblydate = '2015-01-02' then id end d2,
case when assemblydate = '2015-01-03' then id end d3
from d, (select @d1c := 0, @d2c := 0, @d3c := 0) q;
That query just adds an id column, that increments for each day, the results look like this. 该查询仅添加一个id列,该列每天递增,结果如下所示。
+------+-------+-------+-------+
| id | d1 | d2 | d3 |
+------+-------+-------+-------+
| 1 | 12345 | NULL | NULL |
| 2 | 12346 | NULL | NULL |
| 3 | 12347 | NULL | NULL |
| 1 | NULL | 12348 | NULL |
| 2 | NULL | 12349 | NULL |
| 3 | NULL | 12350 | NULL |
| 1 | NULL | NULL | 12351 |
| 2 | NULL | NULL | 12352 |
| 3 | NULL | NULL | 12352 |
+------+-------+-------+-------+
Once that's done, we just wrap another query around the result, group by the id and pick up the values that aren't null with max()
完成后,我们只需要对结果进行另一个查询,将ID分组即可,并使用max()
不为null的值
select max(d1), max(d2), max(d3) from
(
select if(assemblydate = '2015-01-01', @d1c := @d1c +1,
if(assemblydate = '2015-01-02', @d2c := @d2c + 1, @d3c := @d3c +1)) id,
case when assemblydate = '2015-01-01' then id end d1,
case when assemblydate = '2015-01-02' then id end d2,
case when assemblydate = '2015-01-03' then id end d3
from d, (select @d1c := 0, @d2c := 0, @d3c := 0) q
) qq
group by id;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.