在我的数据库中,我创建了表:
| ID |FAILURE | FAMILY_ID | FAILURE_DATE
| 1 | AAA | 1 | 2016-06-16 17:22:09.823
| 2 | BBB | 2 | 2016-06-16 17:17:09.853
| 3 | CCC | 3 | 2016-06-16 17:12:09.853
| 4 | DDD | 4 | 2016-06-16 17:07:09.853
| 5 | AAA | 2 | 2016-06-16 17:22:09.857
| 6 | BBB | 1 | 2016-06-16 17:17:09.857
| 7 | CCC | 2 | 2016-06-16 17:12:09.857
| 8 | DDD | 3 | 2016-06-16 17:07:09.857
| 9 | AAA | 4 | 2016-06-16 17:22:09.857
| 10 | BBB | 3 | 2016-06-16 17:17:09.857
| 11 | CCC | 2 | 2016-06-16 17:12:09.857
| 12 | DDD | 1 | 2016-06-16 17:07:09.857
| 13 | AAA | 2 | 2016-06-16 17:22:09.857
| 14 | BBB | 1 | 2016-06-16 17:17:09.857
| 15 | CCC | 4 | 2016-06-16 17:12:09.857
| 16 | DDD | 3 | 2016-06-16 17:07:09.860
| 17 | AAA | 4 | 2016-06-16 17:22:09.860
| 18 | BBB | 1 | 2016-06-16 17:17:09.860
| 19 | CCC | 2 | 2016-06-16 17:12:09.860
| 20 | DDD | 3 | 2016-06-16 17:07:09.860
使用此表格,我需要选择每个家庭在确定的时间段内出现的失败次数。
我在下面创建SQL代码:
SELECT
distinct main.id,
main.FAILURE,
main.FAMILY,
main.FAILURE_DATE
FROM
dbo.tblTest Main
iNNER JOIN
(
SELECT
tbl.FAILURE,
tbl.FAMILY,
COUNT( 1 ) as CNT
FROM
DBO.tblTest tbl
WHERE
tbl.FAILURE_DATE >= (SELECT DATEADD(MINUTE, -45, ( SELECT GETDATE())))
GROUP BY
tbl.FAILURE,
tbl.FAMILY
HAVING
COUNT ( 1 ) > 1
) result on result.FAMILY = Main.FAMILY AND result.FAILURE = Main.FAILURE
结果是:
|ID | FAILURE | FAMILY | FAILURE_DATE
|6 | BBB | 1 | 2016-06-16 17:17:09.857
|14 | BBB | 1 | 2016-06-16 17:17:09.857
|18 | BBB | 1 | 2016-06-16 17:17:09.860
|13 | AAA | 2 | 2016-06-16 17:22:09.857
|5 | AAA | 2 | 2016-06-16 17:22:09.857
|17 | AAA | 4 | 2016-06-16 17:22:09.860
|9 | AAA | 4 | 2016-06-16 17:22:09.857
那么,如何才能获得FAILURE和FAMILY的时间?
greate结果应为:
|ID | FAILURE | FAMILY | FAILURE_DATE
|6 | BBB | 1 | 2016-06-16 17:17:09.857
|13 | AAA | 2 | 2016-06-16 17:22:09.857
|9 | AAA | 4 | 2016-06-16 17:22:09.857
如何更改我的SQL查询以获得这样的结果?
答案 0 :(得分:2)
使用DATEADD(MI,-45,GETDATE())
时很难重新创建精确的结果,但我认为17:17应该根据您当前的时间,数据和发布时间而有效 - 您可以根据需要更换
这是一个通过CTE使用窗口函数的查询。
DECLARE @TEST TABLE (ID INT,
FAILURE VARCHAR(5),
FAMILY_ID INT,
FAILURE_DATE DATETIME)
INSERT INTO @TEST (ID,FAILURE,FAMILY_ID,FAILURE_DATE)
VALUES (1,'AAA',1,'2016-06-16 17:22:09.823'),
(2,'BBB',2,'2016-06-16 17:17:09.853'),
(3,'CCC',3,'2016-06-16 17:12:09.853'),
(4,'DDD',4,'2016-06-16 17:07:09.853'),
(5,'AAA',2,'2016-06-16 17:22:09.857'),
(6,'BBB',1,'2016-06-16 17:17:09.857'),
(7,'CCC',2,'2016-06-16 17:12:09.857'),
(8,'DDD',3 ,'2016-06-16 17:07:09.857'),
(9,'AAA',4,'2016-06-16 17:22:09.857'),
(10,'BBB',3,'2016-06-16 17:17:09.857'),
(11,'CCC',2,'2016-06-16 17:12:09.857'),
(12,'DDD',1,'2016-06-16 17:07:09.857'),
(13,'AAA',2,'2016-06-16 17:22:09.857'),
(14,'BBB',1,'2016-06-16 17:17:09.857'),
(15,'CCC',4,'2016-06-16 17:12:09.857'),
(16,'DDD',3,'2016-06-16 17:07:09.860'),
(17,'AAA',4,'2016-06-16 17:22:09.860'),
(18,'BBB',1,'2016-06-16 17:17:09.860'),
(19,'CCC',2,'2016-06-16 17:12:09.860'),
(20,'DDD',3,'2016-06-16 17:07:09.860')
;WITH CTE AS
(
SELECT *
,COUNT(2) OVER(PARTITION BY FAILURE,FAMILY_ID) AS recordCount
,ROW_NUMBER() OVER (PARTITION BY FAILURE,FAMILY_ID ORDER BY FAILURE_DATE,ID) AS rID
FROM @TEST
WHERE FAILURE_DATE >= '2016-06-16 17:17' --DATEADD(MI,-45,GETDATE())
)
SELECT ID, FAILURE, FAMILY_ID, FAILURE_DATE
FROM CTE
WHERE recordCount>1
AND rID=1
答案 1 :(得分:0)
由于我现在没有Microsoft SQL Server 2012,请尝试以下代码:
SELECT X.id, X.FAILURE, X.FAMILY, X.FAILURE_DATE
FROM (
SELECT
main.id,
main.FAILURE,
main.FAMILY,
main.FAILURE_DATE,
ROW_NUMBER() OVER(PARTITION BY main.FAILURE,main.FAMILY ORDER BY main.FAILURE_DATE) AS RM
FROM
dbo.tblTest Main
) X
WHERE X.RM=1
这是partition by的链接,可以帮助您