我有这个问题:
SELECT
[TesterName]
,[MTDate]
,[Login]
,[TesterStatus]
,[TesterState]
,[Workweek]
FROM
[Montester_Preprod].[dbo].[pub_MontesterData]
WHERE
SiteName LIKE '%HVC%'
AND TesterTypeName = 'CMT'
AND
( TesterName NOT LIKE '%263%'
AND TesterName NOT LIKE '%371%'
AND TesterName NOT LIKE '%201%'
AND TesterName NOT LIKE '%521%'
AND TesterName NOT LIKE '%588%'
AND TesterName NOT LIKE '%299%'
AND TesterName NOT LIKE '%284%'
AND TesterName NOT LIKE '%493%'
AND TesterName NOT LIKE '%386%')
返回此数据:
TesterName MTDate Login TesterStatus TesterState Workweek
CMT0062 2017-02-22 00:15:00.000 czma 1 3 201708
CMT0062 2017-02-22 00:30:00.000 czma 1 3 201708
CMT0062 2017-02-22 00:45:00.000 czma 1 3 201708
CMT0062 2017-02-22 01:00:00.000 czma 1 3 201708
CMT0062 2017-02-22 01:15:00.000 czma 1 3 201708
CMT0062 2017-02-22 01:30:00.000 czma 1 2 201708
CMT0062 2017-02-22 01:45:00.000 czma 1 7 201708
CMT0327G1 2017-03-28 11:00:00.000 hadargox 1 7 201713
CMT0289G1 2017-03-28 11:00:00.000 oaharoni 1 7 201713
CMT0748G1 2017-03-28 11:00:00.000 vfarcas 1 7 201713
CMT0628G1 2017-03-28 11:00:00.000 obarhum 2 7 201713
CMT0534G1 2017-03-28 11:00:00.000 1 1 201713
CMT0062G1 2017-03-28 11:00:00.000 hadargox 1 7 201713
我试图检索和失败的是" MTDate" (搜索相同的完整日期+" testername"相等(" testername"应该没有" G1"最后,例如CMT0062 = CMT0062(G1) )如果两个搜索都是真的,则检查测试者状态,如果它们都是1"如果没有"打印" 1,那么#34; TesterStatus来自没有" G1" at的行testerName。
对于每个" MTDate"我有两个相同的日期,例如CMT0371和CMT0371G1。
答案 0 :(得分:0)
查看查询的真值表,您会看到一些不明显的东西,但会简化您的逻辑。
如果,当MTDate列也匹配时,您总是返回CMTxxxx中的值,您将始终返回正确的值 - 无需进行比较。
要获取应为您的案例显示的所有行,我的查询将如下所示:
SELECT
[TesterName]
,[MTDate]
,[Login]
,[TesterStatus]
,[TesterState]
,[Workweek]
FROM
[Montester_Preprod].[dbo].[pub_MontesterData] pmd
WHERE
SiteName LIKE '%HVC%'
AND TesterTypeName = 'CMT'
AND
( TesterName NOT LIKE '%263%'
AND TesterName NOT LIKE '%371%'
AND TesterName NOT LIKE '%201%'
AND TesterName NOT LIKE '%521%'
AND TesterName NOT LIKE '%588%'
AND TesterName NOT LIKE '%299%'
AND TesterName NOT LIKE '%284%'
AND TesterName NOT LIKE '%493%'
AND TesterName NOT LIKE '%386%'
)
AND NOT (
pmd.TesterName Like '%G1'
AND EXISTS (
SELECT *
FROM [Montester_Preprod].[dbo].[pub_MontesterData] pm2
WHERE pm2.TesterName = Left(pmd.TesterName, Len(pmd.TesterName)-2))
AND pm2.MTDate = pmd.MTDate
)