Subject | Newbie question - combining results from two Select Statements |
---|---|
Author | Phil Horst |
Post date | 2002-10-25T17:15:25Z |
I am looking for help with SQL. I keep running into roadblocks and I
suspect it has to do with the fact that I haven't yet learned to think
Firebird. My prior experience with SQL was with Sybase (SQLAnywhere).
How can I solve the following problem - in a relatively simple way?
I need to produce a report that requires data from two different subsets
of data. The first subset uses an aggregate function (Sum) to provide
totals of hours spent on active cases. The second subset is without any
aggregate functions - it simply lists all closed cases for the fiscal
year. I would like to combine these two subsets into one cursor or
table.
So far I have considered the following approaches - without success.
1) I can't use UNION to combine the subsets because it seems to be
illegal to include aggregate functions in a UNION statement.
2) I then considered creating a work table. It seems that INSERT ...
Select ... won't work on the subset with the aggregate function because
it seems that the ORDER By clause is not acceptable in an Insert
statement.
Am I missing something in the above attempts? Or can someone tell me
what might work? Below are the select statements that I am using to
prepare the different subsets.
Thanks for any help offered
Phil Horst
Tabor Community Services
1) The subset of active records:
select PR.ProgramDescription
, PC.FirstName
, CL.ProgramID
, C.PrimaryClientName
, CL.CaseID
, sum(CL.nettime)
from caselog CL
left outer join HBprogram PR on CL.ProgramID = PR.ProgramID
left outer join CaseManager CM on CL.CaseManagerID =
CM.CaseManagerID
left outer join Person PC on CM.PersonID = PC.PersonID
left outer join Case C on CL.CaseID = C.CaseID
where
logdate between '09-01-2002' and '09-30-2002'
and nettime > 0
group by
PR.ProgramDescription
,CL.ProgramID
,PC.FirstName
,C.PrimaryClientName
,CL.CaseID
order by
PR.ProgramDescription
,CL.ProgramID
,PC.FirstName
,C.PrimaryClientName
,CL.CaseID
2) The subset of inactive records:
SELECT Prog.ProgramDescription
, PC.FirstName as Counselor
, CP.ProgramID
, C.PrimaryClientName
, C.CaseID
, 0 as NetTime
FROM
caseProgram CP
left outer join HBProgram Prog on Prog.ProgramID = CP.ProgramID
left outer join CaseManager CM on CP.CaseManagerID =
CM.CaseManagerID
left outer join Person PC on CM.PersonID = PC.PersonID
left outer join Case C on CP.CaseID = C.CaseID
where
CP.DateClosed >= '07-01-2002' and
CP.DateClosed < '09-01-2002'
order by Prog.ProgramDescription
, PC.FirstName
, C.PrimaryClientname
suspect it has to do with the fact that I haven't yet learned to think
Firebird. My prior experience with SQL was with Sybase (SQLAnywhere).
How can I solve the following problem - in a relatively simple way?
I need to produce a report that requires data from two different subsets
of data. The first subset uses an aggregate function (Sum) to provide
totals of hours spent on active cases. The second subset is without any
aggregate functions - it simply lists all closed cases for the fiscal
year. I would like to combine these two subsets into one cursor or
table.
So far I have considered the following approaches - without success.
1) I can't use UNION to combine the subsets because it seems to be
illegal to include aggregate functions in a UNION statement.
2) I then considered creating a work table. It seems that INSERT ...
Select ... won't work on the subset with the aggregate function because
it seems that the ORDER By clause is not acceptable in an Insert
statement.
Am I missing something in the above attempts? Or can someone tell me
what might work? Below are the select statements that I am using to
prepare the different subsets.
Thanks for any help offered
Phil Horst
Tabor Community Services
1) The subset of active records:
select PR.ProgramDescription
, PC.FirstName
, CL.ProgramID
, C.PrimaryClientName
, CL.CaseID
, sum(CL.nettime)
from caselog CL
left outer join HBprogram PR on CL.ProgramID = PR.ProgramID
left outer join CaseManager CM on CL.CaseManagerID =
CM.CaseManagerID
left outer join Person PC on CM.PersonID = PC.PersonID
left outer join Case C on CL.CaseID = C.CaseID
where
logdate between '09-01-2002' and '09-30-2002'
and nettime > 0
group by
PR.ProgramDescription
,CL.ProgramID
,PC.FirstName
,C.PrimaryClientName
,CL.CaseID
order by
PR.ProgramDescription
,CL.ProgramID
,PC.FirstName
,C.PrimaryClientName
,CL.CaseID
2) The subset of inactive records:
SELECT Prog.ProgramDescription
, PC.FirstName as Counselor
, CP.ProgramID
, C.PrimaryClientName
, C.CaseID
, 0 as NetTime
FROM
caseProgram CP
left outer join HBProgram Prog on Prog.ProgramID = CP.ProgramID
left outer join CaseManager CM on CP.CaseManagerID =
CM.CaseManagerID
left outer join Person PC on CM.PersonID = PC.PersonID
left outer join Case C on CP.CaseID = C.CaseID
where
CP.DateClosed >= '07-01-2002' and
CP.DateClosed < '09-01-2002'
order by Prog.ProgramDescription
, PC.FirstName
, C.PrimaryClientname