English 中文(简体)
按实际数据分列的各栏的数据变化情况
原标题:Changing position of data in columns depending on actual data
  • 时间:2010-12-28 10:13:05
  •  标签:
  • tsql

附录一

ID  Marks1  Marks2  Marks3
-------------------------
1   10      0       4
2   0      40       90

现在,我需要首先从这个桌子上选择积极的价值观。 因此,如果标记为0,则该标记将转而正确。 选任考试委员会应在产出之后提供

ID  Marks1  Marks2  Marks3
-------------------------
1   10      4       0
2   40      90      0

您能否指导我采取这种做法? 如果能够在某种发言中进行,那将是巨大的。 提前感谢。

问题回答

如同你一样,你需要检查下几行,即上列第0栏。

Select
Coalesce(Marks1, Marks2, Marks3,0) as Marks1,
Case when marks1 is not null 
     then Coalesce(Marks2, Marks3, 0) else 0 
end as Marks2,
case when marks1 is not null 
     and marks2 is not null 
     then Coalesce(Marks3,0) 
end as Marks3

from
(
Select
Case when Marks1 =0 then null else Marks1 end as Marks1,
Case when Marks2 =0 then null else Marks2 end as Marks2,
Case when Marks3 =0 then null else Marks3 end as Marks3
  From mytbl
)




相关问题
How to write this T-SQL WHERE condition?

I ve got two tables: TableA Col1 Col2 TableB Col3 Col4 I want to join them together: SELECT * from TableA join TableB ON (...) Now, in place of ... I need to write an expression ...

Customer and Order Sql Statement

TSQL query to select all records from Customer that has an Order and also select all records from customer that does not have an Order. The table Customer contains a primary key of CustomerID. The ...

Recommended way of querying multiple Versioned tables

Have a win 2003 box with MSSQL 2005 running on it. There is a database which is populated every morning with new/modified SalesOrder made the previous day. The database has several tables: SalesOrder, ...

update duplicate record

I have a table with the following fields Id Name IsPublic i need to write a sql query that updates IsPublic to false where name has a duplicate. Only one of the duplicates should have IsPublic = ...

Define variable to use with IN operator (T-SQL)

I have a Transact-SQL query that uses the IN operator. Something like this: select * from myTable where myColumn in (1,2,3,4) Is there a way to define a variable to hold the entire list "(1,2,3,4)"? ...

Selecting records during recursive stored procedure

I ve got a content management system that contains a hierarchical structure of categories, with sub-categories subject to different ordering options at each level. Currently, that s retrieved by a (...

热门标签