从SQL表中获取数据

时间:2014-09-10 18:09:12

标签: sql sql-server

我有一个包含以下四列的SQL Server表:

ID, TCname, Status, Date

我将测试用例名称,状态和日期存储在此表中。我想从这张表中得到以下数据。

  1. 今天没有添加任何新的测试用例。(测试用例昨天不存在,但今天出现)
  2. 测试用例昨天失败但今天过去了。
  3. 同样的测试用例昨天和今天都失败了。
  4. 由于

3 个答案:

答案 0 :(得分:0)

假设您只在表中插入新记录而不更新旧记录。

第一个例子:

SELECT COUNT(ID) FROM [TABLE_NAME] WHERE Date >= DATEADD(dd, DATEDIFF(dd,0,GETDATE()), 0)

第二个例子:

SELECT ID, TCName, Status, Date 
  FROM [TABLE_NAME] 
 WHERE TCname IN (SELECT TCname 
                    FROM [TABLE_NAME] 
                   WHERE Date >=  DATEADD(dd, DATEDIFF(dd,1,GETDATE()), 0)  
                     AND Date <= DATEADD(dd, DATEDIFF(dd,0,GETDATE()), 0) 
                     AND Status = 'Fail'
                 ) 
   AND Status = 'Pass'`  

第三个例子:

SELECT ID, TCName, Status, Date 
  FROM [TABLE_NAME] 
 WHERE TCname IN (SELECT TCname 
                    FROM [TABLE_NAME] 
                   WHERE Date >=  DATEADD(dd, DATEDIFF(dd,1,GETDATE()), 0)  
                     AND Date <= DATEADD(dd, DATEDIFF(dd,0,GETDATE()), 0) 
                     AND Status = 'Fail'
                ) 
   AND Status = 'Fail'`

答案 1 :(得分:0)

假设Date列的类型为DATE(不是DATETIME),并且每天为每个测试用例插入一条记录(当天存在,显然)

这是一个小提琴,因此您可以验证它是否符合您的需要: http://sqlfiddle.com/#!6/1c627/5

SELECT COUNT(*) FROM cases c WHERE NOT EXISTS(SELECT 1 FROM cases sub_c 
WHERE sub_c.TCname = c.TCname
AND sub_c.Date <= DATEADD(day,DATEDIFF(day, 1, GETDATE()), 0))

SELECT TCname FROM cases c WHERE EXISTS(SELECT 1 FROM cases sub_c 
WHERE sub_c.TCname = c.TCname 
AND sub_c.Date = DATEADD(day,DATEDIFF(day, 0, GETDATE()), 0) 
AND sub_c.Status = 'PASS') 
AND EXISTS(SELECT 1 FROM cases sub_c 
WHERE sub_c.TCname = c.TCname
AND sub_c.Date = DATEADD(day,DATEDIFF(day, 1, GETDATE()), 0) 
AND sub_c.Status = 'FAIL')
GROUP BY c.TCname

SELECT TCname FROM cases c WHERE EXISTS(SELECT 1 FROM cases sub_c 
WHERE sub_c.TCname = c.TCname 
AND sub_c.Date = DATEADD(day,DATEDIFF(day, 0, GETDATE()), 0) 
AND sub_c.Status = 'FAIL') 
AND EXISTS(SELECT 1 FROM cases sub_c 
WHERE sub_c.TCname = c.TCname
AND sub_c.Date = DATEADD(day,DATEDIFF(day, 1, GETDATE()), 0) 
AND sub_c.Status = 'FAIL')
GROUP BY c.TCname

答案 2 :(得分:0)

假设您的日期列为DATETIME

1.今天新增的测试案例没有增加

SELECT COUNT (*) AS [New Test Case Count]
FROM Testcase 
WHERE DATE >= DATEADD(dd, 0, DATEDIFF(dd, 0, GETDATE()))

2.昨天失败但今天过世的案件

SELECT DISTINCT TCname
FROM Testcase 
WHERE TCName IN (SELECT TCName 
                 FROM Testcase t 
                 WHERE Date BETWEEN DATEADD(dd, 0, DATEDIFF(dd, 1, GETDATE())) 
                                AND DATEADD(dd, 0, DATEDIFF(dd, 0, GETDATE())) 
                 AND t.Status = 'FAIL')
AND DATE >= DATEADD(dd, 0, DATEDIFF(dd, 0, GETDATE()))
AND Status = 'PASS'

3.昨天和今天的测试案件都失败了。 SQL Fiddle Demo

SELECT DISTINCT TCname
FROM Testcase 
WHERE TCName IN (SELECT TCName 
                 FROM Testcase t 
                 WHERE t.Date >= DATEADD(dd, 0, DATEDIFF(dd, 1, GETDATE())) 
                 AND t.Date < DATEADD(dd, 0, DATEDIFF(dd, 0, GETDATE())) 
                 AND t.Status = 'FAIL')
AND DATE >= DATEADD(dd, 0, DATEDIFF(dd, 0, GETDATE()))
AND Status = 'FAIL'