![](/img/trans.png)
[英]How to retrieve data from two tables related using a third table, SQL Server
[英]How to get data from two temporary tables and insert in third table without adding new row in SQL Server
我正在尝试制作需要从两个不同表中获取数据的报告。 我不能使用加入。 所以我试图插入一个临时表。 但它为每个插入返回不止一行。 我希望它在一行中。下面是 MS Sql 服务器中的查询。
@month int= 0,
@year int = 0, --(2020)
@regionid int = null, --(13/14/16/17/18/19/20)
@countryid int = null,
@clientid int = null
AS
BEGIN
IF OBJECT_ID('tempdb..#tempClientReport') IS NOT NULL
DROP TABLE #tempClientReport
Create Table #tempClientReport
(
Client_ID int,
Client_Name varchar(250),
Project_Received_Request int default 0,
Project_Delivered_Request int default 0,
BAU_Received_Request int default 0,
BAU_Delivered_Request int default 0
)
insert into #tempClientReport(Client_ID, Client_Name, Project_Received_Request, BAU_Received_Request,Project_Delivered_Request,BAU_Delivered_Request)
select
RELEASE_MANAGEMENT_TRAN.CLIENT_ID,
Client_Name,
Sum(case when REQUEST_ID = 2 then 1 else 0 end) as ProjectCount,
Sum(case when REQUEST_ID = 1 then 1 else 0 end) as BAUCount,null,null
from
FLOW_TRANSACTION INNER JOIN RELEASE_MANAGEMENT_TRAN on RELEASE_MANAGEMENT_TRAN.RM_ID = FLOW_TRANSACTION.RM_ID
inner join CLIENT_MASTER
on RELEASE_MANAGEMENT_TRAN.CLIENT_ID = CLIENT_MASTER.CLIENT_ID
where
(@month = 0 or (DATEPART(month,RELEASE_MANAGEMENT_TRAN.CREATION_DATE) = @month))
and (DATEPART(year,RELEASE_MANAGEMENT_TRAN.CREATION_DATE) = @year)
and (@regionid is null or RELEASE_MANAGEMENT_TRAN.REGION_ID = @regionid)
and (@countryid is null or RELEASE_MANAGEMENT_TRAN.GE_ID = @countryid)
and (@clientid is null or RELEASE_MANAGEMENT_TRAN.CLIENT_ID = @clientid)
and TO_STAGEID = '11' and STATUS != 'R'
--and CLIENT_MASTER.ACTIVE_STATE = 'A'
group by
RELEASE_MANAGEMENT_TRAN.CLIENT_ID,
Client_Name
union all
select
RELEASE_MANAGEMENT_TRAN.CLIENT_ID,
Client_Name,null,null,
Sum(case when REQUEST_ID = 2 then 1 else 0 end) as ProjectCount,
Sum(case when REQUEST_ID = 1 then 1 else 0 end) as BAUCount
from
PACKAGE_QA INNER JOIN RELEASE_MANAGEMENT_TRAN on RELEASE_MANAGEMENT_TRAN.RM_ID = PACKAGE_QA.RM_ID
inner join CLIENT_MASTER
on RELEASE_MANAGEMENT_TRAN.CLIENT_ID = CLIENT_MASTER.CLIENT_ID
where
(@month = 0 or (DATEPART(month,RELEASE_MANAGEMENT_TRAN.CREATION_DATE) = @month))
and (DATEPART(year,RELEASE_MANAGEMENT_TRAN.CREATION_DATE) = @year)
and (@regionid is null or RELEASE_MANAGEMENT_TRAN.REGION_ID = @regionid)
and (@countryid is null or RELEASE_MANAGEMENT_TRAN.GE_ID = @countryid)
and (@clientid is null or RELEASE_MANAGEMENT_TRAN.CLIENT_ID = @clientid)
and STATUS = 'C'
group by
RELEASE_MANAGEMENT_TRAN.CLIENT_ID,
Client_Name
order by Client_Name
END
select * from #tempClientReport order by Client_Name
IF OBJECT_ID('tempdb..#tempRegion') IS NOT NULL
DROP TABLE #tempRegion
它给出的结果如下
1 Media 0 NULL 2 NULL
1 Media NULL 0 NULL 2
3 Horizon 0 NULL 1 NULL
媒体等是我想要的客户名称,对于每个客户,它应该只在一行中提供数据,如您所见,它在这里提供了两行。请帮助
你能像这样使用 SUM 和 GROUP BY:
select
Client_ID, [Client_Name],
SUM([Project_Received_Request]) AS [Project_Received_Request],
SUM([Project_Delivered_Request]) AS [Project_Delivered_Request],
SUM([BAU_Received_Request]) AS [BAU_Received_Request],
SUM([BAU_Delivered_Request]) AS [BAU_Delivered_Request]
from #tempClientReport
group by Client_ID, [Client_Name]
order by [Client_Name]
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.