加入两个查询(已部分完成)

时间:2012-07-17 14:55:27

标签: sql-server join case

我正在尝试将两个查询一起加入。这两个查询分别正常运行。我从查询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

0 个答案:

没有答案