我有两个表(SQL服务器):
errorcode(code, utctime, fileversion)
errordescr(code, fileversion, description)
errorcode
包含给定utctime的故障历史记录。
错误映射到errordescr表中的描述。
如果errordescr
表中没有该版本,我想使用默认的fileversion值(defaultversion),因此我可以获得给定错误的默认描述。
- 当errordescr
中存在错误的文件版本,但代码未在errordescr
中注册时,我想为描述返回NULL,我不想获取默认值'defaultversion'中的描述。
我的问题是,当我执行查询时,即使在正确版本中有描述,我也会从'defaultversion'获得匹配。
由于
SELECT TOP 50000 sn, utctime, mydb.dbo.error.code, description FROM mydb.dbo.error
LEFT OUTER JOIN mydb.dbo.errordescr
ON(
(
mydb.dbo.error.fileversion=mydb.dbo.errordescr.fileversion
OR
mydb.dbo.errordescr.fileversion='defaultversion'
)
AND
mydb.dbo.error.code=mydb.dbo.errordescr.code
)
ORDER BY utctime DESC
Exemples: 错误的条目:
1. code: 001, utctime: 1970-01-01 00:00:00:000, fileversion: v1.0
2. code: 002, utctime: 1970-01-01 00:00:00:000, fileversion: v2.0
3. code: 003, utctime: 1970-01-01 00:00:00:000, fileversion: v3.0
4. code: 002, utctime: 2000-01-01 00:00:00:000, fileversion: v1.0
错误代码中的条目:
1. code: 001, fileversion: v1.0, description: "error code #1 from file v1.0"
2. code: 002, fileversion: v1.0, description: "error code #2 from file v2.0"
3. code: 003, fileversion: defaultversion, description: "error code #3 from default"
结果可能是这样的:
显示所有错误:
1. code: 001, utctime: 1970-01-01 00:00:00:000, fileversion: v1.0, description: "error code #1 from file v1.0"
2. code: 002, utctime: 1970-01-01 00:00:00:000, fileversion: v2.0, description: "error code #2 from file v2.0"
3. code: 003, utctime: 1970-01-01 00:00:00:000, fileversion: v3.0, description:"error code #3 from default"
4. code: 002, utctime: 2000-01-01 00:00:00:000, fileversion: v1.0, description: NULL
答案 0 :(得分:1)
尝试:
SELECT TOP 50000 sn, utctime, mydb.dbo.error.code, case when description is null then 'defaultversion' else description End
FROM mydb.dbo.error
LEFT OUTER JOIN mydb.dbo.errordescr
ON mydb.dbo.error.fileversion=mydb.dbo.errordescr.fileversion
AND mydb.dbo.error.code=mydb.dbo.errordescr.code
ORDER BY utctime DESC