vonfranzken
asked on
Divide by additional field from another database
Need to divide the last set byt PerPupilSpending
SELECT
(( SELECT SUM(S2C_TTL) FROM njsurvey.dbo.AES_ES_2)
+ ( SELECT SUM(S2C_TTL) FROM njsurvey.dbo.AES_HS_2))
/ (Select SUM(ENROLL_TTL) from njsurvey.dbo.AES_CONTACT_S CHOOL)AS PerPupilSpending
/(select SUM(CL_TOT) from reportcards.dbo.finance WHERE YEAR = '0405') AS TotalCost
SELECT
(( SELECT SUM(S2C_TTL) FROM njsurvey.dbo.AES_ES_2)
+ ( SELECT SUM(S2C_TTL) FROM njsurvey.dbo.AES_HS_2))
/ (Select SUM(ENROLL_TTL) from njsurvey.dbo.AES_CONTACT_S
/(select SUM(CL_TOT) from reportcards.dbo.finance WHERE YEAR = '0405') AS TotalCost
ASKER
Can I run this in query manager?
Isnt there a way to do it using the beggining point we had worked on together from the query above?
Would rather have it that way as I am finding it to be more confusing now.
Also getting;
Server: Msg 170, Level 15, State 1, Line 17
Line 17: Incorrect syntax near '@x'.
Isnt there a way to do it using the beggining point we had worked on together from the query above?
Would rather have it that way as I am finding it to be more confusing now.
Also getting;
Server: Msg 170, Level 15, State 1, Line 17
Line 17: Incorrect syntax near '@x'.
I see I was missing a comma in the last select statement:
declare @x decimal (20,4)
declare @p decimal (20,4)
declare @t decimal (20,4)
SET NOCOUNT ON
SELECT @x =
(
( SELECT SUM(S2C_TTL) FROM njsurvey.dbo.AES_ES_2)
+ ( SELECT SUM(S2C_TTL) FROM njsurvey.dbo.AES_HS_2)
)
select @p = SUM(ENROLL_TTL) from njsurvey.dbo.AES_CONTACT_S CHOOL
select @t = SUM(CL_TOT) from reportcards.dbo.finance WHERE YEAR = '0405'
select @x / @p AS PerPupilSpending
, @x / @t AS TotalCost
>Can I run this in query manager?
Yes
>Isnt there a way to do it using the beggining point we had worked on together from the query above?
you could, but the way I show you is 1) easier to maintain 2) easier to read (IMHO), 3) better scalable
declare @x decimal (20,4)
declare @p decimal (20,4)
declare @t decimal (20,4)
SET NOCOUNT ON
SELECT @x =
(
( SELECT SUM(S2C_TTL) FROM njsurvey.dbo.AES_ES_2)
+ ( SELECT SUM(S2C_TTL) FROM njsurvey.dbo.AES_HS_2)
)
select @p = SUM(ENROLL_TTL) from njsurvey.dbo.AES_CONTACT_S
select @t = SUM(CL_TOT) from reportcards.dbo.finance WHERE YEAR = '0405'
select @x / @p AS PerPupilSpending
, @x / @t AS TotalCost
>Can I run this in query manager?
Yes
>Isnt there a way to do it using the beggining point we had worked on together from the query above?
you could, but the way I show you is 1) easier to maintain 2) easier to read (IMHO), 3) better scalable
ASKER
Very cool I see what your saying now if I wanted to lets say multiply by the result of another select
I merely declare the variable define its type select @m = lets say and do the math at the end,
in this example what if I wanted to multiply the total cost by my @m, what would it look like
@x/@t*@m=NewResult ???
Lastly how could I round off the results?
The show as
PerPupilSpending = 1.753880849004775018
Total Cost = .244213803587716631
I merely declare the variable define its type select @m = lets say and do the math at the end,
in this example what if I wanted to multiply the total cost by my @m, what would it look like
@x/@t*@m=NewResult ???
Lastly how could I round off the results?
The show as
PerPupilSpending = 1.753880849004775018
Total Cost = .244213803587716631
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Cool do you need the paranthesis in:
select (@x/@t)*@m As NewResult
select (@x/@t)*@m As NewResult
no, because / is evaluated before *, but I prefer to put them to make it clear for readers.
See, with the ( ) it is CLEAR to everyone that you want to make the / first, without the / you have to ask yourself the question or test or read the documentation.
See, with the ( ) it is CLEAR to everyone that you want to make the / first, without the / you have to ask yourself the question or test or read the documentation.
ASKER
Thanks so much because I have learned so much
declare @x decimal (20,4)
declare @p decimal (20,4)
declare @t decimal (20,4)
SET NOCOUNT ON
SELECT @x =
(
( SELECT SUM(S2C_TTL) FROM njsurvey.dbo.AES_ES_2)
+ ( SELECT SUM(S2C_TTL) FROM njsurvey.dbo.AES_HS_2)
)
select @p = SUM(ENROLL_TTL) from njsurvey.dbo.AES_CONTACT_S
select @t = SUM(CL_TOT) from reportcards.dbo.finance WHERE YEAR = '0405'
select
@x / @p AS PerPupilSpending
@x / @t AS TotalCost