SQL QUERY

I need assistance with the following SQL Query.   The Leads Total and Tech's totals are working producing a product as desired; however, the total available is not summing.  


SELECT        ID, Dates, Sta, Leads, Leads * 4 AS LeadsTotal, Techs, Techs * 8.5 AS TechsTotal, PTO, TRG, NonR, Rout, 
                         LeadsTotal + TechsTotal - NonR - Rout AS TotalAvail
FROM            PAF1Sql

Open in new window

BOEING39Asked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Dale BurrellDirectorCommented:
You can't make use of a computed column at the same level of the query.
SELECT ID, Dates, Sta, Leads, LeadsTotal, Techs, TechsTotal, PTO, TRG, NonR, Rout
  , LeadsTotal + TechsTotal - NonR - Rout AS TotalAvail
from
(
  SELECT ID, Dates, Sta, Leads
    , Leads * 4 AS LeadsTotal
    , Techs, Techs * 8.5 AS TechsTotal, PTO, TRG, NonR, Rout
  FROM
) x

Open in new window

0
QuinnDexCommented:
try this, you cant re use an AS Name until after the query is complete.


SELECT        ID, Dates, Sta, Leads, Leads * 4 AS LeadsTotal, Techs, Techs * 8.5 AS TechsTotal, PTO, TRG, NonR, Rout, 
                        ( Leads * 4) + (Techs * 8.5) - NonR - Rout AS TotalAvail
FROM            PAF1Sql

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Dale BurrellDirectorCommented:
QuinnDex's way will also work, but more scope for error, i.e. you have to keep the same calculation identical in multiple locations. I would suggest therefore that that isn't best practice for writing queries.
0
Big Business Goals? Which KPIs Will Help You

The most successful MSPs rely on metrics – known as key performance indicators (KPIs) – for making informed decisions that help their businesses thrive, rather than just survive. This eBook provides an overview of the most important KPIs used by top MSPs.

BOEING39Author Commented:
I am getting a syntax error.

SELECT ID, Dates, Sta, Leads, LeadsTotal, Techs, TechsTotal, PTO, TRG, NonR, Rout
  , LeadsTotal + TechsTotal - NonR - Rout AS TotalAvail
FROM PAF1Sql;
 
(
  SELECT ID, Dates, Sta, Leads
    , Leads * 4 AS LeadsTotal
    , Techs, Techs * 8.5 AS TechsTotal, PTO, TRG, NonR, Rout
  FROM PAF1Sql;

) x
0
Dale BurrellDirectorCommented:
Well if you tell us what the error is... Or even if you're feeling brave, google it ;)

You haven't the query I posted, you're using something inbetween, from both tables. Try the one I posted
0
PortletPaulfreelancerCommented:
It's not a complex query, instead of attempting to re-use the column aliases you could just repeat the maths where it is needed:
SELECT
        ID
      , Dates
      , Sta
      , Leads
      , Leads * 4                                 AS LeadsTotal
      , Techs
      , Techs * 8.5                               AS TechsTotal
      , PTO
      , TRG
      , NonR
      , Rout
      , (Leads * 4) + (Techs * 8.5) - NonR - Rout AS TotalAvail
FROM PAF1Sql
;

Open in new window

IF those multipliers alter at all, you might want to use parameters.

But the initial problem has already been pointed out - you cannot "re-use" the column aliases "at the same level of" query, so you can "nest" those calculations, then in the higher level then you may use the column aliases (LeadsTotal, TechsTotal).

IF this calculation is used in multiple places it would be useful either as a computed column - or perhaps by creating a view: BUT if the multipliers do change then these options may not be so useful.
0
Dale FyeCommented:
Any chance any of those values are NULL?  If so, NULL + anything = NULL.
0
Chris LuttrellSenior Database ArchitectCommented:
In your posted query, you are doing 2 queries not the subquery like suggested.  Try this
SELECT ID, Dates, Sta, Leads, LeadsTotal, Techs, TechsTotal, PTO, TRG, NonR, Rout
  , LeadsTotal + TechsTotal - NonR - Rout AS TotalAvail
FROM
(
  SELECT ID, Dates, Sta, Leads
    , Leads * 4 AS LeadsTotal
    , Techs, Techs * 8.5 AS TechsTotal, PTO, TRG, NonR, Rout
  FROM PAF1Sql
) x 

Open in new window

0
BOEING39Author Commented:
Everyone thanks for the quick responses.   I utilized QuinnDex response to get it to work; however, As Paul pointed out if the multipliers are variable this would not work so well so I did finally utilize parameters.   Again thanks for those who responded.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft SQL Server

From novice to tech pro — start learning today.