我有一个表名' emp_records'其条目如:
Point_name | date | value |
X | 2016-02-01 | 22.2 |
Y | 2016-02-01 | 44 |
Z | 2016-02-01 | 10.4 |
X | 2016-02-02 | 2.8 |
Y | 2016-02-02 | 22.5 |
Z | 2016-02-02 | 15.4 |
.
.
.
因此,X,Y,Z的条目对应于每个日期。 现在我想做以下事情: 假设今天的日期是:2016-03-15
获取表中最新日期的X,Y,Z条目,说X,Y,Z的最新条目是最新的2016-02-20
将这些条目从2016-02-20复制到(当前日期 - 1),即2016-03-14。对于每组X,Y,Z,只应更改日期列。 例如,如果最新的条目集是2016-02-20:
Point_name | date | value |
X | 2016-02-20 | 20.5 |
Y | 2016-02-20 | 39 |
Z | 2016-02-20 | 11.4 |
将此设置复制到当前日期 - 1 像:
Point_name | date | value |
X | 2016-02-21 | 20.5 |
Y | 2016-02-21 | 39 |
Z | 2016-02-21 | 11.4 |
X | 2016-02-22 | 20.5 |
Y | 2016-02-22 | 39 |
Z | 2016-02-22 | 11.4 |
X | 2016-02-23 | 20.5 |
Y | 2016-02-23 | 39 |
Z | 2016-02-23 | 11.4 |
X | 2016-02-24 | 20.5 |
Y | 2016-02-24 | 39 |
Z | 2016-02-24 | 11.4 |
X | 2016-02-25 | 20.5 |
Y | 2016-02-25 | 39 |
Z | 2016-02-25 | 11.4 |
. . .
. . .
. . .
X | 2016-03-14 | 20.5 |
Y | 2016-03-14 | 39 |
Z | 2016-03-14 | 11.4 |
这只是一个示例表,我怀疑自己。实际的表包含9个不同的Point名称和许多其他列。任何人都可以帮助在单个SQL查询中编写它吗?
答案 0 :(得分:0)
此声明可以满足您的需求
insert into @emp_records SELECT point_name,dateadd(d,-1,GETDATE()),value
FROM @emp_records WHERE DATE IN (SELECT MAX(DATE) FROM @emp_records)
我使用下面的这句话来测试它。
DECLARE @emp_records TABLE (point_name VARCHAR(5),[DATE] DATE,VALUE DECIMAL(18,2))
INSERT INTO @emp_records VALUES ('X','02/20/2016',20.3)
INSERT INTO @emp_records VALUES ('Y','02/20/2016',10.7)
INSERT INTO @emp_records VALUES ('Z','02/20/2016',12.8)
insert into @emp_records SELECT point_name,dateadd(d,-1,GETDATE()),value
FROM @emp_records WHERE DATE IN (SELECT MAX(DATE) FROM @emp_records)
SELECT * FROM @emp_records
答案 1 :(得分:0)
在MySql中,如果您没有生成这些行的表(或内联查询),则无法生成动态行数。由于您最近的日期可能在遥远的过去,您可能需要大量这些记录。
所以我建议先创建一个名为 series 的辅助表。这只需要做一次:
create table series (
n int auto_increment,
primary key (n)
);
insert into series values (); -- 1 record
insert into series select null from series; -- 2 records
insert into series select null from series; -- 4 records
insert into series select null from series; -- 8 records
insert into series select null from series; -- 16 records
insert into series select null from series; -- 32 records
insert into series select null from series; -- 64 records
insert into series select null from series; -- 128 records
insert into series select null from series; -- 256 records
insert into series select null from series; -- 1024 records
insert into series select null from series; -- 2048 records
您可以重复insert
以使该表中的记录数翻倍,直到您有足够的记录。但是2000年已经涵盖了6年的日子,所以实际上这已经足够了。
然后你的插入语句变为:
insert into emp_records (point_name, date, value)
select point_name,
DATE_ADD(date, INTERVAL series.n DAY) as date,
value
from emp_records,
series
where date = (select max(date) from emp_records)
and DATE_ADD(date, INTERVAL series.n DAY) <= CURDATE();
This是一个显示select
部分结果的小提琴。
答案 2 :(得分:0)
首先获取每个emp_records
MAX
日Point_name
的{{1}}。 (以下查询为您提供了MAX(DATE)
中每个点table
的记录:
SELECT * FROM
(
SELECT
Point_name, date, value
FROM
emp_records
ORDER BY
Point_name , date DESC
) myAlias
GROUP BY
Point_name
现在,当你需要插入emp_records
时(通过迭代上面的结果来构建你的查询字符串):
/* PSUEDO-CODE (As Server-side Language not tagged) */
/* DECLARE a variable holding the Query-String */
string queryString = "INSERT INTO emp_records (Point_name, date, value) VALUES ";
/* GET the Current-Date in the language, you are using */
string dateString = "2016-03-15";
/* ITERATE the Fetched-Data resulted from the First-Query to Build the INSERT Query */
for(i=0; i<FetchedArray.Length; i++)
{
/* GET the Days-Difference in dateString and FetchedArray['date'] */
for(j=1; j <= numberOfDifferenceDays; j++)
{
/* DECLARE a Variable having the Date tobe inserted named as insertDate */
insertDate = FetchedArray['date'].AddDays(j);
/* CONCATENATE the queryString */
queryString += "(" + FetchedArray['Point_name'] +
", " + insertDate + ", " +
", " + FetchedArray['value'] + "), ";
}
}
/*
AT the end of this Iteration,
you would get a INSERT-QUERY that needs to be executed
to get what you wanted
Display variable queryString to check if requirement has fulfilled
Remove the COMMA at the end of the variable queryString
If you have the Correct Insert-Query (Execute it)
*/