?
Solved

Divide by additional field from another database

Posted on 2006-05-01
8
Medium Priority
?
235 Views
Last Modified: 2006-11-18
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_SCHOOL)AS PerPupilSpending
/(select SUM(CL_TOT) from reportcards.dbo.finance WHERE YEAR = '0405') AS TotalCost
0
Comment
Question by:vonfranzken
  • 4
  • 4
8 Comments
 
LVL 143

Expert Comment

by:Guy Hengel [angelIII / a3]
ID: 16576040
you should do this in 2 steps:

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_SCHOOL
select @t = SUM(CL_TOT) from reportcards.dbo.finance WHERE YEAR = '0405'

select
  @x / @p AS PerPupilSpending
  @x / @t AS TotalCost
0
 

Author Comment

by:vonfranzken
ID: 16576086
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'.
0
 
LVL 143

Expert Comment

by:Guy Hengel [angelIII / a3]
ID: 16576100
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_SCHOOL
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
0
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 

Author Comment

by:vonfranzken
ID: 16576138
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
0
 
LVL 143

Accepted Solution

by:
Guy Hengel [angelIII / a3] earned 2000 total points
ID: 16576150
this looks like you declare the table fields and/or the variables as FLOAT, which are not recommended at all, you should use decimal instead.

yes:
select (@x/@t)*@m As NewResult

to do the rounding explicitely:
select cast ( (@x/@t)*@m  as decimal(20,2)) As NewResult


0
 

Author Comment

by:vonfranzken
ID: 16576163
Cool do you need the paranthesis in:

select (@x/@t)*@m As NewResult
0
 
LVL 143

Expert Comment

by:Guy Hengel [angelIII / a3]
ID: 16576169
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.
0
 

Author Comment

by:vonfranzken
ID: 16576173
Thanks so much because I have learned so much
0

Featured Post

Get expert help—faster!

Need expert help—fast? Use the Help Bell for personalized assistance getting answers to your important questions.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Ready to get certified? Check out some courses that help you prepare for third-party exams.
This shares a stored procedure to retrieve permissions for a given user on the current database or across all databases on a server.
Via a live example, show how to extract insert data into a SQL Server database table using the Import/Export option and Bulk Insert.
Via a live example, show how to setup several different housekeeping processes for a SQL Server.
Suggested Courses

621 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question