首先,如果标题没有意义,我很抱歉,但下面是详细的情节。
说我有document_revision
表
id document_id phase_id user_id
1 1 3 1
2 1 2 1
3 1 1 1
4 2 3 2
5 2 2 2
其中phase_id
是:transcribe = 3;证明= 2;并提交= 1。
我想写一个查询,我可以过滤修订记录,如果相同的用户进行了转录和证明,我将忽略证明阶段。所以输出将是:
id document_id phase_id user_id
1 1 3 1
3 1 1 1
4 2 3 2
我已经挣扎了好几个小时才弄清楚这个问题,但到目前为止还没有运气。
答案 0 :(得分:2)
假设您只想在第2阶段和第3阶段涉及user_id的任何情况下使用阶段3,那么您可以使用//
// Script file 2
// Database credentials test
//
dbmysqlconntest = PropertiesService.getDocumentProperties();
dbmysqlconntest.setProperties({
'mysqlUrl': 'jdbc:mysql://dbserverdev;databaseName=sample',
'mysqlUser': 'username',
'mysqlPassword': 'password'});
var keys = dbmysqlconntest.getKeys();
for (var i = 0; i < keys.length; i++) {
dbmysqlconntest.getProperty(keys[i]);
}
var url = dbmysqlconntest.getProperty(keys[0]);
var user = dbmysqlconntest.getProperty(keys[1]);
var password = dbmysqlconntest.getProperty(keys[2]);
function delProperty()
{
var userProperties = PropertiesService.getUserProperties();
userProperties.deleteProperty('mysqlUrl');
}
进行此操作,例如:
ROW_NUMBER()
答案 1 :(得分:1)
DECLARE @document_revision TABLE (
id INT IDENTITY(1,1),
document_id INT,
phase_id INT,
user_id INT
);
INSERT INTO @document_revision
(document_id, phase_id, user_id)
VALUES
(1, 3, 1),
(1, 2, 1),
(1, 1, 1),
(2, 3, 2),
(2, 2, 2),
-- To test a scenario where there is a proof and a submit with no transcribe phases and same document
(3, 2, 3),
(3, 1, 3),
-- To test a scenario where there is a transcribe and a submit with no proof phases and same document
(4, 3, 4),
(4, 1, 4),
-- To test a scenario where there is a proof and a submit with no transcribe phase (for document_id 5) but different document and same user as above
(5, 2, 4);
SELECT dr.id
, dr.document_id
, dr.phase_id
, dr.user_id
FROM @document_revision AS dr
WHERE NOT EXISTS ( SELECT 1
FROM @document_revision AS temp
-- Same user
WHERE temp.user_id = dr.user_id
-- Same document
AND temp.document_id = dr.document_id
-- To check if there is already a transcribe phase_id with the same user_id and document_id
AND temp.phase_id = 3
-- -- To check if there is already a proof phase_id with the same user_id and document_id
AND dr.phase_id = 2 )
结果:
id document_id phase_id user_id
1 1 3 1
3 1 1 1
4 2 3 2
6 3 2 3
7 3 1 3
8 4 3 4
9 4 1 4
10 5 2 4