BEBaldauf
asked on
Join multiple pivot queries
I have a series of queries that use pivot, some using COUNT and some using SUM. They all run fine individually, but I want to join them all together so that I get the results of all of them as a single set of results. Is this possible?
declare @cols nvarchar(max)
select @cols = stuff(( select distinct '],[Yr' + FISCAL_YEAR
from ACADEMICCALENDAR as t2
where DATEPART(year,fiscal_year)>=datepart(year,getdate())-5
group by FISCAL_YEAR
having max(START_DATE)<GETDATE() and max(END_DATE)<GETDATE()
for xml path('')), 1, 2, '') + ']'
declare @query nvarchar(max)
set @query = '
select * from (select distinct ''Yr''+c.FISCAL_YEAR "Fiscal_Year",''Graduations - Highest Credential'' "RowType",a.PEOPLE_ID
from ACADEMIC a
inner join ACADEMICCALENDAR c on a.ACADEMIC_YEAR=c.ACADEMIC_YEAR and a.ACADEMIC_TERM=c.ACADEMIC_TERM and a.ACADEMIC_SESSION=c.ACADEMIC_SESSION
inner join VWUPROGRAMGROUPS v on v.Curriculum=a.CURRICULUM
where a.GRADUATED=''g''
and a.ACADEMIC_SESSION=''main''
and v.GroupID=''1''
and v.HighestCredential=a.CURRICULUM ) as t1
pivot (count(people_id) for fiscal_year in (' + @cols + ')) as p'
exec(@query)
set @query = '
select * from (select distinct ''Yr''+c.FISCAL_YEAR "Fiscal_Year",''Credentials Awarded'' "RowType",a.PEOPLE_ID
from ACADEMIC a
inner join ACADEMICCALENDAR c on a.ACADEMIC_YEAR=c.ACADEMIC_YEAR and a.ACADEMIC_TERM=c.ACADEMIC_TERM and a.ACADEMIC_SESSION=c.ACADEMIC_SESSION
inner join VWUPROGRAMGROUPS v on v.Curriculum=a.CURRICULUM
where
a.GRADUATED=''g''
and a.ACADEMIC_SESSION=''main''
and v.GroupID=''1''
) as s
pivot (count ( people_id) for fiscal_year in (' + @cols + ')) as p'
exec(@query)
set @query = '
select * from (select distinct ''Yr''+c.FISCAL_YEAR "Fiscal_Year",''FTEs'' "RowType",SUM(f.CREDIT)/30 "FTE"
from VWUPROGRAMGROUPS v
inner join academic a on a.CURRICULUM=v.Curriculum
inner join ACADEMICCALENDAR c on c.ACADEMIC_YEAR=a.ACADEMIC_YEAR and c.ACADEMIC_TERM=a.ACADEMIC_TERM and c.ACADEMIC_SESSION=a.ACADEMIC_SESSION
inner join TRANSCRIPTDETAIL t on a.PEOPLE_ID=t.PEOPLE_ID and a.ACADEMIC_YEAR=t.ACADEMIC_YEAR and a.ACADEMIC_TERM=t.ACADEMIC_TERM and a.ACADEMIC_SESSION=t.ACADEMIC_SESSION
inner join VWUCREDITVALUEFORFTE f on f.PEOPLE_ID=t.PEOPLE_ID and f.ACADEMIC_YEAR=t.ACADEMIC_YEAR and f.ACADEMIC_TERM=t.ACADEMIC_TERM and f.EVENT_ID=t.EVENT_ID and f.SECTION=t.SECTION
where
v.GroupID=''1''
and a.ACADEMIC_SESSION=''main''
and A.PRIMARY_FLAG=''Y''
GROUP BY C.FISCAL_YEAR
) as s
pivot (sum ( fte) for fiscal_year in (' + @cols + ')) as p'
exec(@query)
set @query = '
SELECT * FROM (
select distinct ''Yr''+AccountSummary.Year "Fiscal_Year",''Operational Expense'' "RowType",
isnull( AccountSummary.[Debit Amount],0 ) "Debits"
from prod..AccountSummary AccountSummary
where substring(AccountSummary.[Account Number],9,5) <>''00000''
and substring(AccountSummary.[Account Number],6,2) in(''10'',''21'')
group by AccountSummary.Year, accountsummary.segment3, AccountSummary.[Debit Amount]
having accountsummary.segment3 =''10100''
) AS S
pivot (sum(debits) for fiscal_year in (' + @cols + ')) as p'
exec(@query)
set @query = 'SELECT * FROM (select ''Yr''+ AccountSummary.Year "Fiscal_Year",''Grant Funded'' "RowType",
isnull(AccountSummary.[Debit Amount],0) "Debits"
from PROD..GL40200 a
left outer join PROD..AccountSummary AccountSummary on a.SGMNTID=substring(AccountSummary.[Account Number],9,5)
where a.SGMNTID=''21''
group by AccountSummary.Year, a.DSCRIPTN,accountsummary.segment3,AccountSummary.[Debit Amount]
having accountsummary.segment3 =''10100'' or AccountSummary.Segment3 is null
) AS S
pivot (sum(debits) for fiscal_year in (' + @cols + ')) as p'
exec(@query)
set @query = 'SELECT * FROM (select distinct ''Yr''+ AccountSummary.Year "Fiscal_Year",''Instructor Expense'' "RowType",
isnull(AccountSummary.[Debit Amount],0) "Debits"
from prod..AccountSummary AccountSummary
where substring(AccountSummary.[Account Number],9,5) <>''00000''
and substring(AccountSummary.[Account Number],6,2) in(''10'',''21'')
and AccountSummary.Segment1 >=''5073''
and AccountSummary.Segment1 <=''5119''
group by AccountSummary.Year, accountsummary.segment3, AccountSummary.[Debit Amount]
having accountsummary.segment3=''10100''
) AS S
pivot (sum(debits) for fiscal_year in (' + @cols + ')) as p'
exec(@query)
set @query = 'SELECT * FROM (select ''Yr''+ AccountSummary.Year "Fiscal_Year",''Capital Cost'' "RowType",
isnull(AccountSummary.[Debit Amount],0) "Debits"
from PROD..GL40200 a
left outer join PROD..AccountSummary AccountSummary on a.SGMNTID=substring(AccountSummary.[Account Number],9,5)
where a.SGMNTID=''30''
group by a.DSCRIPTN,AccountSummary.Year, accountsummary.segment3,AccountSummary.[Debit Amount]
having accountsummary.segment3 =''10100'' or AccountSummary.Segment3 is null
) AS S
pivot (sum(debits) for fiscal_year in (' + @cols + ')) as p'
exec(@query)
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER