我想根据周进行预测。我有这样的桌子。
personName, year, week1, ..., week52.
并且想根据我需要多少周将下一年添加到列中。选择中的某种选择。
最终选择应如下所示:
select personName, week1,week2,..., (nextyear)week1,(nextyear)week2,.. from x
任何想法如何将明年移至列?
我想根据周进行预测。我有这样的桌子。
personName, year, week1, ..., week52.
并且想根据我需要多少周将下一年添加到列中。选择中的某种选择。
最终选择应如下所示:
select personName, week1,week2,..., (nextyear)week1,(nextyear)week2,.. from x
任何想法如何将明年移至列?
您想为您的数据而不是列创建行 - 出于您所询问的确切原因。
像这样的结构:
personName Year Week
然后您可以执行如下查询:
select week from table1 where year=$year-1 and week=$week
或像这样的纯 SQL:
select
a.personName
a.week,
a.year
from
table1 a
join table1 b
on a.personName=b.personName
and a.week=b.week
and a.year=b.year-1
但是,这种类似的方法可以与您的表一起使用,如下所示:
select
a.personName
a.week1,
a.week2,
a.week3,
b.week1,
b.week2,
b.week3,
a.year
from
table1 a
join table1 b
on a.personName=b.personName
and a.year=b.year-1