top button
Flag Notify
    Connect to us
      Site Registration

Site Registration

Please let me know the query to update this

+4 votes
316 views

Table Name : Hist_table

 Shp_cd wt_grp  net_wt  tot_wt
   101   10       9      7
   102   20       8      2
   103   15       4      1

Factor_table

Fact_id fact_column factor
  1     wt_grp        2
  2     net_wt        5
  3     tot_wt        3

Note – this factor table contains rows as the column name of hist_table.
Now we have to update the Hist_table with the multiplied with the factors
For example wt_grp factor is 2 then we have to update all the wt_grp column of hist table as Hist_table.wt_grp * factor_table.factor
So the result should be

shpcd   wt_grp  net_wt  tot_wt
101     20        45    21
102     40        40    6
103     30        20    3
posted Jun 18, 2015 by Shivaranjini

Share this question
Facebook Share Button Twitter Share Button LinkedIn Share Button

1 Answer

+2 votes
 
Best answer

-- sample data

DECLARE @Hist TABLE (Shp_cd int, wt_grp int, net_wt int, tot_wt int)
INSERT INTO @Hist (Shp_cd, wt_grp, net_wt, tot_wt) VALUES (101, 10, 9, 7)
INSERT INTO @Hist (Shp_cd, wt_grp, net_wt, tot_wt) VALUES (102, 20, 8, 2)
INSERT INTO @Hist (Shp_cd, wt_grp, net_wt, tot_wt) VALUES (103, 15, 4, 1)

DECLARE @Factor TABLE (Fact_ID INT IDENTITY(1,1), fact_column varchar(6), factor int)
INSERT INTO @Factor (fact_column, factor) VALUES ('wt_grp', 2)
INSERT INTO @Factor (fact_column, factor) VALUES ('net_wt', 5)
INSERT INTO @Factor (fact_column, factor) VALUES ('tot_wt', 3)

-- cross join to pick up factors

SELECT 
    h.shp_cd,
    MAX(CASE WHEN f.fact_column = 'wt_grp' THEN wt_grp * factor END) AS wt_grp,
    MAX(CASE WHEN f.fact_column = 'net_wt' THEN net_wt * factor END) AS net_wt,
    MAX(CASE WHEN f.fact_column = 'tot_wt' THEN tot_wt * factor END) AS tot_wt
FROM 
    @Hist h
     CROSS JOIN 
    @Factor f
GROUP BY h.Shp_cd -- to bring it down to a single resultset
answer Jun 18, 2015 by Manikandan J
Similar Questions
+3 votes
Name | Gender -------------- 
A | F 
B | M 
C | F 
D | F 
E | M

From the above data, gender was wrongly entered, which means in place of F it should be M and in place of M it should F. How to update whole table with a single line sql query (don't use pl/sql block). Since, if I will update gender column one by one, then possible error would be all rows values of gender column becomes either F or M.
Final output should be

Name | Gender -------------- 
A | M 
B | F 
C | M 
D | M
 E | F 
+2 votes

I would like to trigger the specific cause on intra frequency Handover.
Could you please advice how to trigger below causes?

  1. Unknown
  2. Unspecified
  3. Time Critical Handover
  4. Handover Desirable for Radio Reasons
  5. Resource Optimisation Handover

Best Regard.

...