SQL查询以根据特定条件选择值

时间:2018-01-23 11:42:58

标签: mysql

我的数据库中有以下数据:

Id      MachineName CategoryName    CounterName  InstanceName   RawValue
11180   SERVER64    Process         ID Process   w3wp#2         2068
11180   SERVER64    Process         Working Set  w3wp#2         9310208

现在我希望实现这个目标,如果我发现价值' 2068'对于" ID流程"然后,我想要检索工作集RawValue。所以基于ID Process I的值现在 [InstanceName] = w3wp#2因此我想要检索的值= 9310208

现在我尝试了不同的SQL查询:

SELECT *
  FROM [dbo].[LoadTest]
  WHERE [LoadTestRunId] = '11180' and [CategoryName] = 'Process' and [InstanceName] like 'w3wp%'

但我需要一个过滤器。谁能引导我走向正确的方向?

2 个答案:

答案 0 :(得分:1)

这将对您有所帮助。我使用变量因为你需要找到一个特定的ID

SQL代码

<nav class="navbar navbar-expand-xl bg-light navbar-light">
    <div class="collapse navbar-collapse">
        <ul class="nav navbar-nav">
            <li class="nav-item" routerlinkactive="active current">
                <a routerlink="/messages" class="nav-link"><b>Messages</b></a>
            </li>
            <li class="nav-item" routerlinkactive="active current">
                <a routerlink="/transformations" class="nav-link"><b>Transformations</b></a>
            </li>
            <li class="nav-item" routerlinkactive="active current">
                <a routerlink="/actions" class="nav-link"><b>Actions</b></a>
            </li>
            <li class="nav-item" routerlinkactive="active current">
                <a routerlink="/rules" class="nav-link"><b>Rules</b></a>
            </li>
        </ul>
    </div>
</nav>

没有基于ID和InstanceName的变量的SQL代码

declare @myt table (id int,MachineName nvarchar(50),CategoryName nvarchar(50),CounterName nvarchar(50),InstanceName nvarchar(50),RawValue int)

insert into @myt
values

(11180 ,'SERVER64','Process','ID Process','w3wp#2',2068),
(11180 ,'SERVER64','Process','Working Set','w3wp#2',9310208)

declare @FindID int

Set @FindID = 2068;

with IdProcess as (
Select * from  @myt 
where RawValue = @FindID and CounterName = 'ID Process'
)
Select a.ID,a.MachineName,a.CategoryName,b.CounterName,a.InstanceName,b.RawValue from IdProcess a
inner join @myt b on a.InstanceName = b.InstanceName and b.CounterName='Working Set'

带有CategoryName过滤器的SQL代码

with IdProcess as (
Select * from  @myt 
where  CounterName = 'ID Process'
)
Select a.ID,a.MachineName,a.CategoryName,b.CounterName,a.InstanceName,b.RawValue from IdProcess a
inner join @myt b on a.id = b.id and a.InstanceName = b.InstanceName and b.CounterName='Working Set'

<强>结果

enter image description here

答案 1 :(得分:0)

这将执行。

Select * from (SELECT ROW_NUMBER() OVER(PARTITION BY LoadTestRunId ORDER BY LoadTestRunId DESC) as row,*
FROM [dbo].[LoadTest]) t1 where row=1

使用where子句

Select * from (SELECT ROW_NUMBER() OVER(PARTITION BY LoadTestRunId ORDER BY LoadTestRunId DESC),*
FROM [dbo].[LoadTest]) t1 where t1=1
and [LoadTestRunId] = '11180' and [CategoryName] = 'Process' and [InstanceName] like 'w3wp%'