Using a calculated column in a SQL Select statement
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty{ margin-bottom:0;
}
I am trying to write a query. Column A comes from database, column B is either 1 or -1 and it is result of a CASE statement that looks into the same table. Now, my column C needs to refer to column B as part of it's calculation (let's say it is Column_B * Column_X ). How is this done since I cannot use Column_B in my SELECT statement.
sql-server query
add a comment |
I am trying to write a query. Column A comes from database, column B is either 1 or -1 and it is result of a CASE statement that looks into the same table. Now, my column C needs to refer to column B as part of it's calculation (let's say it is Column_B * Column_X ). How is this done since I cannot use Column_B in my SELECT statement.
sql-server query
add a comment |
I am trying to write a query. Column A comes from database, column B is either 1 or -1 and it is result of a CASE statement that looks into the same table. Now, my column C needs to refer to column B as part of it's calculation (let's say it is Column_B * Column_X ). How is this done since I cannot use Column_B in my SELECT statement.
sql-server query
I am trying to write a query. Column A comes from database, column B is either 1 or -1 and it is result of a CASE statement that looks into the same table. Now, my column C needs to refer to column B as part of it's calculation (let's say it is Column_B * Column_X ). How is this done since I cannot use Column_B in my SELECT statement.
sql-server query
sql-server query
asked 3 hours ago
Nico M.Nico M.
298
298
add a comment |
add a comment |
1 Answer
1
active
oldest
votes
If I read your question correctly, what I think you are asking is - how do I use the result of one calculated column in another? Well, if you want to actually add that column to the table you could just inline the calculation within your new column - this would work but requires more maintenance and is probably not an ideal solution (this also works in a SELECT statement but can increase maintenance overhead):
CREATE TABLE T
(
A INT NOT NULL
, X INT NOT NULL
, (CASE A WHEN NULL THEN 0 ELSE 1 END) AS B
, (CASE A WHEN NULL THEN 0 ELSE 1 END) * X AS C
);
This can become messy and can be painful to maintain if the calculations are complicated. You can however define your base calculations on the table and then put a view over the top which can then use your calculated columns:
CREATE VIEW V
AS
SELECT A, B, B * X AS C
FROM T;
This is my recommendation due to how really simple it is to maintain because the logic is isolated. The calculation of C never needs to know how B is calculated, it just uses the value that is generated.
If you don't like that, then your other option is to define the logic within the query against the table itself and then perform the calculation on that. There are a few ways to do this, here are a couple of the ones I prefer:
COMMON TABLE EXPRESSIONS
-- CTE Method
WITH results AS
(
SELECT A
, (CASE A WHEN NULL THEN 0 ELSE 1 END) AS B
, X -- don't forget to include the other columns you need!
FROM MyTable
)
SELECT A, B, B * X AS C
FROM results;
SUBQUERIES
-- SubQuery
SELECT A, B, B * X AS C
FROM (SELECT A
, (CASE A WHEN NULL THEN 0 ELSE 1 END) AS B
, X -- don't forget to include the other columns you need!
FROM MyTable) AS results;
TABLE VARIABLE
-- Total overkill, but can be done (could also use a temp table instead)
DECLARE @results TABLE
(
A INT NOT NULL,
B INT NOT NULL,
X INT NOT NULL
);
INSERT @results
SELECT A
, (CASE A WHEN NULL THEN 0 ELSE 1 END) AS B
, X -- don't forget to include the other columns you need!
FROM MyTable;
SELECT A, B, B * X AS C
FROM @results;
what is the best approach if I have multiple calculated columns. A scenario when I have A and X from table and then: B: calculated from A and X. C:calculated from A and B, and D: calculated from C and X, etc.
– Nico M.
2 hours ago
Use the CTE approach, you can have multiple CTE's which then derive values from the previous ones. This will allow you to have calculations that rely on multiple other calculations.
– Mr.Brownstone
1 hour ago
add a comment |
Your Answer
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "182"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: false,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: null,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fdba.stackexchange.com%2fquestions%2f234244%2fusing-a-calculated-column-in-a-sql-select-statement%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
If I read your question correctly, what I think you are asking is - how do I use the result of one calculated column in another? Well, if you want to actually add that column to the table you could just inline the calculation within your new column - this would work but requires more maintenance and is probably not an ideal solution (this also works in a SELECT statement but can increase maintenance overhead):
CREATE TABLE T
(
A INT NOT NULL
, X INT NOT NULL
, (CASE A WHEN NULL THEN 0 ELSE 1 END) AS B
, (CASE A WHEN NULL THEN 0 ELSE 1 END) * X AS C
);
This can become messy and can be painful to maintain if the calculations are complicated. You can however define your base calculations on the table and then put a view over the top which can then use your calculated columns:
CREATE VIEW V
AS
SELECT A, B, B * X AS C
FROM T;
This is my recommendation due to how really simple it is to maintain because the logic is isolated. The calculation of C never needs to know how B is calculated, it just uses the value that is generated.
If you don't like that, then your other option is to define the logic within the query against the table itself and then perform the calculation on that. There are a few ways to do this, here are a couple of the ones I prefer:
COMMON TABLE EXPRESSIONS
-- CTE Method
WITH results AS
(
SELECT A
, (CASE A WHEN NULL THEN 0 ELSE 1 END) AS B
, X -- don't forget to include the other columns you need!
FROM MyTable
)
SELECT A, B, B * X AS C
FROM results;
SUBQUERIES
-- SubQuery
SELECT A, B, B * X AS C
FROM (SELECT A
, (CASE A WHEN NULL THEN 0 ELSE 1 END) AS B
, X -- don't forget to include the other columns you need!
FROM MyTable) AS results;
TABLE VARIABLE
-- Total overkill, but can be done (could also use a temp table instead)
DECLARE @results TABLE
(
A INT NOT NULL,
B INT NOT NULL,
X INT NOT NULL
);
INSERT @results
SELECT A
, (CASE A WHEN NULL THEN 0 ELSE 1 END) AS B
, X -- don't forget to include the other columns you need!
FROM MyTable;
SELECT A, B, B * X AS C
FROM @results;
what is the best approach if I have multiple calculated columns. A scenario when I have A and X from table and then: B: calculated from A and X. C:calculated from A and B, and D: calculated from C and X, etc.
– Nico M.
2 hours ago
Use the CTE approach, you can have multiple CTE's which then derive values from the previous ones. This will allow you to have calculations that rely on multiple other calculations.
– Mr.Brownstone
1 hour ago
add a comment |
If I read your question correctly, what I think you are asking is - how do I use the result of one calculated column in another? Well, if you want to actually add that column to the table you could just inline the calculation within your new column - this would work but requires more maintenance and is probably not an ideal solution (this also works in a SELECT statement but can increase maintenance overhead):
CREATE TABLE T
(
A INT NOT NULL
, X INT NOT NULL
, (CASE A WHEN NULL THEN 0 ELSE 1 END) AS B
, (CASE A WHEN NULL THEN 0 ELSE 1 END) * X AS C
);
This can become messy and can be painful to maintain if the calculations are complicated. You can however define your base calculations on the table and then put a view over the top which can then use your calculated columns:
CREATE VIEW V
AS
SELECT A, B, B * X AS C
FROM T;
This is my recommendation due to how really simple it is to maintain because the logic is isolated. The calculation of C never needs to know how B is calculated, it just uses the value that is generated.
If you don't like that, then your other option is to define the logic within the query against the table itself and then perform the calculation on that. There are a few ways to do this, here are a couple of the ones I prefer:
COMMON TABLE EXPRESSIONS
-- CTE Method
WITH results AS
(
SELECT A
, (CASE A WHEN NULL THEN 0 ELSE 1 END) AS B
, X -- don't forget to include the other columns you need!
FROM MyTable
)
SELECT A, B, B * X AS C
FROM results;
SUBQUERIES
-- SubQuery
SELECT A, B, B * X AS C
FROM (SELECT A
, (CASE A WHEN NULL THEN 0 ELSE 1 END) AS B
, X -- don't forget to include the other columns you need!
FROM MyTable) AS results;
TABLE VARIABLE
-- Total overkill, but can be done (could also use a temp table instead)
DECLARE @results TABLE
(
A INT NOT NULL,
B INT NOT NULL,
X INT NOT NULL
);
INSERT @results
SELECT A
, (CASE A WHEN NULL THEN 0 ELSE 1 END) AS B
, X -- don't forget to include the other columns you need!
FROM MyTable;
SELECT A, B, B * X AS C
FROM @results;
what is the best approach if I have multiple calculated columns. A scenario when I have A and X from table and then: B: calculated from A and X. C:calculated from A and B, and D: calculated from C and X, etc.
– Nico M.
2 hours ago
Use the CTE approach, you can have multiple CTE's which then derive values from the previous ones. This will allow you to have calculations that rely on multiple other calculations.
– Mr.Brownstone
1 hour ago
add a comment |
If I read your question correctly, what I think you are asking is - how do I use the result of one calculated column in another? Well, if you want to actually add that column to the table you could just inline the calculation within your new column - this would work but requires more maintenance and is probably not an ideal solution (this also works in a SELECT statement but can increase maintenance overhead):
CREATE TABLE T
(
A INT NOT NULL
, X INT NOT NULL
, (CASE A WHEN NULL THEN 0 ELSE 1 END) AS B
, (CASE A WHEN NULL THEN 0 ELSE 1 END) * X AS C
);
This can become messy and can be painful to maintain if the calculations are complicated. You can however define your base calculations on the table and then put a view over the top which can then use your calculated columns:
CREATE VIEW V
AS
SELECT A, B, B * X AS C
FROM T;
This is my recommendation due to how really simple it is to maintain because the logic is isolated. The calculation of C never needs to know how B is calculated, it just uses the value that is generated.
If you don't like that, then your other option is to define the logic within the query against the table itself and then perform the calculation on that. There are a few ways to do this, here are a couple of the ones I prefer:
COMMON TABLE EXPRESSIONS
-- CTE Method
WITH results AS
(
SELECT A
, (CASE A WHEN NULL THEN 0 ELSE 1 END) AS B
, X -- don't forget to include the other columns you need!
FROM MyTable
)
SELECT A, B, B * X AS C
FROM results;
SUBQUERIES
-- SubQuery
SELECT A, B, B * X AS C
FROM (SELECT A
, (CASE A WHEN NULL THEN 0 ELSE 1 END) AS B
, X -- don't forget to include the other columns you need!
FROM MyTable) AS results;
TABLE VARIABLE
-- Total overkill, but can be done (could also use a temp table instead)
DECLARE @results TABLE
(
A INT NOT NULL,
B INT NOT NULL,
X INT NOT NULL
);
INSERT @results
SELECT A
, (CASE A WHEN NULL THEN 0 ELSE 1 END) AS B
, X -- don't forget to include the other columns you need!
FROM MyTable;
SELECT A, B, B * X AS C
FROM @results;
If I read your question correctly, what I think you are asking is - how do I use the result of one calculated column in another? Well, if you want to actually add that column to the table you could just inline the calculation within your new column - this would work but requires more maintenance and is probably not an ideal solution (this also works in a SELECT statement but can increase maintenance overhead):
CREATE TABLE T
(
A INT NOT NULL
, X INT NOT NULL
, (CASE A WHEN NULL THEN 0 ELSE 1 END) AS B
, (CASE A WHEN NULL THEN 0 ELSE 1 END) * X AS C
);
This can become messy and can be painful to maintain if the calculations are complicated. You can however define your base calculations on the table and then put a view over the top which can then use your calculated columns:
CREATE VIEW V
AS
SELECT A, B, B * X AS C
FROM T;
This is my recommendation due to how really simple it is to maintain because the logic is isolated. The calculation of C never needs to know how B is calculated, it just uses the value that is generated.
If you don't like that, then your other option is to define the logic within the query against the table itself and then perform the calculation on that. There are a few ways to do this, here are a couple of the ones I prefer:
COMMON TABLE EXPRESSIONS
-- CTE Method
WITH results AS
(
SELECT A
, (CASE A WHEN NULL THEN 0 ELSE 1 END) AS B
, X -- don't forget to include the other columns you need!
FROM MyTable
)
SELECT A, B, B * X AS C
FROM results;
SUBQUERIES
-- SubQuery
SELECT A, B, B * X AS C
FROM (SELECT A
, (CASE A WHEN NULL THEN 0 ELSE 1 END) AS B
, X -- don't forget to include the other columns you need!
FROM MyTable) AS results;
TABLE VARIABLE
-- Total overkill, but can be done (could also use a temp table instead)
DECLARE @results TABLE
(
A INT NOT NULL,
B INT NOT NULL,
X INT NOT NULL
);
INSERT @results
SELECT A
, (CASE A WHEN NULL THEN 0 ELSE 1 END) AS B
, X -- don't forget to include the other columns you need!
FROM MyTable;
SELECT A, B, B * X AS C
FROM @results;
edited 2 hours ago
answered 2 hours ago
Mr.BrownstoneMr.Brownstone
9,84232343
9,84232343
what is the best approach if I have multiple calculated columns. A scenario when I have A and X from table and then: B: calculated from A and X. C:calculated from A and B, and D: calculated from C and X, etc.
– Nico M.
2 hours ago
Use the CTE approach, you can have multiple CTE's which then derive values from the previous ones. This will allow you to have calculations that rely on multiple other calculations.
– Mr.Brownstone
1 hour ago
add a comment |
what is the best approach if I have multiple calculated columns. A scenario when I have A and X from table and then: B: calculated from A and X. C:calculated from A and B, and D: calculated from C and X, etc.
– Nico M.
2 hours ago
Use the CTE approach, you can have multiple CTE's which then derive values from the previous ones. This will allow you to have calculations that rely on multiple other calculations.
– Mr.Brownstone
1 hour ago
what is the best approach if I have multiple calculated columns. A scenario when I have A and X from table and then: B: calculated from A and X. C:calculated from A and B, and D: calculated from C and X, etc.
– Nico M.
2 hours ago
what is the best approach if I have multiple calculated columns. A scenario when I have A and X from table and then: B: calculated from A and X. C:calculated from A and B, and D: calculated from C and X, etc.
– Nico M.
2 hours ago
Use the CTE approach, you can have multiple CTE's which then derive values from the previous ones. This will allow you to have calculations that rely on multiple other calculations.
– Mr.Brownstone
1 hour ago
Use the CTE approach, you can have multiple CTE's which then derive values from the previous ones. This will allow you to have calculations that rely on multiple other calculations.
– Mr.Brownstone
1 hour ago
add a comment |
Thanks for contributing an answer to Database Administrators Stack Exchange!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fdba.stackexchange.com%2fquestions%2f234244%2fusing-a-calculated-column-in-a-sql-select-statement%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown