0

我在sql server中有下表

HEAD    | COL1  | COL2
------------------------
MARKS   | 546   | 798
TOTAL   | 1000  | 1000
PERCENT | NULL  | NULL

我想通过查询填充如下结果。请帮我

HEAD    | COL1  | COL2
------------------------
MARKS   | 546   | 798
TOTAL   | 1000  | 1000
PERCENT | 54.6  | 79.8
4

2 回答 2

0
update tablename set
col1 = c1, col2 = c2 from
(
Select x.col1*100./y.col1 c1, x.col2*100./y.col2 c2 from
(select * from TableName where head='mark') x
cross join 
(select * from TableName where head='total') y
 ) z
where head = 'percent';
于 2013-07-13T13:03:20.813 回答
0

不会接受你的表结构。它是颠倒的,伤害了我的眼睛。为您的表结构编写解决方案只会治愈症状,我正在尝试治愈真正的问题。

CREATE TABLE Table1
([head] varchar(10), [marks] INT, [total] INT, [pct] as cast(100.0*marks / total as numeric(4,1)))

INSERT INTO Table1(head, marks, total)
VALUES ('Col1', 546, 1000),('Col2', 798, 1000)

SELECT head, marks, total FROM Table1

结果:

head  marks  total  pct
Col1  546    1000   54.6
Col2  798    1000   79.8
于 2013-07-13T11:31:19.517 回答