Link to home
Start Free TrialLog in
Avatar of Mauro Cazabonnet
Mauro CazabonnetFlag for United States of America

asked on

SQL Where not exists in same table

I'm trying to insert into table with the not in condition
but doesn't seem to work

Here is the data

VCENTER      ALERT
A-PROD      Alert
A-PROD      Warning
A-UAT      NULL
B-UAT      NULL
C-PROD      Warning
C-UAT      NULL
D-PROD      Alert
E-PROD      NULL

Here is my sql

DECLARE @AMRSVCentersIDX TABLE
            (            
                  [VCENTER] nvarchar(200),
                  [ALERT] varchar(20),      
                  [ID] int            
            )


            

            INSERT INTO @AMRSVCentersIDX
            SELECT DISTINCT tgh.[ENVIRONMENT], RIGHT(tha.AlertType, CHARINDEX(' ', REVERSE(tha.AlertType)) - 1) as [ALERT],
            ROW_NUMBER() OVER (Order by tgh.[ENVIRONMENT]) AS [ID]
            FROM [Reporting].[dbo].[tblGEN2_HOSTS] tgh WITH (NOLOCK)
            LEFT JOIN (SELECT DISTINCT [DataCenter], [AlertType] FROM [Reporting].[dbo].[tblHostAlerts] WITH (NOLOCK) WHERE [Duration] >= 10) tha ON tgh.[ENVIRONMENT] = tha.[DataCenter]
            WHERE tgh.[ENVIRONMENT] NOT LIKE '%DEV%' AND tgh.[REGION] = 'AMRS'
            AND tgh.[ENVIRONMENT] NOT IN (SELECT '''' + [VCENTER] + '''' as [ENVIRONMENT] FROM @AMRSVCentersIDX)

What I expect to be inserted into the temp table is

VCENTER      ALERT      ID
A-PROD      Alert      1
A-UAT      NULL      2
B-UAT      NULL      3
C-PROD      Warning      4
C-UAT      NULL      5
D-PROD      Alert      6
E-PROD      NULL      7

Any help would be most appreciated

Regards,
M
Avatar of Mauro Cazabonnet
Mauro Cazabonnet
Flag of United States of America image

ASKER

Note:
If an alert and warning exists for both I would only want to show the alert
ASKER CERTIFIED SOLUTION
Avatar of Guy Hengel [angelIII / a3]
Guy Hengel [angelIII / a3]
Flag of Luxembourg image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Awesome thx!!!!!!!

needed order by with partition by

Final result

INSERT INTO @AMRSVCentersIDX
            SELECT ENVIRONMENT, ALERT, ROW_NUMBER() OVER (Order by [ENVIRONMENT]) [ID]
            FROM (
            SELECT tgh.[ENVIRONMENT], RIGHT(tha.AlertType, CHARINDEX(' ', REVERSE(tha.AlertType)) - 1) as [ALERT],
                   Row_number()
                   OVER (
                        partition by tgh.[ENVIRONMENT]
                        ORDER BY tha.[DATACENTER]) [RN]
             FROM [Reporting].[dbo].[tblGEN2_HOSTS] tgh WITH (NOLOCK)
             LEFT JOIN (SELECT DISTINCT [DataCenter], [AlertType] FROM [Reporting].[dbo].[tblHostAlerts] WITH (NOLOCK) WHERE [Duration] >= 10) tha ON tgh.[ENVIRONMENT] = tha.[DataCenter]
             WHERE tgh.[ENVIRONMENT] NOT LIKE '%DEV%' AND tgh.[REGION] = 'AMRS' AND tgh.[ENVIRONMENT] NOT IN (SELECT [VCENTER] FROM @AMRSVCentersIDX)

                   ) x
                   WHERE x.rn = 1