Click here to Skip to main content
15,890,845 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Hi experts,

I have two result set like this

district     Raised        approved
xyz             3            2
abc        4           2
pqr        3            2

To get this output am using a query like

Select
IMS.IPS_District_Name as [District Name],
SUM(iMs.IPS_OB) as Raised,
Sum(ImS.IpS_TotRaised) as Approved from
Intermediate_Overdue_Statistics IOS
inner join Intermediate_MonthWise_Statistics IMS
on ios.IOS_District_Code=ims.IpS_District_Code AND
IOS.IOS_Taluk_Code =ImS.IpS_Taluk_Code
where IPS_Month =3 and ims.IPS_Year =2014
group by ImS.IpS_District_Name
order by IMS.IPS_District_Name

another result set is

district       pending
xyz              1
abc             2
pqr             1

to get this result i have used query like this

SQL
Select IOS_District_Name,SUM(IOS_TotOverdue )+SUM (IOS_TotWithinTime) as [Pending]
from Intermediate_Overdue_Statistics
where datepart(mm,IOS_DOT)=3 and datepart(yyyy,IOS_DOT)=2014
GROUP by IOS_District_Name
order by IOS_District_Name



now finally i want out put like this


district    Raised   Approved  Pending
xyz           3              2          1
abc                4                2            2
pqr          3              2          1


Can any one please help me to solve this issue??
(Thanks in advance)
Posted

use this query
SQL
select t1.District Name,t1.Raised,t1.Approved ,t2.Pending from
 (Select
IMS.IPS_District_Name as [District Name],
SUM(iMs.IPS_OB) as Raised,
Sum(ImS.IpS_TotRaised) as Approved from
Intermediate_Overdue_Statistics IOS
inner join Intermediate_MonthWise_Statistics IMS
on ios.IOS_District_Code=ims.IpS_District_Code AND
IOS.IOS_Taluk_Code =ImS.IpS_Taluk_Code
where IPS_Month =3 and ims.IPS_Year =2014
group by ImS.IpS_District_Name
order by IMS.IPS_District_Name) t1 inner join (Select IOS_District_Name,SUM(IOS_TotOverdue )+SUM (IOS_TotWithinTime) as [Pending]
from Intermediate_Overdue_Statistics
where datepart(mm,IOS_DOT)=3 and datepart(yyyy,IOS_DOT)=2014
GROUP by IOS_District_Name
order by IOS_District_Name) t2 on t1.District_Name=t2.IOS_District_Name 



Query Format:

SQL
select t1.colmn,t2.column  from(select *From table1)t1 inner join (select *From table2) t2 on t1.id=t2.id
 
Share this answer
 
Comments
Keerthi Kumar(Andar) 29-Mar-14 7:16am    
thanks a lot sir, but order by statement will not work here
King Fisher 29-Mar-14 7:28am    
welcome :) .it will work
Keerthi Kumar(Andar) 29-Mar-14 7:29am    
no it is giving error like
The ORDER BY clause is invalid in views, inline functions, derived tables, subqueries, and common table expressions, unless TOP or FOR XML is also specified.
King Fisher 29-Mar-14 7:50am    
you want order by distinct_name?
Maciej Los 29-Mar-14 11:01am    
Why to join Intermediate_Overdue_Statistics twice?
Please, see my answer.
Try something like this:
SQL
SELECT [District Name], SUM(Raised) AS Raised, SUM(Approved) AS Approved, SUM(Pending) AS Pending
FROM ( 
    SELECT IMS.IPS_District_Name as [District Name], IMS.IPS_OB AS Raised, IMS.IpS_TotRaised AS Approved, IOS.IOS_TotOverdue + IOS.IOS_TotWithinTime AS Pending
    FROM Intermediate_Overdue_Statistics AS IOS INNER JOIN Intermediate_MonthWise_Statistics AS IMS ON IOS.IOS_District_Code=IMS.IpS_District_Code AND
        IOS.IOS_Taluk_Code =IMS.IpS_Taluk_Code
    WHERE IMS.IPS_Month =3 AND IMS.IPS_Year =2014 AND datepart(mm,IOS.IOS_DOT)=3 and datepart(yyyy,IOS.IOS_DOT)=2014
    ) AS t1
GROUP BY t1.[District Name]
ORDER BY t1.[District Name]
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900