2016-09-22 165 views
-1

connection_table極品嵌套select查詢

 
    app | src_port | dst_port | src_ip | dst_ip | time | L1 | L2 
----------+-----------+----------+----------+---------+-------+-----+---- 
    HTTP | 100  | 200  | x  | y  | t1 | 1 | 0  
    HTTPS | 101  | 300  | x  | y  | t1 | 1 | 0 
    HTTP | 102  | 200  | a  | b  | t2 | 0 | 1 
    HTTP | 100  | 200  | x  | y  | t2 | 1 | 0 
    HTTP | 100  | 200  | x  | y  | t3 | 1 | 0 
    HTTP | 111  | 200  | x  | y  | t4 | 1 | 0 

結果

 
    app | sum(L1) | sum(L2) 
----------+----------+-------- 
    HTTP | 2  | 1 
    HTTPS | 0  | 1 

查詢

select app_table.app, 
     SUM(app_table.L1), 
     SUM(app_table.L2) 
from (
    select app, L1, L2 
    from connection_table 
    group by app, src_port, dst_port, src_ip, dst_ip 
) as app_table 
group by app_table.app; 

釷簡單Postgres的查詢上述查詢用於獲取結果。我需要一個沒有嵌套選擇的查詢?
在這裏,src_port,dst_port,src_ip和dst_ip唯一標識一個應用程序。需要統計每個應用的總L1和L2。

+0

你真的使用所有的DBMS在這裏? (不要標記不涉及的產品。) – jarlh

+0

使用派生表有什麼問題? –

+0

簡單查詢只適用於我的應用程序。如果這是獲得結果的唯一方法,那就沒問題。 – Kranthi

回答

0

使用集團通過

注:您的輸出似乎是錯誤的。

SELECT 
    app, 
    SUM(L1) AS L1, 
    SUM(L2) AS L2 
FROM connection_table T 
GROUP BY T.app 

輸出:

enter image description here

+0

這將純粹按應用分組。不是真正的解決方案。 – Kranthi

+0

它看起來像你的最終輸出,是你的輸出,然後我的答案? –

+0

您的輸出是正確的,但對於給定的要求不起作用。你可以使用不同的「src_port」檢查多個條目並且具有相同的應用程序嗎? – Kranthi