2015-09-25 43 views
0

我有2個查詢,如下所示的joing結果,2個查詢與上相同的表不同groupByclauses

這些查詢的
query1 = select department as 'Department_name', firstname as 'FirstName', lastname as 'LastName', manager as 'Manager', 
title as 'Title', firstofficer as 'First_Officer', email as 'Email', resourceName as 'Resource Name', 
entitlementName as 'Ent Name', concat(resourcename, entitlementname) AS 'Entitlements', count(distinct uid) 
as 'Total_Common' from outlier_report or1 
group by department, concat(resourcename, entitlementname) 


query2 = select department as 'Department_name', 
count(distinct uid) AS 'Total count of user', 
count(distinct resourcename) AS 'Total resource count', 
count(distinct concat(resourcename,entitlementname)) as 'Total Ent Count' 
from outlier_report or2 group by department; 

每個返回(基於組由子句115和125分別地)的結果的不同計數 我必須加入這兩個結果,以便我擁有所有的領域和4個計數(1個來自第一個,另外3個來自其他)。 如何使用SQL連接或使用Java程序來將單獨的查詢結果操作爲1輸出csv。

回答

0

每個查詢都是一個「視圖」,所以你可以加入它們。由於您在查詢兩個查詢中的同一個表時沒有進行過濾(where-clause),所以內部連接很好:

select a.department, a.AA, a.BB, b.CC, b.DD 
    from (select department, AA, BB 
      from outlier_report 
      group by department, concat(resourcename, entitlementname) 
     ) a 
    join (select department, CC, DD 
      from outlier_report 
      group by department 
     ) b on a.department = b.department 
相關問題