我正在尝试将这两个查询连接在一起。这两个查询分别正常运行。我已经从查询 2 中获得了 CASE 语句来处理查询 1。
在某些背景下,这个查询是针对一个商业智能程序运行的,该程序在自动运行插入时创建图形/图表。现在,当没有新数据插入到特定 IM.NAME 的表中时,程序会回溯到最后一条记录,这会使图表结果产生偏差。当 5 个 IM.NAME 字段之一未根据下面的主查询返回时,我需要一个“1”来打印出来。我已经包含了一个 SQLfiddle,显示了第二个查询:http ://sqlfiddle.com/#!3/2cdbd/1
SELECT DISTINCT
SA.DELETED,
PAT.VERSION,
PAT.PATTERNDATE,
SA.AGENT_VERSION,
SCL.COMPUTER_NAME AS Computer_Name,
SCO.OPERATION_SYSTEM AS Operation_System,
dateadd(s,convert(bigint,SA.CREATION_TIME)/1000,
'01-01-1970 00:00:00') CREATION_DTTM,
dateadd(s,convert(bigint,SA.LAST_UPDATE_TIME)/1000,
'01-01-1970 00:00:00') Lastupdatetime,
DATEADD(s, convert(bigint,LAST_SCAN_TIME)/1000,
'01-01-1970 00:00:00')LAST_SCAN_TIME,
PAT.PATTERNDATE AS Pattern_Date,
SCL.USER_NAME AS User_Name,
VSC.IP_ADDR1_TEXT AS IP_Add,
CASE WHEN IM.NAME LIKE '%-FRN%' THEN IM.NAME
WHEN IM.NAME LIKE '%-BCK%' THEN IM.NAME
WHEN IM.NAME LIKE '%-PPP%' THEN IM.NAME
WHEN IM.NAME LIKE '%-MES%' THEN IM.NAME
WHEN IM.NAME LIKE '%-MMT%' THEN IM.NAME
ELSE '1' END AS Group_Name,
IM.NAME as test
FROM ((((SEM_AGENT SA
INNER JOIN SEM_CLIENT SCL
ON ((SA.COMPUTER_ID=SCL.COMPUTER_ID)
AND (SA.DOMAIN_ID=SCL.DOMAIN_ID))
AND (SA.GROUP_ID=SCL.GROUP_ID))
INNER JOIN SEM_COMPUTER SCO
ON ((SA.COMPUTER_ID=SCO.COMPUTER_ID)
AND (SA.DOMAIN_ID=SCO.DOMAIN_ID))
AND (SA.DELETED=SCO.DELETED))
INNER JOIN PATTERN PAT
ON SA.PATTERN_IDX=PAT.PATTERN_IDX)
INNER JOIN IDENTITY_MAP IM
ON SCL.GROUP_ID=IM.ID)
INNER JOIN V_SEM_COMPUTER VSC
ON SCO.COMPUTER_ID=VSC.COMPUTER_ID
AND SA.DELETED=0
WHERE PAT.Patterndate < (SELECT MAX(Patterndate) -1 FROM Pattern)
AND SCO.OPERATION_SYSTEM NOT LIKE '%2000%'
ORDER BY Computer_Name
Select (CASE WHEN IM.NAME LIKE '%-FRN%' THEN IM.NAME
WHEN IM.NAME LIKE '%-BCK%' THEN IM.NAME
WHEN IM.NAME LIKE '%-PPP%' THEN IM.NAME
WHEN IM.NAME LIKE '%-MES%' THEN IM.NAME
WHEN IM.NAME LIKE '%-MMT%' THEN IM.NAME
ELSE '1' END) AS Group_Name,
tagTypes.tagType,
IM.NAME
From
(
Select '%-FRN%' AS tagType
UNION
Select '%-BCK%' AS tagType
UNION
Select '%-PPP%' AS tagType
UNION
Select '%-MES%' AS tagType
UNION
Select '%-MMT%' AS tagType
) AS tagTypes
left join
IDENTITY_MAP IM
on IM.NAME like tagTypes.tagType