My question here is what is the difference between CTE and View in SQL . I mean, in this case I have to use CTE , and in the case of View . I know that both are virtual tables, but I cannot distinguish between their use.
I found a similar question here , but it's about performance.
Update 1:
For example: I have a database filled with deals ( tbl_trade ). I need to select from the 3.5 million records only those transactions that were open in the current month before the current time, and then manipulate the data (with various queries on the virtual table - it looks like View). The problem here is that I want a SUM of 3-4 columns, and then I need to SUM several columns and create a virtual column with the result (looks like a CTE).
For example: tbl_trade has columns: profit , bonus and expenses . I need SUM(profit) , SUM(bonus) , SUM(expenses) and a new column total , which will be equal to SUM(profit) + SUM(bonus) + SUM(expenses) .
PS. Restarting requests for SUM not an option, as I already have a result.
Thanks in advance!
sql sql-server sql-view common-table-expression
Blackm
source share