Trying to use a calculated column to calculate another column - sql-server-2008

I am trying to calculate a BMI field based on height in inches and weight in pounds. I wrote a sub query but something is wrong...
SELECT CASE_NUM,HEIGHT_F AS HEIGHT_ft,HEIGHT_I AS HEIGHT_inches,WEIGHT_P AS WEIGHT_lbs,WEIGHT_O AS WEIGHT_oz,
PRESS_SYST,PRESS_DIAST,HEART_RATE,RESP_RATE,WAIST_CIR,APPROVED_DATE,STAFF_ID,
S.SORT_NAME AS SERVER_NAME,CAST(PRESS_SYST AS VARCHAR(5)) + '/' + CAST (PRESS_DIAST AS VARCHAR(5)) AS BP,
HEIGHT_F * 12 + HEIGHT_I
AS HEIGHT_TOTAL_IN
FROM (
SELECT
(HEIGHT_TOTAL_IN * HEIGHT_TOTAL_IN / WEIGHT_P) * 703 AS BMI
FROM AZCLCDEV A
INNER JOIN CDCLIENT C
ON A.CLIENT_ID = C.ID
INNER JOIN CAEMP S
ON A.STAFF_ID = S.ID

I think I can see what you were trying to do. Try this query:
SELECT *, (HEIGHT_TOTAL_IN * HEIGHT_TOTAL_IN / WEIGHT_P) * 703 AS BMI
FROM (
SELECT CASE_NUM, HEIGHT_F AS HEIGHT_ft, HEIGHT_I AS HEIGHT_inches,
WEIGHT_P AS WEIGHT_lbs,WEIGHT_O AS WEIGHT_oz,
PRESS_SYST, PRESS_DIAST, HEART_RATE, RESP_RATE, WAIST_CIR,
APPROVED_DATE, STAFF_ID, S.SORT_NAME AS SERVER_NAME,
CAST(PRESS_SYST AS VARCHAR(5)) + '/' + CAST (PRESS_DIAST AS VARCHAR(5)) AS BP,
HEIGHT_F * 12 + HEIGHT_I AS HEIGHT_TOTAL_IN
FROM AZCLCDEV A
INNER JOIN CDCLIENT C
ON A.CLIENT_ID = C.ID
INNER JOIN CAEMP S
ON A.STAFF_ID = S.ID
) OrigQuery

Related

Join 2 query in different as different column

I need to join 2 queries within a database but place it in 2 different column
i tried union but the result are in the same column "total fulltime"
(SELECT (SUM(servicehours) + SUM(`teachinghours`) + SUM(`researchhours`)) as 'Total fulltime'
FROM staff_hour sh,staff s,role r
WHERE r.roleid =s.roleid
AND s.staffid = sh.staffid
AND r.roleid='2'
AND sh.staffperiodyear = 'FY2018')
union
(SELECT (SUM(servicehours) + SUM(`teachinghours`) + SUM(`researchhours`)) as 'Total parttime'
FROM staff_hour sh,staff s,role r
WHERE r.roleid =s.roleid
AND s.staffid = sh.staffid
AND r.roleid='3'
AND sh.staffperiodyear = 'FY2018')
How do i join it so that there are 2 different column('total fulltime' & 'total parttime') with the total sum?
You could use your queries with cross join
select t1.Total_fulltime
, t2.Total_parttime
, t1.Total_fulltime + t2.Total_parttime total_sum
from (SELECT (SUM(servicehours) + SUM(`teachinghours`) + SUM(`researchhours`)) as Total_fulltime
FROM staff_hour sh,staff s,role r
WHERE r.roleid =s.roleid
AND s.staffid = sh.staffid
AND r.roleid='2'
AND sh.staffperiodyear = 'FY2018') t1
CROSS JOIN
(SELECT (SUM(servicehours) + SUM(`teachinghours`) + SUM(`researchhours`)) as Total_parttime
FROM staff_hour sh,staff s,role r
WHERE r.roleid =s.roleid
AND s.staffid = sh.staffid
AND r.roleid='3'
AND sh.staffperiodyear = 'FY2018') t2

How to GROUP BY an ID the result of a SUM with a JOIN

I have two Tables, one with my contacts informations (contact) and one with the users interactions (notitia).
Here's an exemple :
SELECT n.contact_id, c.email, c.numero, c.code_postal, n.nbr_click, n.nbr_ouverture,
(
n.nbr_click * 2
)
+
(
n.nbr_ouverture * 3
)
as total FROM notitia n LEFT JOIN contact c ON c.id = n.contact_id LIMIT 5
The result is :
Now I'm Trying to do the SUM of the nbr_click, nbr_ouverture and total if the contact_id is the same, In my exemple we have 2 times the contact_id 3 so the result should be :
I tried to GROUP BY n.contact_id :
SELECT n.contact_id, c.email, c.numero, c.code_postal, n.nbr_click, n.nbr_ouverture,
(
n.nbr_click * 2
)
+
(
n.nbr_ouverture * 3
)
as total FROM notitia n LEFT JOIN contact c ON c.id = n.contact_id GROUP BY n.contact_id LIMIT 5
But here only the first Row with the contact_id = 3 is taken
I also tried to do the SUM :
SELECT n.contact_id, c.email, c.numero, c.code_postal, SUM(n.nbr_click), SUM(n.nbr_ouverture),
(
SUM(n.nbr_click) * 2
)
+
(
SUM(n.nbr_ouverture) * 3
)
as total FROM notitia n LEFT JOIN contact c ON c.id = n.contact_id LIMIT 5
But here everything is added in on row with enormous result
You should have kind of the mix of the two queries you have written. You should GROUP BY contact_id and then apply SUM to the columns for which you need the SUM. Try this:
SELECT n.contact_id,
c.email, c.numero, c.code_postal, SUM(n.nbr_click), SUM(n.nbr_ouverture),
(
SUM(n.nbr_click) * 2
)
+
(
SUM(n.nbr_ouverture) * 3
)
as total FROM notitia n
LEFT JOIN contact c ON c.id = n.contact_id
GROUP BY n.contact_id LIMIT 5

use join count in calculation in select

SELECT e.qty * e.unit_cost * e.unit_no * (e.factor/100) AS Ext_price, c.cost_type, c.cost_dt, c.internal_cost_amt, c.client_cost_amt
FROM equipment e
LEFT JOIN CostAllocation c ON (c.object_row_id = e.row_id)
I now want to divide the Ext_price by the number of CostAllocation rows that match the equipment row. How would I do that?
SELECT e.qty*e.unit_cost*e.unit_no*e.factor/(100*x.cnt) AS Ext_price,
c.cost_type,
c.cost_dt,
c.internal_cost_amt,
c.client_cost_amt
FROM equipment e
LEFT JOIN CostAllocation c ON c.object_row_id = e.row_id
LEFT JOIN
(SELECT row_id,
count(*) cnt
FROM CostAllocation
GROUP BY row_id) x ON c.object_row_id = x.row_id
try below:
select g.*,g.Ext_price/(select count(*) from CostAllocation k where k.object_row_id=g.row_id) as youranswer
from
(
SELECT e.row_id,e.qty*e.unit_cost*e.unit_no*e.factor/100 AS Ext_price, c.cost_type, c.cost_dt, c.internal_cost_amt, c.client_cost_amt
FROM equipment e
LEFT JOIN CostAllocation c ON c.object_row_id = e.row_id)g
Since mysql does not support analytical functions it is probably easiest to add a subquery (untested):
SELECT Ext_price, cost_type, cost_dt, internal_cost_amt
, client_cost_amt
, Ext_price / ( nullif( (select count(1)
from CostAllocation c2
where c2.object_row_id = e.row_id), 0) )
FROM (
SELECT e.qty*e.unit_cost*e.unit_no*e.factor/100 AS Ext_price
, c.cost_type, c.cost_dt, c.internal_cost_amt, c.client_cost_amt
FROM equipment e
LEFT JOIN CostAllocation c
ON c.object_row_id = e.row_id
) as T
I added nullif to avoid division by zero.

No Join Predicate Warning - Why Does this query never complete/timeout?

Here is my query that I get a "No Join Predicate" warning:
select DISTINCT d.* from Device d , Company c1
WHERE d.CustomerID = c1.CompanyID AND c1.CompanyNumber in
(SELECT * FROM [dbo].[Split] ('56257100', ','))
OR
EXISTS ( select * from (SELECT * FROM [dbo].[Split] ('American', ',')) as s
WHERE d.CustomerID = c1.CompanyID AND c1.CompanyName like '%' + s.items + '%' )
Essentially I had two queries, and I want to join them with an OR (get the results from both). I believe it's trying to join the two queries as a Cartesian product, which isn't what I want.
If I separate it as 2 queries and UNION the results, it is fast. This however makes me re-structure my dynamic SQL quite a bit. I'd like to keep it formatted where I can have an OR there, or an AND if need be.
The parenthesis are incorrect. They should be as follows so that it knows d is the same across the OR
select DISTINCT d.* from Device d , Company c1
WHERE d.CustomerID = c1.CompanyID AND
(
c1.CompanyNumber in (SELECT * FROM [dbo].[Split] ('56257100', ','))
OR
EXISTS ( select * from (SELECT * FROM [dbo].[Split] ('American', ',')) as s
WHERE d.CustomerID = c1.CompanyID AND c1.CompanyName like '%' + s.items + '%' )
)

Mysql query - JOINS and WHERE clause, beginner here :)

having difficulties with a form and mysql. 3 tables, 1 sum of a tables values. The form provides the value to search, but it does not work with the "WHERE >= '$search_total_rating" being in the wrong place, i am doing something very wrong here.
$result = mysql_query("SELECT coffeeshops.*, services.*, ratings.*, sum(temp.total) as final_total FROM coffeeshops inner join services on coffeeshops.shop_id=services.shop_id
inner join ratings on coffeeshops.shop_id=ratings.shop_id
inner join (select SUM(comfort + service + ambience + friendliness + spacious)/(5) / COUNT(shop_id) AS total, shop_id FROM ratings GROUP BY shop_id) as temp on coffeeshops.shop_id=temp.shop_id WHERE >= '$search_total_rating'");
I do not fully understand this, but what i am trying to do is WHERE the total rating sum is >= selected rating. I am trying to access final_total which is not an actual column in my database, that is why SUM is being used to get the total rating for each shop. Hopefully it is a minor shuffle of the code. Thanks
You should use having instead of where
SELECT coffeeshops.*, services.*, ratings.*, sum(temp.total) as final_total
FROM coffeeshops inner join services on coffeeshops.shop_id=services.shop_id
inner join ratings on coffeeshops.shop_id=ratings.shop_id
inner join (
select SUM(comfort + service + ambience + friendliness + spacious)/5/ COUNT(shop_id) AS total, shop_id
FROM ratings GROUP BY shop_id)
as temp on coffeeshops.shop_id=temp.shop_id
having final_total >= '$search_total_rating'
You have already calculated your total in the subquery. No need for a second SUM().
SELECT coffeeshops.*
, services.*
, ratings.*
, temp.total as final_total
FROM coffeeshops
inner join services
on coffeeshops.shop_id = services.shop_id
inner join ratings
on coffeeshops.shop_id = ratings.shop_id
inner join
( select SUM(comfort + service + ambience + friendliness + spacious) / 5
/ COUNT(shop_id) AS total
, shop_id
FROM ratings
GROUP BY shop_id
) as temp
on coffeeshops.shop_id = temp.shop_id
WHERE temp.total >= '$search_total_rating'
You could also use a HAVING in the subquery:
SELECT coffeeshops.*
, services.*
, ratings.*
, temp.total as final_total
FROM coffeeshops
inner join services
on coffeeshops.shop_id = services.shop_id
inner join ratings
on coffeeshops.shop_id = ratings.shop_id
inner join
( select SUM(comfort + service + ambience + friendliness + spacious) / 5
/ COUNT(shop_id) AS total
, shop_id
FROM ratings
GROUP BY shop_id
HAVINGE total >= '$search_total_rating'
) as temp
on coffeeshops.shop_id = temp.shop_id