我有一个TSQL查询,它给出了给定结果集的趋势数据的最大值。我想根据每个月的结果得到这些数据。 (我在结果集中有一个datetime列)。如果我选择三个月的数据,它必须为每个月提供这个数字。现在,它会查找最大值并在所有月份给出相同的结果。
下面是我用来获得趋势结果的表达式。它是select语句的一部分,包含其他列的数量。
select col1, col2, sampledatecollected, (Select MAX(AvailMemSlope) FROM SlopeCTE) * MetricID + (Select MAX (AvailMemIntercept) FROM InterceptCTE) AS AvailMemTrend
我想我可能需要做这样的事情,但考虑到表达方式,我对如何获得理想结果感到困惑
select name, max(value)
from tbl1
group by name
CPUTrend id我从第一个查询中指定的表达式获得的数据。
示例数据:
Date AVGCPU MAXCPU CPUTrend
8/22/2016 20 40 44
8/23/2016 20 40 44
8/24/2016 20 40 44
8/25/2016 20 40 44
9/22/2016 20 50 44
9/23/2016 20 50 44
9/24/2016 20 50 44
预期结果:
Date AVGCPU MAXCPU CPUTrend
8/22/2016 20 40 32
8/23/2016 20 40 32
8/24/2016 20 40 32
8/25/2016 20 40 32
9/22/2016 20 50 44
9/23/2016 20 50 44
9/24/2016 20 50 44
现在我得到的只有44,因为它是最大值。
答案 0 :(得分:0)
我想你只想要一个子查询:
with t as (
select col1, col2, sampledatecollected,
(Select MAX(AvailMemSlope) FROM SlopeCTE) * MetricID +
(Select MAX (AvailMemIntercept) FROM InterceptCTE) AS AvailMemTrend
from ?? -- the SQL in your question is incomplete
)
select year(sampledatecollected), month(sampledatecollected),
max(AvailMemTrend)
from t
group by year(sampledatecollected), month(sampledatecollected)
order by year(sampledatecollected), month(sampledatecollected);