in Education by
I need to sum some rows applying on each row a formula and after all sum in a last column all the results. I'd like that in the total column I didn't have to especify the previous formulas but use the name of the column, I mean something like the following code (which doesn't work because total_1, total_2 and total_3 are not recognized) SELECT t.id, t.dpt, sum(t1.coefficient * t1.value) AS total_1, sum(t2.coefficient * t2.value) AS total_2, sum(t3.coefficient * t3.value) AS total_3, (total_1 + total_2 + total_3) AS total -- Not recognized FROM my table t JOIN table1 t1 ON... JOIN table2 t2 ON... JOIN table3 t3 ON... group by t.id, t.dpt I know I could do something like thefollowing, but it emplies some redundancy in the definitions of the sums. SELECT t.id, t.dpt, sum(t1.coefficient * t1.value) AS total_1, sum(t2.coefficient * t2.value) AS total_2, sum(t3.coefficient * t3.value) AS total_3, sum(t1.coefficient * t1.value + t2.coefficient * t2.value + t3.coefficient * t3.value) AS total FROM my table t JOIN table1 t1 ON... JOIN table2 t2 ON... JOIN table3 t3 ON... group by t.id, t.dpt Is there any efficient query which can be helpful to avoid redefining the columns sums in the total column? Thanks JavaScript questions and answers, JavaScript questions pdf, JavaScript question bank, JavaScript questions and answers pdf, mcq on JavaScript pdf, JavaScript questions and solutions, JavaScript mcq Test , Interview JavaScript questions, JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)

1 Answer

0 votes
by
The simplest way I think of is: SELECT *, (total_1 + total_2 + total_3) AS total FROM ( SELECT t.id, t.dpt, sum(t1.coefficient * t1.value) AS total_1, sum(t2.coefficient * t2.value) AS total_2, sum(t3.coefficient * t3.value) AS total_3 FROM my table t JOIN table1 t1 ON... JOIN table2 t2 ON... JOIN table3 t3 ON... GROUP BY t.id, t.dpt ) t Added: I've tested queries like bellow (I know that they differ, but you should test in on your own it's the best way to spot the difference). Before each run I've executed DBCC DROPCLEANBUFFERS and DBCC FREEPROCCACHE. 2nd run were executed after few minutes and the difference is due to the server load probably. SET STATISTICS TIME ON DBCC DROPCLEANBUFFERS DBCC FREEPROCCACHE go select t1.col1, sum(i.col2) as s, sum(i.col2) * 4 as smult from tab1 t1 join tab2 t2 on t1.col1 = t2.col1 group by t1.col1 DBCC DROPCLEANBUFFERS DBCC FREEPROCCACHE go select *, s * 4 as smult from ( select t1.col1, sum(i.col2) as s from tab1 t1 join tab2 t2 on t1.col1 = t2.col1 group by t1.col1 ) t 1st query 1st run: SQL Server parse and compile time: CPU time = 0 ms, elapsed time = 516 ms. SQL Server Execution Times: CPU time = 63 ms, elapsed time = 3958 ms. 1st query 2nd run: SQL Server parse and compile time: CPU time = 0 ms, elapsed time = 374 ms. SQL Server Execution Times: CPU time = 31 ms, elapsed time = 4152 ms. 2nd query 1st run: SQL Server parse and compile time: CPU time = 0 ms, elapsed time = 513 ms. SQL Server Execution Times: CPU time = 94 ms, elapsed time = 3445 ms. 2nd query 2nd run: SQL Server parse and compile time: CPU time = 16 ms, elapsed time = 363 ms. SQL Server Execution Times: CPU time = 125 ms, elapsed time = 3146 ms.

Related questions

0 votes
    I need to sum some rows applying on each row a formula and after all sum in a last column all ... JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Feb 18, 2022 in Education by JackTerrance
0 votes
    I want to make a table to be able to help me make price comparisons. Explanations: In my table I ... JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked May 15, 2022 in Education by JackTerrance
0 votes
    A __________ is a set of elements appearing in rows and columns where the elements are of the same mode whether ... of R Programming Select the correct answer from above options...
asked Feb 13, 2022 in Education by JackTerrance
0 votes
    ___________ provides optional labels with the columns and rows. (a) Disnames (b) Dimnames (c) Denmes (d) ... Out of R Programming Select the correct answer from above options...
asked Feb 12, 2022 in Education by JackTerrance
0 votes
    ….and….are two ways of entering data in calc a) Right and left. b)rows and columns. c) Constants and formulas Select the correct answer from above options...
asked Dec 13, 2021 in Education by JackTerrance
0 votes
    in split cell window we can specify the number of (i) Rows and cells (ii) Rows and Columns (iii) Columns and ... (iv) Tables and Cells Select the correct answer from above options...
asked Dec 5, 2021 in Education by JackTerrance
0 votes
    7. There are … rows and ……… columns in a single worksheet a. 1045588 and 1034 b. 1048576 and 1024 c. 104566 and 1014 Select the correct answer from above options...
asked Nov 30, 2021 in Education by JackTerrance
0 votes
    in split cell window we can specify the number of (i) Rows and cells (ii) Rows and Columns (iii) Columns and ... (iv) Tables and Cells Select the correct answer from above options...
asked Nov 26, 2021 in Education by JackTerrance
0 votes
    in split cell window we can specify the number of (i) Rows and cells (ii) Rows and Columns (iii) Columns and ... (iv) Tables and Cells Select the correct answer from above options...
asked Nov 25, 2021 in Education by JackTerrance
0 votes
    I have some long data I need to convert into a wide fixed width text format. I have a large ... JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Jul 11, 2022 in Education by JackTerrance
0 votes
    I am trying to figure out how to pull only specific rows and columns using Google apps script. The ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked May 13, 2022 in Education by JackTerrance
0 votes
    __________ is used when you have variables that form rows instead of columns. (a) tidy() (b) spread() (c ... Analysis of R Programming Select the correct answer from above options...
asked Feb 10, 2022 in Education by JackTerrance
0 votes
    Which of the following statement applies the function (FUN) to either rows (1) or columns (2) on object ... Regression of R Programming Select the correct answer from above options...
asked Feb 9, 2022 in Education by JackTerrance
0 votes
    I want to bind two data frames with different set of columns and I also want to retain all the columns that fails to ... do that in R? Select the correct answer from above options...
asked Jan 23, 2022 in Education by JackTerrance
0 votes
    In _______________ index instead of storing all the columns for a record together, each column is stored ... topic in portion Indexing and Hashing of Database Management...
asked Oct 10, 2021 in Education by JackTerrance
...