如何将数据从table1插入到table2我正在使用MS Access。 这就是我想要的。
表1
|id| date | time |
| 3| 7/22/2017 | 8:00 |
| 3| 7/22/2017 | 17:00 |
| 4| 7/22/2017 | 8:00 |
| 4| 7/22/2017 | 12:00 |
| 4| 7/22/2017 | 13:00 |
| 4| 7/22/2017 | 17:00 |
我想在新桌子上这样做 表2
|id|working date|time-in|time-out|time-in|time-out|
| 3| 7/22/2017 | 8:00 | | | 17:00 |
| 4| 7/22/2017 | 8:00 | 12:00 | 13:00 | 17:00 |
是否可以循环或这是一个查询?请帮忙
答案 0 :(得分:0)
从执行不等自连接的查询开始,将顺序值分配给按[id]和[date]分组的行
SELECT
t1.id,
t1.date,
t1.time,
COUNT(*) AS seq
FROM
Table1 t1
INNER JOIN
Table1 t2
ON t1.id = t2.id AND t1.date = t2.date AND t1.time >= t2.time
GROUP BY t1.id, t1.date, t1.time
给了我们
id date time seq
-- ---------- -------- ---
3 2017-07-22 08:00:00 1
3 2017-07-22 17:00:00 2
4 2017-07-22 08:00:00 1
4 2017-07-22 12:00:00 2
4 2017-07-22 13:00:00 3
4 2017-07-22 17:00:00 4
在计算"标记"的查询中包裹它表示' in1',' out1',' in2',' out2'等
SELECT
[id],
[date],
[time],
IIf([seq] Mod 2 = 1, "in", "out") & ([seq] + 1) \ 2 AS tag
FROM
(
SELECT
t1.id,
t1.date,
t1.time,
COUNT(*) AS seq
FROM
Table1 t1
INNER JOIN
Table1 t2
ON t1.id = t2.id AND t1.date = t2.date AND t1.time >= t2.time
GROUP BY t1.id, t1.date, t1.time
)
制造
id date time tag
-- ---------- -------- ----
3 2017-07-22 08:00:00 in1
3 2017-07-22 17:00:00 out1
4 2017-07-22 08:00:00 in1
4 2017-07-22 12:00:00 out1
4 2017-07-22 13:00:00 in2
4 2017-07-22 17:00:00 out2
然后将整个事物包装在交叉表查询中
TRANSFORM Min([time]) AS MinOftime
SELECT [id], [date]
FROM
(
SELECT
[id],
[date],
[time],
IIf([seq] Mod 2 = 1, "in", "out") & ([seq] + 1) \ 2 AS tag
FROM
(
SELECT
t1.id,
t1.date,
t1.time,
COUNT(*) AS seq
FROM
Table1 t1
INNER JOIN
Table1 t2
ON t1.id = t2.id AND t1.date = t2.date AND t1.time >= t2.time
GROUP BY t1.id, t1.date, t1.time
)
)
GROUP BY [id], [date]
PIVOT [tag]
获得最终结果
id date in1 in2 out1 out2
-- ---------- -------- -------- -------- --------
3 2017-07-22 08:00:00 17:00:00
4 2017-07-22 08:00:00 13:00:00 12:00:00 17:00:00