2016-08-22 58 views
-2

我有一個TSQL查詢,它給出了給定結果集的趨勢數據的最大值。我想根據每個月的結果得到這些數據。 (我在結果集中有一個日期時間列)。如果我選擇三個月的數據,它必須每個月都給這個數字。現在它會查找所有月份的最大值並給出相同的結果。獲取每個月的組的最大值TSQL

下面是我用來獲得趨勢結果的表達式。它是包含其他列數的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

請告訴我在問題中新增了一些更爲詳細的一些示例數據和預期的結果 – TheGameiswar

+0

@TheGameiswar。請驗證 – Ramya

+0

請發佈當前樣本數據 – TheGameiswar

回答

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);