根据字段值排除项目
exclude items based on field value
我使用以下脚本创建了一个视图,但其中一个字段正在撤回我不需要的信息。在 'IsServer' 列中,服务器项标记为 1,非服务器项标记为 0。是否可以创建视图并排除标记为 1 的服务器项在 IsServer 列?我不确定如何将它构建到创建视图脚本中。
CREATE VIEW [dbo].[vw_Cherwell_Machines] AS
SELECT dbo.rptComputers.HostName,
dbo.rptComputers.Vendor,
dbo.tblComputer.IsVirtual,
dbo.rptComputers.ProcessorCount,
dbo.tblComputer.IsServer,
dbo.rptComputers.BiosSerialNumber,
dbo.rptComputers.PhysicalMemory,
dbo.rptComputers.ProcessorType,
dbo.rptComputers.ProcessorSpeed,
dbo.rptComputers.MostFrequentUser,
dbo.tblComputer.Domain,
dbo.rptComputers.ClientVersion,
dbo.rptComputers.ClientInstallDate,
dbo.rptComputers.ClientConfigurationName,
dbo.rptComputers.BiosVersion,
dbo.rptComputers.BiosDate,
dbo.rptComputers.Manufacturer,
dbo.rptComputers.Model,
dbo.rptComputers.IsPortable,
dbo.rptComputers.OperatingSystem,
dbo.tblComputer.OSServicePack,
dbo.rptComputers.ComputerStatusCode,
dbo.rptComputers.IPAddress AS 'MultipleIPAddress',
CASE WHEN PATINDEX ('%[, ]%',dbo.rptComputers.IPAddress) > 0 THEN
left(dbo.rptComputers.IPAddress,(PATINDEX('%[, ]%',dbo.rptComputers.IPAddress))-1)
ELSE dbo.rptComputers.IPAddress
END AS 'IPAddress',
dbo.rptComputers.LastScanDate
FROM dbo.rptComputers INNER JOIN dbo.tblComputer ON dbo.rptComputers.CID = dbo.tblComputer.CID
AND dbo.rptComputers.ComputerID = dbo.tblComputer.ComputerID
GROUP BY dbo.rptComputers.HostName,
dbo.tblComputer.IsVirtual,
dbo.rptComputers.Vendor,
dbo.rptComputers.ProcessorCount,
dbo.rptComputers.BiosSerialNumber,
dbo.tblComputer.IsServer,
dbo.rptComputers.PhysicalMemory,
dbo.rptComputers.ProcessorType,
dbo.rptComputers.ProcessorSpeed,
dbo.rptComputers.MostFrequentUser,
dbo.tblComputer.Domain,
dbo.rptComputers.ClientVersion,
dbo.rptComputers.ClientInstallDate,
dbo.rptComputers.ClientConfigurationName,
dbo.rptComputers.BiosVersion,
dbo.rptComputers.BiosDate,
dbo.rptComputers.Manufacturer,
dbo.rptComputers.Model,
dbo.rptComputers.IsPortable,
dbo.rptComputers.OperatingSystem,
dbo.tblComputer.OSServicePack,
dbo.rptComputers.ComputerStatusCode,
dbo.rptComputers.IPAddress,
dbo.rptComputers.LastScanDate
您需要过滤视图;即,带有 WHERE
子句的视图。像这样的事情(我在评论中标记了我添加到您的代码中的 begin/end):
CREATE VIEW [dbo].[vw_Cherwell_Machines] AS
SELECT dbo.rptComputers.HostName,
dbo.rptComputers.Vendor,
dbo.tblComputer.IsVirtual,
dbo.rptComputers.ProcessorCount,
dbo.tblComputer.IsServer,
dbo.rptComputers.BiosSerialNumber,
dbo.rptComputers.PhysicalMemory,
dbo.rptComputers.ProcessorType,
dbo.rptComputers.ProcessorSpeed,
dbo.rptComputers.MostFrequentUser,
dbo.tblComputer.Domain,
dbo.rptComputers.ClientVersion,
dbo.rptComputers.ClientInstallDate,
dbo.rptComputers.ClientConfigurationName,
dbo.rptComputers.BiosVersion,
dbo.rptComputers.BiosDate,
dbo.rptComputers.Manufacturer,
dbo.rptComputers.Model,
dbo.rptComputers.IsPortable,
dbo.rptComputers.OperatingSystem,
dbo.tblComputer.OSServicePack,
dbo.rptComputers.ComputerStatusCode,
dbo.rptComputers.IPAddress AS 'MultipleIPAddress',
CASE WHEN PATINDEX ('%[, ]%',dbo.rptComputers.IPAddress) > 0 THEN
left(dbo.rptComputers.IPAddress,(PATINDEX('%[, ]%',dbo.rptComputers.IPAddress))-1)
ELSE dbo.rptComputers.IPAddress
END AS 'IPAddress',
dbo.rptComputers.LastScanDate
FROM dbo.rptComputers INNER JOIN dbo.tblComputer ON dbo.rptComputers.CID = dbo.tblComputer.CID
AND dbo.rptComputers.ComputerID = dbo.tblComputer.ComputerID
/*begin change*/
WHERE tblComputer.IsServer = 0
/*end change*/
GROUP BY dbo.rptComputers.HostName,
dbo.tblComputer.IsVirtual,
dbo.rptComputers.Vendor,
dbo.rptComputers.ProcessorCount,
dbo.rptComputers.BiosSerialNumber,
dbo.tblComputer.IsServer,
dbo.rptComputers.PhysicalMemory,
dbo.rptComputers.ProcessorType,
dbo.rptComputers.ProcessorSpeed,
dbo.rptComputers.MostFrequentUser,
dbo.tblComputer.Domain,
dbo.rptComputers.ClientVersion,
dbo.rptComputers.ClientInstallDate,
dbo.rptComputers.ClientConfigurationName,
dbo.rptComputers.BiosVersion,
dbo.rptComputers.BiosDate,
dbo.rptComputers.Manufacturer,
dbo.rptComputers.Model,
dbo.rptComputers.IsPortable,
dbo.rptComputers.OperatingSystem,
dbo.tblComputer.OSServicePack,
dbo.rptComputers.ComputerStatusCode,
dbo.rptComputers.IPAddress,
dbo.rptComputers.LastScanDate
Documentation: https://docs.microsoft.com/en-us/sql/relational-databases/indexes/create-filtered-indexes?view=sql-server-ver15 Be sure to see the "limitations" section there, in case there's something in your environment which will not work here.
我使用以下脚本创建了一个视图,但其中一个字段正在撤回我不需要的信息。在 'IsServer' 列中,服务器项标记为 1,非服务器项标记为 0。是否可以创建视图并排除标记为 1 的服务器项在 IsServer 列?我不确定如何将它构建到创建视图脚本中。
CREATE VIEW [dbo].[vw_Cherwell_Machines] AS
SELECT dbo.rptComputers.HostName,
dbo.rptComputers.Vendor,
dbo.tblComputer.IsVirtual,
dbo.rptComputers.ProcessorCount,
dbo.tblComputer.IsServer,
dbo.rptComputers.BiosSerialNumber,
dbo.rptComputers.PhysicalMemory,
dbo.rptComputers.ProcessorType,
dbo.rptComputers.ProcessorSpeed,
dbo.rptComputers.MostFrequentUser,
dbo.tblComputer.Domain,
dbo.rptComputers.ClientVersion,
dbo.rptComputers.ClientInstallDate,
dbo.rptComputers.ClientConfigurationName,
dbo.rptComputers.BiosVersion,
dbo.rptComputers.BiosDate,
dbo.rptComputers.Manufacturer,
dbo.rptComputers.Model,
dbo.rptComputers.IsPortable,
dbo.rptComputers.OperatingSystem,
dbo.tblComputer.OSServicePack,
dbo.rptComputers.ComputerStatusCode,
dbo.rptComputers.IPAddress AS 'MultipleIPAddress',
CASE WHEN PATINDEX ('%[, ]%',dbo.rptComputers.IPAddress) > 0 THEN
left(dbo.rptComputers.IPAddress,(PATINDEX('%[, ]%',dbo.rptComputers.IPAddress))-1)
ELSE dbo.rptComputers.IPAddress
END AS 'IPAddress',
dbo.rptComputers.LastScanDate
FROM dbo.rptComputers INNER JOIN dbo.tblComputer ON dbo.rptComputers.CID = dbo.tblComputer.CID
AND dbo.rptComputers.ComputerID = dbo.tblComputer.ComputerID
GROUP BY dbo.rptComputers.HostName,
dbo.tblComputer.IsVirtual,
dbo.rptComputers.Vendor,
dbo.rptComputers.ProcessorCount,
dbo.rptComputers.BiosSerialNumber,
dbo.tblComputer.IsServer,
dbo.rptComputers.PhysicalMemory,
dbo.rptComputers.ProcessorType,
dbo.rptComputers.ProcessorSpeed,
dbo.rptComputers.MostFrequentUser,
dbo.tblComputer.Domain,
dbo.rptComputers.ClientVersion,
dbo.rptComputers.ClientInstallDate,
dbo.rptComputers.ClientConfigurationName,
dbo.rptComputers.BiosVersion,
dbo.rptComputers.BiosDate,
dbo.rptComputers.Manufacturer,
dbo.rptComputers.Model,
dbo.rptComputers.IsPortable,
dbo.rptComputers.OperatingSystem,
dbo.tblComputer.OSServicePack,
dbo.rptComputers.ComputerStatusCode,
dbo.rptComputers.IPAddress,
dbo.rptComputers.LastScanDate
您需要过滤视图;即,带有 WHERE
子句的视图。像这样的事情(我在评论中标记了我添加到您的代码中的 begin/end):
CREATE VIEW [dbo].[vw_Cherwell_Machines] AS
SELECT dbo.rptComputers.HostName,
dbo.rptComputers.Vendor,
dbo.tblComputer.IsVirtual,
dbo.rptComputers.ProcessorCount,
dbo.tblComputer.IsServer,
dbo.rptComputers.BiosSerialNumber,
dbo.rptComputers.PhysicalMemory,
dbo.rptComputers.ProcessorType,
dbo.rptComputers.ProcessorSpeed,
dbo.rptComputers.MostFrequentUser,
dbo.tblComputer.Domain,
dbo.rptComputers.ClientVersion,
dbo.rptComputers.ClientInstallDate,
dbo.rptComputers.ClientConfigurationName,
dbo.rptComputers.BiosVersion,
dbo.rptComputers.BiosDate,
dbo.rptComputers.Manufacturer,
dbo.rptComputers.Model,
dbo.rptComputers.IsPortable,
dbo.rptComputers.OperatingSystem,
dbo.tblComputer.OSServicePack,
dbo.rptComputers.ComputerStatusCode,
dbo.rptComputers.IPAddress AS 'MultipleIPAddress',
CASE WHEN PATINDEX ('%[, ]%',dbo.rptComputers.IPAddress) > 0 THEN
left(dbo.rptComputers.IPAddress,(PATINDEX('%[, ]%',dbo.rptComputers.IPAddress))-1)
ELSE dbo.rptComputers.IPAddress
END AS 'IPAddress',
dbo.rptComputers.LastScanDate
FROM dbo.rptComputers INNER JOIN dbo.tblComputer ON dbo.rptComputers.CID = dbo.tblComputer.CID
AND dbo.rptComputers.ComputerID = dbo.tblComputer.ComputerID
/*begin change*/
WHERE tblComputer.IsServer = 0
/*end change*/
GROUP BY dbo.rptComputers.HostName,
dbo.tblComputer.IsVirtual,
dbo.rptComputers.Vendor,
dbo.rptComputers.ProcessorCount,
dbo.rptComputers.BiosSerialNumber,
dbo.tblComputer.IsServer,
dbo.rptComputers.PhysicalMemory,
dbo.rptComputers.ProcessorType,
dbo.rptComputers.ProcessorSpeed,
dbo.rptComputers.MostFrequentUser,
dbo.tblComputer.Domain,
dbo.rptComputers.ClientVersion,
dbo.rptComputers.ClientInstallDate,
dbo.rptComputers.ClientConfigurationName,
dbo.rptComputers.BiosVersion,
dbo.rptComputers.BiosDate,
dbo.rptComputers.Manufacturer,
dbo.rptComputers.Model,
dbo.rptComputers.IsPortable,
dbo.rptComputers.OperatingSystem,
dbo.tblComputer.OSServicePack,
dbo.rptComputers.ComputerStatusCode,
dbo.rptComputers.IPAddress,
dbo.rptComputers.LastScanDate
Documentation: https://docs.microsoft.com/en-us/sql/relational-databases/indexes/create-filtered-indexes?view=sql-server-ver15 Be sure to see the "limitations" section there, in case there's something in your environment which will not work here.