我想从现有表中删除响应特定时间范围的条目(如果它们存在于此表中),然后计算它们并将它们插入到该表中。 如果它们不存在则创建它们并仅插入到表中。 怎么可能这样做? 谁能举个例子吗? 表结构应如下:
create table test_table
(
[Date] float
,[UserName] nvarchar(max)
,[SessionType] int
,[Duration] float
,[MessageCount] int
)
答案 0 :(得分:1)
如果你有一个存储DATE和TIME的列,那么你可以根据它删除记录。
假设我的表上有一个名为CreateDate的列。然后我可以删除所有通过Just Giving在上午10点到11点之间创建的记录
DELETE FROM MyTable WHERE
CreateDate BETWEEN '2018-01-12 10:00:00.000' AND '2018-01-12 11:00:00.000'
现在使用Normal INSERT语句再次插入值
答案 1 :(得分:1)
你可以按照这样的步骤进行
首先将时间范围内的记录集存储在临时表
中import express from 'express';
const router = express.Router();
import User from '../models/user.model';
import UserCtrl from '../controllers/user.controller';
module.exports = (jwt, app, passportGoogle) => {
/* GET api listing. */
router.get('/', (req, res) => {
UserCtrl.getUsers({})
.exec((err, users) => {
if (err)
return next(err);
res.json(users)
})
});
router.get('/google',
passportGoogle.authenticate('google', { scope: ['profile', 'email'] }));
router.get('/google/callback',
passportGoogle.authenticate('google', { failureRedirect: '/session/login' }),
//HERE : failureRedirect: '/session/login' :- it never called
function (req, res) {
console.log('req.user', req.user)
// Successful authentication, redirect home.
res.redirect('/dashboard');
});
return router;
}
然后从表中删除记录
SELECT * INTO tempTable FROM YourTable WHERE CONVERT(FLOAT, [Date]) BETWEEN 43100.3603763503 AND 43110.3603763503
然后根据您的要求使用DELETE FROM YourTable WHERE CONVERT(FLOAT, [Date]) BETWEEN 43100.3603763503 AND 43110.3603763503
中提供的数据进行计算并将数据插入表中
tempTable
然后放下tempTable
INSERT INTO YourTable
SELECT * FROM tempTable
答案 2 :(得分:0)
我正在更新我的解决方案,谢谢大家的帮助。
create mytable table
(
[Date] float
,[UserName] nvarchar(max)
,[SessionType] int
,[Duration] float
,[MessageCount] int
)
IF EXISTS (SELECT * FROM mytable WHERE [Date] >= 43082 AND [Date] < 43111)
BEGIN
DELETE FROM mytable WHERE [Date] >= 43082 AND [Date]< 43111
;WITH Data AS (
/*do the calculation of data*/
)
INSERT INTO mytable select * from Data
END
ELSE
BEGIN
;WITH Data AS (
/*do the calculation of data*/
)
INSERT INTO mytable select * from Data
END