开发者

newbie SQL Question regarding computed columns

I have a table with columns开发者_如何学Go Q1 and Q2 say. I now want to define a view such that I have three columns in in Q1 Q2 and H1 such that each entry in H1 is the sum of corresponding entries Q1 and Q1

How can I do this as as SQL Query?

Thanks


I wouyld try this :

CREATE VIEW Q1Q2H1 AS
SELECT Q1,Q2,Q1+Q2 as H1
FROM Table


CREATE VIEW ComputedColumn AS
SELECT Q1, Q2, Q1 + Q2 AS H1
  FROM myTable


SELECT
  Q1, Q2, Q1 + Q2 AS H1
FROM
  table


SELECT Q1, Q2, Q1 + Q2 AS H1 FROM ...


Assuming Q1 and Q2 are numeric types, this should do:

CREATE VIEW SumView
AS
SELECT Q1, Q2, Q1 + Q2 AS H1
FROM MyTable
GO


Something like

SELECT Q1, Q2, Q1 + Q2 as H1 FROM Table;


Something like:

CREATE VIEW [MyView]
AS
SELECT     Q1, Q2, Q1 + Q2 AS H1
FROM       MyTable


All good answers, but I'd consider having a computed column on the table if your RDBMS supports it.

Eg For SQL Server

ALTER TABLE Mytable ADD H1 AS Q1 + Q2

Now it's available in all queries on this table (stored proc, triggers etc) and for constraints

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜