Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 SQL Server 2008 Forums
 Transact-SQL (2008)
 Help Divinding results of two sql queries

Author  Topic 

kcarbone1970
Yak Posting Veteran

52 Posts

Posted - 2015-03-26 : 13:40:06
I need help dividing the results of two independent queries, I am stuck. The resulting table should be two columns (Expuslions & Suspensions) displaying the division result i.e.,(Rate). E.g., expulsions/Total
first query

SELECT
SUM(CASE WHEN Discipline = 'E' THEN TOTAL END) AS Expulsions ,
SUM(CASE WHEN Discipline IN ('I', 'O') THEN TOTAL END) AS Suspensions
FROM SUSPEXPD
WHERE CDS = @CDS


Second Query

SELECT
SUM(ENR_TOTAL) AS Total
FROM [StateData].[dbo].[enr13]
WHERE CDS_Code LIKE Cast(@CDS as varchar(7)) + '%


Thanks

Cartesian Yak

gbritton
Master Smack Fu Yak Hacker

2780 Posts

Posted - 2015-03-26 : 13:57:29
something like this might do it:


SELECT Expulsions/ENR_Total as Expulsions
, Suspensions/ENR_TOTAL as Suspensions

FROM
(
SELECT
SUM(CASE WHEN Discipline = 'E' THEN TOTAL END) AS Expulsions ,
SUM(CASE WHEN Discipline IN ('I', 'O') THEN TOTAL END) AS Suspensions
FROM SUSPEXPD
WHERE CDS = @CDS
) _1

CROSS JOIN
(
SELECT
SUM(ENR_TOTAL) AS Total
FROM [StateData].[dbo].[enr13]
WHERE CDS_Code LIKE Cast(@CDS as varchar(7)) + '%'
) _2
Go to Top of Page

kcarbone1970
Yak Posting Veteran

52 Posts

Posted - 2015-03-26 : 14:30:34
Thank You!!

Cartesian Yak
Go to Top of Page
   

- Advertisement -