Quantcast
Channel: Transact-SQL forum
Viewing all articles
Browse latest Browse all 23857

How to write a sql query to calculate weights using CTE

$
0
0

Hi guys,

want some help using a CTE to generate data using recursive SQL - input data in table A to be transformed into table B shown below

Table A

Instru_id_index     instru_id_name    instru_id_constit  con_name   weight

        56                       INDEX A                      23                  A                 25

        56                       INDEX A                      24                 B                  25

        56                       INDEX A                      25                  C                 25

        56                       INDEX A                      57              INDEX  B       25

        57                      INDEX B                     31                  D                 33

        57                      INDEX B                     32                  E                  33

        57                      INDEX B                     33                  F                  33

(Logic should be recursive in order to be able to handle multi-level, not just level 2.)

Table B

Instru_id_index     instru_id_name    instru_id_constit  constit_name   weight

        56                       INDEX A                         23                A                   25

        56                       INDEX A                         24                B                   25

        56                       INDEX A                         25                C                   25

        56                       INDEX A                         31                D                   8.3

        56                       INDEX A                         32                E                    8.3

        56                       INDEX A                         33                F                    8.3

        57                       INDEX B                       31                 D                   33

        57                       INDEX B                       32                E                    33

        57                       INDEX B                       33                F                     33

how can I write a simple CTE construct to  display the data in table B -  How can i calculate the values of weights as 8.3 respectively - calculate these without changing the structure of the tables.

Can I do this in a CTE

Viewing all articles
Browse latest Browse all 23857

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>