2014-04-18 34 views
0

我有以下查詢哪些查詢不同的表,並使用UNION操作者顯示的一組數據:如何插入到從多個表中的一個表,同時使用UNION

-----TOTAL COUNT OF ACTIVE DEA----- DEA 
select attr1671 as 'TYPE', count(attr1668) as 'TOTAL' 

from [MyServer].[DBOTYPE].instance.rmobjectinsta d inner join (select fk1665, max(objectid) 'newobjectid' 
from [MyServer].[DBOTYPE].instance.rmobjectinsta 
group by fk1665) n on d.objectid = n.newobjectid where attr1671 = 'DEA' and cast(attr1668 as date) > cast(getdate()+30 as date) and activestatus = 0 
group by attr1671 


-----TOTAL COUNT OF ACTIVE LICENSES----- LICENSE 
UNION 
select 'LICENSE' as 'TYPE', count(*) as 'TOTAL' 

from [MyServer].[DBOTYPE].instance.rmobjectin t inner join (select fk1656, max(objectid) 'newobjectid' 
from [MyServer].[DBOTYPE].instance.rmobjectin 
group by fk1656) c on t.objectid = c.newobjectid where cast(attr1660 as date) > cast(getdate()+30 as date) and activestatus = 0 


-----TOTAL INFECTION CERTIFICATIONS ACTIVE----- INFECTION 
UNION 
select 'INFECTION' as 'TYPE', count(*) 'TOTAL' 

from [MyServer].[DBOTYPE].instance.rmobject z inner join (select fk1676, max(objectid) 'newobjectid' 
from [MyServer].[DBOTYPE].instance.rmobject 
group by fk1676) h on z.objectid = h.newobjectid where cast(attr1680 as date) > cast(getdate()+30 as date) and activestatus = 0 



-----TOTAL COUNT OF ACTIVE CDS----- CDS 
UNION 
select attr1671 as 'TYPE', count(attr1668) as 'TOTAL' 

from [MyServer].[DBOTYPE].instance.rmobje k inner join (select fk1665, max(objectid) 'newobjectid' 
from [MyServer].[DBOTYPE].instance.rmobje 
group by fk1665) l on k.objectid = l.newobjectid where attr1671 = 'CDS' and cast(attr1668 as date) > cast(getdate()+30 as date) and activestatus = 0 
group by attr1671 

哪個顯示以下內容:

TYPE   TOTAL 
CDS   45 
DEA   56 
INFECTION  67 
LICENSE  41 

我想插入數據到一個表格中,我可以在我的SSRS報表中導入一個DataSet。我怎樣才能實現它?

我試着做以下幾點:

-----TOTAL COUNT OF ACTIVE DEA----- DEA 
select attr1671 as 'TYPE', count(attr1668) as 'TOTAL' 
INTO [MYDB].[DBO].[myT] --on first run and then comment 
from [MyServer].[DBOTYPE].instance.rmobjectinsta d inner join (select fk1665, max(objectid) 'newobjectid' 
from [MyServer].[DBOTYPE].instance.rmobjectinsta 
group by fk1665) n on d.objectid = n.newobjectid where attr1671 = 'DEA' and cast(attr1668 as date) > cast(getdate()+30 as date) and activestatus = 0 
group by attr1671 


-----TOTAL COUNT OF ACTIVE LICENSES----- LICENSE 
UNION 
select 'LICENSE' as 'TYPE', count(*) as 'TOTAL' 
INTO [MYDB].[DBO].[myT] --on first run and then comment 
from [MyServer].[DBOTYPE].instance.rmobjectin t inner join (select fk1656, max(objectid) 'newobjectid' 
from [MyServer].[DBOTYPE].instance.rmobjectin 
group by fk1656) c on t.objectid = c.newobjectid where cast(attr1660 as date) > cast(getdate()+30 as date) and activestatus = 0 


-----TOTAL INFECTION CERTIFICATIONS ACTIVE----- INFECTION 
UNION 
select 'INFECTION' as 'TYPE', count(*) 'TOTAL' 
INTO [MYDB].[DBO].[myT] --on first run and then comment 
from [MyServer].[DBOTYPE].instance.rmobject z inner join (select fk1676, max(objectid) 'newobjectid' 
from [MyServer].[DBOTYPE].instance.rmobject 
group by fk1676) h on z.objectid = h.newobjectid where cast(attr1680 as date) > cast(getdate()+30 as date) and activestatus = 0 



-----TOTAL COUNT OF ACTIVE CDS----- CDS 
UNION 
select attr1671 as 'TYPE', count(attr1668) as 'TOTAL' 
INTO [MYDB].[DBO].[myT] --on first run and then comment 
from [MyServer].[DBOTYPE].instance.rmobje k inner join (select fk1665, max(objectid) 'newobjectid' 
from [MyServer].[DBOTYPE].instance.rmobje 
group by fk1665) l on k.objectid = l.newobjectid where attr1671 = 'CDS' and cast(attr1668 as date) > cast(getdate()+30 as date) and activestatus = 0 
group by attr1671 

但沒有奏效。請幫助...

這是錯誤我得到:

Msg 196, Level 15, State 1, Line 2 SELECT INTO must be the first query in a statement containing a UNION, INTERSECT or EXCEPT operator.

+0

你是什麼意思'這不行'?什麼是錯誤/問題? –

+0

我得到的錯誤是'Msg 196,Level 15,State 1,Line 2 SELECT INTO必須是包含UNION,INTERSECT或EXCEPT運算符的語句中的第一個查詢。' – Si8

回答

1

您可以嘗試包裹整個事情作爲一個子查詢:

select * 
into <whatever> 
from (<your query here> 
    ) t; 
+0

此工作的方式...只是好奇知道't'是什麼。 – Si8

+0

@ SiKni8。 。 。 't'是一個表別名,它是SQL Server中'from'子句中的子查詢所必需的。 –

+0

謝謝。在接受您的答案之前,我必須等待6分鐘:/ – Si8

1

試試這個:

With Emp_CTE (Employee,Count) 
AS 
(

    Select Firstname as Employee , Count(*) as Count from Employee 
    where FirstName like 'V%' 
    group by FirstName 

    union 

    Select Firstname as Employee , Count(*) as Count from Employee 
    where FirstName like 'M%' 
    group by FirstName 

) 
Select * into dbo.EmployeeCount from Emp_CTE; 
相關問題