1. Anuncie Aqui ! Entre em contato fdantas@4each.com.br

[SQL] SQL - CTE vs VIEW

Discussão em 'Outras Linguagens' iniciado por Stack, Novembro 6, 2024 às 20:42.

  1. Stack

    Stack Membro Participativo

    My question here is what is the difference between CTE and View in SQL. I mean in which case I should use the CTE and which case the View. I know that both are some kind of virtual tables but I can't differentiate their use.

    I found a similar question here but it's about performance.

    Update 1:

    For example: I have a database filled with trades(tbl_trade). I need to select from 3.5 millions records only the trades that was opened the current month until current time and then manipulate the data(with different queries on the virtual table - this looks like View). The problem here is that I want a SUM of 3-4 columns and then on I need to SUM some columns and create a virtual column with the result(looks like CTE).

    Eg: 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. Rerunning the queries for SUM is not an option since I already have the result.

    Thanks in advance!

    Continue reading...

Compartilhe esta Página