I am trying to make a report where i need to get data from Two different tables. i cant use Join. So i am trying to insert in a temporary table. But it is returning more than one row for each insert. I want it in a single row.Below is the query in MS Sql Server.
@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
it gives result as below
1 Media 0 NULL 2 NULL
1 Media NULL 0 NULL 2
3 Horizon 0 NULL 1 NULL
Media etc is the client name I want that for each client it should give only in one row the data as you can see it gives two rows here.Please help
Could you use SUM and GROUP BY like this:
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]
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.