-1

データが返されない場合に、1 つまたはすべての行に「0」が印刷された行を返すように、このクエリを変更しようとしています。ISNULL() を使用してみましたが、どこにも行きません。

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,
IM.NAME AS Group_Name

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) -2 FROM Pattern)
AND SCO.OPERATION_SYSTEM NOT LIKE '%2000%'
ORDER BY Computer_Name
4

4 に答える 4

3

レコードがない場合に NULL 行を返す 1 つの方法は、

  • SQL を CTE に入れる
  • あなたのCTEから選択してください
  • CTE に対して NOT EXISTS を使用したデフォルト値の結合。


WITH CTE AS (
   SELECT "Computer Name".  ... 
)
SELECT * FROM CTE
UNION ALL
SELECT '', 0, 0, '' 
WHERE 
    NOT EXISTS(SELECT * FROM CTE)
ORDER BY 
     "Computer Name"

DEMO が「null」レコードを返すおよびDEMO がデータを返すを参照してください。
2 つのデモの違いはWhere 1=2Where 1=1CTE にあります。

于 2012-07-13T18:40:13.033 に答える
0

私はそれを3つのステートメントに分けました:

DECLARE @RecordCount int;

WITH Temp_CTE
AS
(
SELECT DISTINCT EM_AGENT.DELETED
  ,PATTERN.VERSION 
  ,PATTERN.PATTERNDATE
  ,SEM_AGENT.AGENT_VERSION
  ,SEM_CLIENT.COMPUTER_NAME "Computer Name"
  ,SEM_COMPUTER.OPERATION_SYSTEM "Operation System"
  ,dateadd(s,convert(bigint,SEM_AGENT.CREATION_TIME)/1000
    ,'01-01-1970 00:00:00') CREATION_DTTM
  , dateadd(s,convert(bigint,SEM_AGENT.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"
  , PATTERN.PATTERNDATE "Pattern Date"
  , SEM_CLIENT.USER_NAME "User Name"
  , V_SEM_COMPUTER.IP_ADDR1_TEXT "IP Address"
  , IDENTITY_MAP.NAME "Group Name"
FROM (((("SEM_AGENT" "SEM_AGENT" 
INNER JOIN "SEM_CLIENT" "SEM_CLIENT" 
  ON (("SEM_AGENT"."COMPUTER_ID"="SEM_CLIENT"."COMPUTER_ID") 
  AND ("SEM_AGENT"."DOMAIN_ID"="SEM_CLIENT"."DOMAIN_ID")) 
  AND ("SEM_AGENT"."GROUP_ID"="SEM_CLIENT"."GROUP_ID")) 
INNER JOIN "SEM_COMPUTER"   "SEM_COMPUTER" 
  ON (("SEM_AGENT"."COMPUTER_ID"="SEM_COMPUTER"."COMPUTER_ID") 
  AND ("SEM_AGENT"."DOMAIN_ID"="SEM_COMPUTER"."DOMAIN_ID")) 
  AND ("SEM_AGENT"."DELETED"="SEM_COMPUTER"."DELETED"))
INNER JOIN "PATTERN" "PATTERN" 
  ON "SEM_AGENT"."PATTERN_IDX"="PATTERN"."PATTERN_IDX") 
INNER JOIN "IDENTITY_MAP" "IDENTITY_MAP" 
  ON "SEM_CLIENT"."GROUP_ID"="IDENTITY_MAP"."ID") 
INNER JOIN "V_SEM_COMPUTER" "V_SEM_COMPUTER" 
  ON "SEM_COMPUTER"."COMPUTER_ID"="V_SEM_COMPUTER"."COMPUTER_ID" 
  AND "SEM_AGENT"."DELETED"=0
WHERE Pattern.Patterndate < (select max(Patterndate) -2 from Pattern)
  AND SEM_COMPUTER.OPERATION_SYSTEM NOT LIKE '%2000%'
)
SELECT @RecordCount = COUNT(*) FROM Temp_CTE;

IF @RecordCount=0 THEN
  SELECT 0
ELSE
  SELECT * FROM Temp_CTE ORDER BY [Computer Name];
于 2012-07-13T18:59:36.810 に答える
0

条件に一致する行があるかどうかを確認し、そうでない場合は 0 を選択し、そうでない場合はクエリ全体を選択します。

SELECT TOP 1 *
FROM (((("SEM_AGENT" "SEM_AGENT" INNER JOIN "SEM_CLIENT" "SEM_CLIENT"  
  ON (("SEM_AGENT"."COMPUTER_ID"="SEM_CLIENT"."COMPUTER_ID")  
  AND ("SEM_AGENT"."DOMAIN_ID"="SEM_CLIENT"."DOMAIN_ID"))  
  AND ("SEM_AGENT"."GROUP_ID"="SEM_CLIENT"."GROUP_ID")) INNER JOIN "SEM_COMPUTER"   "SEM_COMPUTER"  
  ON (("SEM_AGENT"."COMPUTER_ID"="SEM_COMPUTER"."COMPUTER_ID")  
  AND ("SEM_AGENT"."DOMAIN_ID"="SEM_COMPUTER"."DOMAIN_ID"))  
  AND ("SEM_AGENT"."DELETED"="SEM_COMPUTER"."DELETED")) INNER JOIN "PATTERN" "PATTERN"  
  ON "SEM_AGENT"."PATTERN_IDX"="PATTERN"."PATTERN_IDX") INNER JOIN "IDENTITY_MAP" "IDENTITY_MAP"  
  ON "SEM_CLIENT"."GROUP_ID"="IDENTITY_MAP"."ID") INNER JOIN "V_SEM_COMPUTER" "V_SEM_COMPUTER"  
  ON "SEM_COMPUTER"."COMPUTER_ID"="V_SEM_COMPUTER"."COMPUTER_ID"  
  AND "SEM_AGENT"."DELETED"=0 
WHERE Pattern.Patterndate < (select max(Patterndate) -2 from Pattern) 
AND SEM_COMPUTER.OPERATION_SYSTEM NOT LIKE '%2000%' 

IF @@ROWCOUNT = 0
    SELECT 0
ELSE 
BEGIN
    SELECT DISTINCT "SEM_AGENT"."DELETED" 
      ,"PATTERN"."VERSION"  
      ,"PATTERN"."PATTERNDATE" 
      ,"SEM_AGENT"."AGENT_VERSION" 
      ,"SEM_CLIENT"."COMPUTER_NAME" "Computer Name" 
      , "SEM_COMPUTER"."OPERATION_SYSTEM" "Operation System" 
      ,dateadd(s,convert(bigint,"SEM_AGENT"."CREATION_TIME")/1000,'01-01-1970 00:00:00')     
        CREATION_DTTM 
      , dateadd(s,convert(bigint,"SEM_AGENT"."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" 
      , "PATTERN"."PATTERNDATE" "Pattern Date" 
      , "SEM_CLIENT"."USER_NAME" "User Name" 
      , "V_SEM_COMPUTER"."IP_ADDR1_TEXT" "IP Address" 
      , "IDENTITY_MAP"."NAME" "Group Name" 
    FROM (((("SEM_AGENT" "SEM_AGENT" INNER JOIN "SEM_CLIENT" "SEM_CLIENT"  
      ON (("SEM_AGENT"."COMPUTER_ID"="SEM_CLIENT"."COMPUTER_ID")  
      AND ("SEM_AGENT"."DOMAIN_ID"="SEM_CLIENT"."DOMAIN_ID"))  
      AND ("SEM_AGENT"."GROUP_ID"="SEM_CLIENT"."GROUP_ID")) INNER JOIN "SEM_COMPUTER"   "SEM_COMPUTER"  
      ON (("SEM_AGENT"."COMPUTER_ID"="SEM_COMPUTER"."COMPUTER_ID")  
      AND ("SEM_AGENT"."DOMAIN_ID"="SEM_COMPUTER"."DOMAIN_ID"))  
      AND ("SEM_AGENT"."DELETED"="SEM_COMPUTER"."DELETED")) INNER JOIN "PATTERN" "PATTERN"  
      ON "SEM_AGENT"."PATTERN_IDX"="PATTERN"."PATTERN_IDX") INNER JOIN "IDENTITY_MAP" "IDENTITY_MAP"  
      ON "SEM_CLIENT"."GROUP_ID"="IDENTITY_MAP"."ID") INNER JOIN "V_SEM_COMPUTER" "V_SEM_COMPUTER"  
      ON "SEM_COMPUTER"."COMPUTER_ID"="V_SEM_COMPUTER"."COMPUTER_ID"  
      AND "SEM_AGENT"."DELETED"=0 
    WHERE Pattern.Patterndate < (select max(Patterndate) -2 from Pattern) 
    AND SEM_COMPUTER.OPERATION_SYSTEM NOT LIKE '%2000%' 
    ORDER BY "Computer Name" 
END

内部結合によって結果の数が変わらないことがわかっている場合は、効率のために最初のクエリからそれらを削除できます。

@@ROWCOUNT を使用したくない場合は、COUNT() を使用することもできます。

DECLARE @count INT

SELECT @count = COUNT(*)
FROM (
SELECT TOP 1 *
FROM (((("SEM_AGENT" "SEM_AGENT" INNER JOIN "SEM_CLIENT" "SEM_CLIENT"  
  ON (("SEM_AGENT"."COMPUTER_ID"="SEM_CLIENT"."COMPUTER_ID")  
  AND ("SEM_AGENT"."DOMAIN_ID"="SEM_CLIENT"."DOMAIN_ID"))  
  AND ("SEM_AGENT"."GROUP_ID"="SEM_CLIENT"."GROUP_ID")) INNER JOIN "SEM_COMPUTER"   "SEM_COMPUTER"  
  ON (("SEM_AGENT"."COMPUTER_ID"="SEM_COMPUTER"."COMPUTER_ID")  
  AND ("SEM_AGENT"."DOMAIN_ID"="SEM_COMPUTER"."DOMAIN_ID"))  
  AND ("SEM_AGENT"."DELETED"="SEM_COMPUTER"."DELETED")) INNER JOIN "PATTERN" "PATTERN"  
  ON "SEM_AGENT"."PATTERN_IDX"="PATTERN"."PATTERN_IDX") INNER JOIN "IDENTITY_MAP" "IDENTITY_MAP"  
  ON "SEM_CLIENT"."GROUP_ID"="IDENTITY_MAP"."ID") INNER JOIN "V_SEM_COMPUTER" "V_SEM_COMPUTER"  
  ON "SEM_COMPUTER"."COMPUTER_ID"="V_SEM_COMPUTER"."COMPUTER_ID"  
  AND "SEM_AGENT"."DELETED"=0 
WHERE Pattern.Patterndate < (select max(Patterndate) -2 from Pattern) 
AND SEM_COMPUTER.OPERATION_SYSTEM NOT LIKE '%2000%' ) temp

IF @count = 0
SELECT 0
ELSE 
BEGIN
    SELECT DISTINCT "SEM_AGENT"."DELETED" 
      ,"PATTERN"."VERSION"  
      ,"PATTERN"."PATTERNDATE" 
      ,"SEM_AGENT"."AGENT_VERSION" 
      ,"SEM_CLIENT"."COMPUTER_NAME" "Computer Name" 
      , "SEM_COMPUTER"."OPERATION_SYSTEM" "Operation System" 
      ,dateadd(s,convert(bigint,"SEM_AGENT"."CREATION_TIME")/1000,'01-01-1970 00:00:00')     
        CREATION_DTTM 
      , dateadd(s,convert(bigint,"SEM_AGENT"."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" 
      , "PATTERN"."PATTERNDATE" "Pattern Date" 
      , "SEM_CLIENT"."USER_NAME" "User Name" 
      , "V_SEM_COMPUTER"."IP_ADDR1_TEXT" "IP Address" 
      , "IDENTITY_MAP"."NAME" "Group Name" 
    FROM (((("SEM_AGENT" "SEM_AGENT" INNER JOIN "SEM_CLIENT" "SEM_CLIENT"  
      ON (("SEM_AGENT"."COMPUTER_ID"="SEM_CLIENT"."COMPUTER_ID")  
      AND ("SEM_AGENT"."DOMAIN_ID"="SEM_CLIENT"."DOMAIN_ID"))  
      AND ("SEM_AGENT"."GROUP_ID"="SEM_CLIENT"."GROUP_ID")) INNER JOIN "SEM_COMPUTER"   "SEM_COMPUTER"  
      ON (("SEM_AGENT"."COMPUTER_ID"="SEM_COMPUTER"."COMPUTER_ID")  
      AND ("SEM_AGENT"."DOMAIN_ID"="SEM_COMPUTER"."DOMAIN_ID"))  
      AND ("SEM_AGENT"."DELETED"="SEM_COMPUTER"."DELETED")) INNER JOIN "PATTERN" "PATTERN"  
      ON "SEM_AGENT"."PATTERN_IDX"="PATTERN"."PATTERN_IDX") INNER JOIN "IDENTITY_MAP" "IDENTITY_MAP"  
      ON "SEM_CLIENT"."GROUP_ID"="IDENTITY_MAP"."ID") INNER JOIN "V_SEM_COMPUTER" "V_SEM_COMPUTER"  
      ON "SEM_COMPUTER"."COMPUTER_ID"="V_SEM_COMPUTER"."COMPUTER_ID"  
      AND "SEM_AGENT"."DELETED"=0 
    WHERE Pattern.Patterndate < (select max(Patterndate) -2 from Pattern) 
    AND SEM_COMPUTER.OPERATION_SYSTEM NOT LIKE '%2000%' 
    ORDER BY "Computer Name" 
END
于 2012-07-13T18:33:22.443 に答える
0

0 行以上を返すクエリがあります。0 行が返された場合は、別の行を含める必要があります。

これにより、クエリの計算が複雑になります。これを行う 1 つの方法は、必要な行を、その行がどこから来たかを示すフラグと共に結合することです。次に、windows 関数を使用してその情報をすべての行で共有し、where 句を使用して最終行をフィルター処理します。

select *
from (select t.*, sum(FromQ) over (partition by NULL) as numQ
      from (select *, 1 as FromQ
            from <query>
            union all
            select 0,0,0,0 . . . , 0 as FromQ
           ) t
      ) t
where numQ = 0 or numQ > 0 and FromQ <> 0

また、グループを結合し、FromQ で逆に並べ替え、最初でない場合はアプリケーション内の行を無視することもできます。

于 2012-07-13T18:39:37.923 に答える