简体   繁体   中英

SQL Server: set all column rows to 0 or 1 in a temporary table

I have two tables table_1 and table_2 .

Both tables contain 250 columns each, with an ID code which allows the two tables to be joined, many to many. Each column row has a value for ranging 0 - 500, with the exception of the ID codes.

I need to do some further analysis, and would like to be able to produce a temporary table or query output where, for each row for each column, the value is set to 0 or 1, where 1 is where the sum of the column by id code is >= 1 .

I can do this using case when, but with 500 columns I wonder if there is a more clever way of doing this. If it helps, all the columns in each of the tables end in the word "_bin" .

You haven't specified SQL server version, starting from SQL 2012 you can also use IIF or CHOOSE logical functions for this instead of 'case when' but it still require to do it for all columns.

more details here: https://www.mssqltips.com/sqlservertip/2570/new-logical-functions-in-sql-server-2012-iif-and-choose/

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM