Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Update sql temp table column with computational expression

Tags:

sql

sql-server

I perform the SQL code below. Although computational expression is not Zero, all rows update with zero, as an additional information denominator is bigger in every cases.

UPDATE #t
SET ProportionOfvehicle = (CountOfVehicle / @TotalNumberOfPrivateVehicles)*100
WHERE LegalPersonID IS NOT NULL;
like image 883
Masoud Abrishami Avatar asked Apr 11 '26 00:04

Masoud Abrishami


1 Answers

I could finally solved the problem by my self. I created a temp table which the data type for its computational fields are float like below

**CREATE TABLE #t
(
    CountOfVehicle BIGINT,
    LegalPersonID BIGINT,
    RegionID BIGINT,
    ProportionOfvehicle FLOAT,
    ProportionOfAccident FLOAT,
    ProportionOfPersonel FLOAT
)**

then insert into it for other fields except computational fields with code below

**INSERT  INTO #t
(
    CountOfVehicle,
    LegalPersonID,
    RegionID,
    ProportionOfvehicle,
    ProportionOfAccident,
    ProportionOfPersonel
)
SELECT COUNT(*) AS CountOfVehicle,
       LegalPersonID,
       RegionID,
       NULL AS ProportionOfvehicle,
       NULL AS ProportionOfAccident,
       NULL AS ProportionOfPersonel
FROM Bus.VehicleOwnerships
WHERE OwnershipTypeValue IN (1,2)
GROUP BY LegalPersonID,
         RegionID** 

and finally update the computational fields with the statement below

**UPDATE #t
SET ProportionOfvehicle = CountOfVehicle / @TotalNumberOfOrganizationVehicles
WHERE RegionID IS NOT NUL**
like image 190
Masoud Abrishami Avatar answered Apr 13 '26 14:04

Masoud Abrishami



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!