2017-01-23 50 views
0

我存儲從一個文本文件中的文本文件數據的一個區間(讓說每小時) 數據我得到如下獲取MAX,MIN,SUM,平均值和單個表創建報告

日期(日期時間), TotalTime(varchar(20)), TotalCount(BigInt), Minimum Time(varchar(20)), Maximum Time(varchar(20))。

Input Data

我正在尋找像

Output and Internal Calculation

我的SQL至今

select 
    sum(
    (CASE WHEN (isnumeric(TotalTime) = 1) 
    THEN CAST(TotalTime AS bigint) 
    ELSE 0 
    END) 
) as 'TotalTime', 
    sum(TotalCount) as 'TotalCount', 
    AVG(
    (CASE WHEN (isnumeric(MinTime) = 1) 
    THEN CAST(MinTime AS bigint) 
    ELSE 0 
    END) 
) as 'MinAvgTime', 
    AVG(
    (CASE WHEN (isnumeric(MaxTime) = 1) 
    THEN CAST(MaxTime AS bigint) 
    ELSE 0 
    END) 
) as 'MaxAvgTime', 
    dateadd(DAY,0, datediff(day,0, InputDate)) as created 
from MYTABLE 
group by dateadd(DAY,0, datediff(day,0, InputDate)) 

試圖找出這樣的報道有什麼辦法我可以得到輸出沒有使用LINQ ..是否有任何方法直接從SQL查詢獲得它

回答

0

你從下面的SQL得到什麼結果:

SELECT 
    date(`day`) AS created, 
    sum(TotalTime) AS TotalTime, 
    sum(TotalCount) AS TotalCount, 
    avg(MinTime) AS MinAvgTime, 
    avg(MaxTime) AS MaxAvgTime 
FROM MYTABLE 
GROUP BY 1; 
+0

謝謝 。我得到5列,我期待從sql查詢中獲得10列。有沒有什麼辦法直接從SQL查詢 –

0

我認爲這會爲你工作:

SELECT 
    CONVERT(DATE, InputDate, 103) as InputDate, 
    SUM(cast(TotalTime as int)) as TotalTime, 
    SUM(TotalCount) as TotalCount, 
    AVG(cast(MinTime as float)) as MinAvgTime, 
    AVG(cast(MaxTime as float)) as MaxAvgTime 
FROM 
    MYTABLE 
GROUP BY 
    CONVERT(DATE, InputDate, 103) 
+0

..感謝你..如果你看到輸出圖像,我試圖選擇10列。我正在使用您提供給我的相同查詢,並且正在考慮在結果集上使用LINQ。有沒有什麼辦法直接從SQL查詢中獲取它 –

0

使用視圖或CTE功能,可以從SQL進行查詢直接