Creating a dummy column based on the value of other columns

Asked

Viewed 46 times

1

I have a table with 200 columns. This table has columns that identify the individual (id) and year (year).

I need to check if the id sent information that year. If 0, did not send. If 1, sent.

For this, I need to verify if the values of the columns (c10001, c10002,...,c69993) are all equal to 0, for a given individual and year.

Example of how I want the table to look:

inserir a descrição da imagem aqui

  • c10001, c10002,c69993 are the columns I already have and I want to check the values.

At first I thought of creating a loop checking column by column until you find some with value. But I believe I have more efficient way to do this.

  • Your question seems to have some problems and your experience here in Stack Overflow may not be the best because of it. We want you to do well here and get what you want, but for that we need you to do your part. Here are some guidelines that will help you: Stack Overflow Survival Guide in English (short version). If the solution is very simple it is still possible for someone to do so in the comments.

  • 1

    What’s the point of you having multiple Dummy_aaaa if, from what I understand, only the column referring to the same value in the column ano can be 1 and the others necessarily 0? Will the function crosstabof tablefunc could not be an alternative? https://www.postgresql.org/docs/current/tablefunc.html

  • 1

    Have you tried creating a "virtual column" ? https://www.postgresql.org/docs/12/ddl-generated-columns.html would look like (c10001=0 and c10002=0 ...)

  • @Motta from what I understand, this serves to modify and use column values without changing the original table. Is that it? Does it display the table I want but not saved in the bank? If it is, it will help a lot, although I’m sure my boss will ask to add the columns in the bank.

  • @anonimo I think this might be a yes alternative! I’m seeing examples and thinking about how to use in my case. I’m still a little confused.Thanks!

  • 1

    @Rxt Virtual columns are those that being the result of a calculation would be redundant to be physically created, example value , paid and 'balance', this could be calculated , in your case the calculated could reflect the complicated rule of the 200 columns , I do not know the performance of this and how much would be used , difficult to opine without further details.

Show 1 more comment
No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.