I have two reports I would prefer to combine into one report if possible. It is my understanding I cannot run 2 independent SQL queries within the same report.
The full SQL queries currently in the 2 separate reports are as follows:
SQL 1:
DECLARE @reportYear int = (year(getdate()) - iif(month(getdate()) = 1, 1, 0));
DECLARE @startLoadNumber int = @reportYear * 1000
, @endLoadNumber int = @reportYear * 1000 + 999;
SELECT l.BROKER_AGENCY AS 'BROKER AGENCY'
, l.AGENT_NAME AS 'AGENT NAME'
, l.PETS_LOAD_NUMBER AS 'LOAD NUMBER'
FROM Load_Info_Table as l
WHERE l.PETS_LOAD_NUMBER BETWEEN @startLoadNumber
AND @endLoadNumber
ORDER BY l.PETS_LOAD_NUMBER
SQL 2:
DECLARE @reportYear int = (year(getdate()) - iif(month(getdate()) = 1, 1, 0));
DECLARE @startLoadNumber int = @reportYear * 1000
, @endLoadNumber int = @reportYear * 1000 + 999;
SELECT l.BROKER_AGENCY, COUNT(l.BROKER_AGENCY) as 'TOTAL'
FROM LOAD_INFO_TABLE AS l
WHERE l.PETS_LOAD_NUMBER BETWEEN @startLoadNumber
AND @endLoadNumber
GROUP BY L.BROKER_AGENCY
SQL 2 would run after SQL 1 in the bottom of the page Report would bring results. Is there a way to combine this into one SQL statement? Hope this is clear.
In a nutshell: I want to run the main report, then count all broker names and give a total # of loads run by broker at the bottom.
I think this will do it, although of course I don't have any data to test it with.
SELECT l.BROKER_AGENCY AS 'BROKER AGENCY'
, COUNT(*) OVER(PARTITION BY BROKER_AGENCY) AS 'TOTAL'
, l.AGENT_NAME AS 'AGENT NAME'
, l.PETS_LOAD_NUMBER AS 'LOAD NUMBER'
FROM Load_Info_Table as l
WHERE l.PETS_LOAD_NUMBER BETWEEN @startLoadNumber
AND @endLoadNumber
ORDER BY l.PETS_LOAD_NUMBER
Why wouldn't you use Crystal Reports to create the summary data? Since you want this in the page footer - or following the data from the first query - you will need a separate section in the report that is sourced from the main query.
In SSRS (haven't worked with Crystal in 10 years) - this would be accomplished using a separate tablix. That tablix would use the same source as the first query and only show totals.
SELECT l.BROKER_AGENCY AS 'BROKER AGENCY'
, (SELECT COUNT(*) FROM Load_Info_Table as I2 WHERE I2.BROKER_AGENCY = I.BROKER_AGENCY AND
l.PETS_LOAD_NUMBER BETWEEN @startLoadNumber AND @endLoadNumber
) AS 'TOTAL'
, l.AGENT_NAME AS 'AGENT NAME'
, l.PETS_LOAD_NUMBER AS 'LOAD NUMBER'
FROM Load_Info_Table as l
WHERE l.PETS_LOAD_NUMBER BETWEEN @startLoadNumber
AND @endLoadNumber
ORDER BY l.PETS_LOAD_NUMBER